2014-01-06 97 views
-2

我在Java中做了一個簡單的GUI,其中包括一個菜單,上下文菜單,按鈕,工具欄,複選框和狀態欄。但是,按鈕和工具欄不顯示。這是代碼:對象不顯示在GUI中 - Java

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.BorderFactory; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JCheckBoxMenuItem; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JPopupMenu; 
import javax.swing.JToolBar; 
import javax.swing.KeyStroke; 
import javax.swing.SwingUtilities; 
import javax.swing.border.EtchedBorder; 

public class test extends JFrame { 

private JLabel statusbar; 
private JPopupMenu menu; 
private Toolkit toolkit; 

public test() { 

    initUI(); 
} 

private void initUI() { 

    JPanel panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
    getContentPane().add(panel); 
    toolkit = this.getToolkit(); 
    menu = new JPopupMenu(); 
    JToolBar toolbar1 = new JToolBar(); 
    JToolBar toolbar2 = new JToolBar(); 

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

    JButton newb = new JButton(iconNew); // Declaring Buttons for Toolbar 
    JButton openb = new JButton(iconOpen); 
    JButton saveb = new JButton(iconSave); 
    JButton exitb = new JButton(iconExit); 

    toolbar1.add(newb); // Adding Buttons to Toolbar1 
    toolbar1.add(openb); 
    toolbar1.add(saveb); 
    toolbar1.setAlignmentX(0); // Alignment of Toolbar1 

    toolbar2.add(exitb); // Adding Buttons to Toolbar2 
    toolbar2.setAlignmentX(0); // Alignment of Toolbar2 

    exitb.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 

    }); 

    panel.add(toolbar1); 
    panel.add(toolbar2); 

    add(panel, BorderLayout.NORTH); 

    JMenuBar menubar = new JMenuBar(); // JMenuBar 
    JMenu file = new JMenu("File"); 
    JMenu view = new JMenu("View"); 
    file.setMnemonic(KeyEvent.VK_F); 

    JMenuItem eMenuItem = new JMenuItem("Exit", iconExit); // Exit Menu Item 
    eMenuItem.setMnemonic(KeyEvent.VK_E); 
    eMenuItem.setToolTipText("Exit application"); 
    eMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 
      ActionEvent.CTRL_MASK)); 
    eMenuItem.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 
    JMenuItem nMenuItem = new JMenuItem("New", iconNew); // New Menu Item 
    nMenuItem.setToolTipText("New File"); 
    JMenuItem oMenuItem = new JMenuItem("Open", iconOpen); 
    oMenuItem.setToolTipText("Open File"); 
    JMenuItem sMenuItem = new JMenuItem("Save", iconSave); 
    sMenuItem.setToolTipText("Save File"); 

    JMenu imp = new JMenu("Import..."); // Import Sub-Menu Item 
    imp.setMnemonic(KeyEvent.VK_M); 
    imp.setToolTipText("Import Data"); 
    JMenuItem newsf = new JMenuItem("Import newsfeed list..."); 
    JMenuItem bookm = new JMenuItem("Import bookmarks..."); 
    JMenuItem mail = new JMenuItem("Import mail..."); 

    imp.add(newsf); // Adding Sub-menu Items to Menu 
    imp.add(bookm); 
    imp.add(mail); 

    file.add(nMenuItem); // Adding Menu Items to "File" Menu-List 
    file.add(oMenuItem); 
    file.add(sMenuItem); 
    file.addSeparator(); 
    file.add(imp); 
    file.addSeparator(); 
    file.add(eMenuItem); 

    JButton quitButton = new JButton("Quit"); // Quit Button 
    quitButton.setBounds(870, 380, 80, 30); 
    quitButton.setToolTipText("Press me"); 
    quitButton.setBackground(new Color(66, 89, 205)); 
    quitButton.setForeground(new Color(255, 255, 255)); 
    quitButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 

    JButton nextButton = new JButton("Next >"); // Next Button 
    nextButton.setBounds(770, 380, 80, 30); 
    nextButton.setToolTipText("Next..."); 
    nextButton.setBackground(new Color(66, 89, 205)); 

    nextButton.setForeground(new Color(255, 255, 255)); 

    nextButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 

    panel.add(quitButton); // Adding Buttons to panel 
    panel.add(nextButton); 

    JCheckBoxMenuItem sbar = new JCheckBoxMenuItem("Show StatuBar"); // Creating 
                     // Status-bar 
                     // Check-box 
    sbar.setState(true); 
    sbar.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      if (statusbar.isVisible()) { 
       statusbar.setVisible(false); 
      } else { 
       statusbar.setVisible(true); 
      } 
     } 

    }); 

    view.add(sbar); // Adding Check-box to the View Menu-List 

    JMenuItem menuItemBeep = new JMenuItem("Beep"); // Beep option in Pop-Up 
    menuItemBeep.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      toolkit.beep(); 
     } 
    }); 

    menubar.add(file); // Adding Menu-Lists to Menu Bar 
    menubar.add(view); 

    menu.add(menuItemBeep); // Adding Beep option in Pop-Up 

    JMenuItem menuItemClose = new JMenuItem("Close"); // Close option in 
                 // Pop-Up 
    menuItemClose.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
    }); 

    menu.add(menuItemClose); // Adding Close option in Pop-Up 

    this.addMouseListener(new MouseAdapter() { // Mouse Listener for Pop-Up 
     @Override 
     public void mouseReleased(MouseEvent e) { 
      if (e.getButton() == MouseEvent.BUTTON3) { 
       menu.show(e.getComponent(), e.getX(), e.getY()); 
      } 
     } 
    }); 

    setJMenuBar(menubar); // Menu-bar??? 

    statusbar = new JLabel(" Statusbar"); // Creating Status Bar 
    statusbar.setBorder(BorderFactory 
      .createEtchedBorder(EtchedBorder.RAISED)); 
    add(statusbar, BorderLayout.SOUTH); 

    panel.setLayout(null); // Panel Design and additional Arguments 
    panel.setBackground(new Color(18, 33, 110)); 

    this.setTitle("My First GUI in Java"); 
    this.setSize(1000, 500); 
    this.setLocationRelativeTo(null); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 

public static void main(String[] args) { 

    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      test ex = new test(); 
      ex.setVisible(true); 
     } 
    }); 
    } 
} 

我很新這個,所以任何意見表示讚賞。

謝謝!

+0

您可能不應該擴展JFrame,並且您似乎要添加面板兩次。 –

+0

你在哪裏調用'implements ActionListener'? – Tdorno

+2

你有一堆組件。哪些不顯示?我建議將問題簡化爲[SSCCE](http://sscce.org),並刪除所有額外的代碼。您似乎也將同一個面板添加到內容窗格和JFrame(它只是將它傳遞給內容窗格),這表明您可能希望通過精細梳齒來瀏覽代碼。 –

回答

4

「但是,按鈕和工具欄不顯示。」

  1. 通過您init()方法看,我只看到你增加兩個組件的框架。

    add(panel, BorderLayout.NORTH); 
    add(statusbar, BorderLayout.SOUTH); 
    
  2. 然後與上述相同的面板,設置佈局爲空,當你第一次已經規定它是一個BoxLayout

    panel.setLayout(null); 
    
  3. 我的評論上面的代碼了,有的組件出現。

3

您在開始設置面板的佈局曾經兩次:

panel.setLayout(新的BoxLayout(面板,BoxLayout.Y_AXIS));

和一次以後:

panel.setLayout(空);

你應該嘗試擺脫第二個。這應該可以解決問題