2015-10-15 43 views
0

我一直在關注這個教程:
Java toolbar tutorial
然而,當我試圖插入一個工具欄,圖形用戶界面,它不會顯示出來。有人可以給我建議做什麼?到目前爲止的代碼是這樣的:的Java(鞦韆)工具欄沒有顯示

import javax.swing.Box; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.GroupLayout; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.event.KeyEvent; 
import javax.swing.ImageIcon; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.AbstractAction; 
import static javax.swing.Action.MNEMONIC_KEY; 
import static javax.swing.Action.SMALL_ICON; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 
import javax.swing.KeyStroke; 
import java.awt.BorderLayout; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import javax.swing.BorderFactory; 
import javax.swing.JCheckBoxMenuItem; 
import javax.swing.JLabel; 
import javax.swing.JToolBar; 

public class gui extends JFrame { 

private JLabel statusbar; 

public gui() { 

    initUI(); 
} 

private void initUI() { 

    createMenuBar(); 
    createToolBar(); 

    statusbar = new JLabel("Ready"); 
    statusbar.setBorder 

(BorderFactory.createEtchedBorder()); 
    add(statusbar, BorderLayout.SOUTH); 

    JButton quitButton = new JButton("Quit"); 
    quitButton.setToolTipText("A button component"); 

    quitButton.addActionListener(new ActionListener() 

{ 
     @Override 
     public void actionPerformed(ActionEvent event) 

{ 
      System.exit(0); 
     } 
    }); 

    quitButton.setMnemonic(KeyEvent.VK_B); 

    createLayout(quitButton); 

    setTitle("Typing Tutor"); 
    setSize(360, 250); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 

private void createMenuBar() { 

    JMenuBar menubar = new JMenuBar(); 

    ImageIcon iconNew = new ImageIcon("new.png"); 
    ImageIcon iconOpen = new ImageIcon("open.png"); 
    ImageIcon iconSave = new ImageIcon("save.png"); 
    ImageIcon iconExit = new ImageIcon("exit.png"); 

    JMenu fileMenu = new JMenu("File"); 
    fileMenu.setMnemonic(KeyEvent.VK_F); 

    // Old code 

    JMenu impMenu = new JMenu("Import"); 
    fileMenu.setMnemonic(KeyEvent.VK_I); 

    JMenuItem newsfMi = new JMenuItem("Import newsfeed list..."); 
    JMenuItem bookmMi = new JMenuItem("Import bookmarks..."); 
    JMenuItem mailMi = new JMenuItem("Import mail..."); 

    impMenu.add(newsfMi); 
    impMenu.add(bookmMi); 
    impMenu.add(mailMi); 

    JMenuItem newMi = new JMenuItem("New", iconNew); 
    JMenuItem openMi = new JMenuItem("Open", iconOpen); 
    JMenuItem saveMi = new JMenuItem("Save", iconSave); 

    JMenuItem exitMi = new JMenuItem("Exit", iconExit); 
    exitMi.setMnemonic(KeyEvent.VK_E); 
    exitMi.setToolTipText("Exit application"); 
    exitMi.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) 

    { 
       System.exit(0); 
      } 
     }); 

     // New code 

     JMenu viewMenu = new JMenu("View"); 
     viewMenu.setMnemonic(KeyEvent.VK_V); 

     JCheckBoxMenuItem sbarMi = new JCheckBoxMenuItem 

("Show status bar"); 
     sbarMi.setMnemonic(KeyEvent.VK_S); 
     sbarMi.setDisplayedMnemonicIndex(5); 
     sbarMi.setSelected(true); 

     sbarMi.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 

       if (e.getStateChange() == 

ItemEvent.SELECTED) { 
        statusbar.setVisible(true); 
       } else { 
        statusbar.setVisible(false); 
       } 

      } 

     }); 

     // Newer code 
     JMenu helpMenu = new JMenu("Help"); 


     viewMenu.add(sbarMi); 
     fileMenu.add(newMi); 
     fileMenu.add(openMi); 
     fileMenu.add(saveMi); 
     fileMenu.addSeparator(); 
     fileMenu.add(impMenu); 
     fileMenu.addSeparator(); 
     fileMenu.add(exitMi); 


     menubar.add(fileMenu); 
     menubar.add(viewMenu); 
     menubar.add(Box.createHorizontalGlue()); 
     menubar.add(helpMenu); 

     setJMenuBar(menubar); 
    } 

private void createToolBar() { 

     JToolBar toolbar = new JToolBar(); 

     ImageIcon icon = new ImageIcon("exit.png"); 

     JButton exitButton = new JButton(icon); 
     toolbar.add(exitButton); 

     exitButton.addActionListener(new ActionListener() 

{ 
      @Override 
      public void actionPerformed(ActionEvent event) 

{ 
       System.exit(0); 
      } 
     }); 

     add(toolbar, BorderLayout.NORTH);   
    } 

    private void createLayout(JComponent... arg) { 

     Container pane = getContentPane(); 
     GroupLayout gl = new GroupLayout(pane); 
     pane.setLayout(gl); 

     gl.setAutoCreateContainerGaps(true); 

     gl.setHorizontalGroup(gl.createSequentialGroup() 
       .addComponent(arg[0]) 
     ); 

     gl.setVerticalGroup(gl.createSequentialGroup() 
       .addComponent(arg[0]) 
     ); 

     pack(); 
    } 

    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       gui ex = new gui(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 

我還檢查了計算器和谷歌搜索,但我是我見過的那些要麼Android和/或我能理解。一些建議將不勝感激。謝謝。

回答

1

你已經修改了配置後,創建並添加JToolBar(以GroupLayout - 不要讓我開始對這個選擇)

因此,如果我們改變createLayout所以它創建了一個次要成分,有點像。 ..

private void createLayout(JComponent... arg) { 

    JPanel pane = new JPanel(); 
    //Container pane = getContentPane(); 
    GroupLayout gl = new GroupLayout(pane); 
    pane.setLayout(gl); 

    gl.setAutoCreateContainerGaps(true); 

    gl.setHorizontalGroup(gl.createSequentialGroup() 
        .addComponent(arg[0]) 
    ); 

    gl.setVerticalGroup(gl.createSequentialGroup() 
        .addComponent(arg[0]) 
    ); 

    add(pane); 

    pack(); 
} 

現在會顯示類似...

Frame

因爲您的內容和JToolBar需要住在contentPane之內,所以您需要對您所做的任何更改有所瞭解。這種解決方案使您可以以任何您想要的方式在其上放置「主要」內容的次要窗格,然後將其添加到contentPane,該窗口仍在使用BorderLayout