没有在GUI中显示的对象 - Java

时间:2014-01-06 18:52:03

标签: java swing layout awt

我在Java中创建了一个简单的GUI,其中包括菜单,上下文菜单,按钮,工具栏,复选框和状态栏。但是,按钮和工具栏不显示。这是代码:

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);
        }
    });
  }
}

我对此很新,所以任何意见都表示赞赏。

谢谢!

2 个答案:

答案 0 :(得分:4)

  

“但是,按钮和工具栏未显示。”

  1. 通过init()方法,我只看到您在框架中添加了两个组件。

    add(panel, BorderLayout.NORTH);
    add(statusbar, BorderLayout.SOUTH);
    
  2. 然后使用与上面相同的面板,当您首次将布局指定为BoxLayout

    时,将布局设置为null
    panel.setLayout(null); 
    
  3. 我评论了上面的代码,并出现了一些组件。

答案 1 :(得分:3)

您在开始时将面板布局设置两次:

  

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

以后一次:

  

panel.setLayout(空);

你应该尝试摆脱第二个。这应该解决问题