JMenuBar不显示正确的菜单选项

时间:2014-02-24 12:54:18

标签: java swing user-interface netbeans jmenu

- 我正在尝试使用包含“打开”,“运行”,“帮助”等菜单的菜单栏显示GUI,但我在GUI中看到的只是菜单“文件”

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class test11 extends JFrame {   
   public test11(String title, int width, int height) {

        //setting up frame
        setTitle(title);
        setSize(width, height);        
        setLocationRelativeTo(null);
        setMinimumSize(new Dimension(700, 500));
        setVisible(true);
        setLayout(new MigLayout("debug, fillx, gap unrel rel","[grow, fill][fill]","[fill][fill]"));//no idea

        //Menubar       
        JMenuBar m = new JMenuBar();
        m.setBackground(new Color(192,192,192));

        JMenu op = new JMenu("Open");
        op.add(new JMenuItem("Catalogue"));
        op.add(new JMenuItem("Read a Paper!"));
        JMenuItem find = new JMenuItem("Find...");
        find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK));  
        op.add(find);
        JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_E);
        exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        op.add(exit);

        JMenu run = new JMenu("Run");
        run.add(new JMenuItem("Search My System"));  
        JMenuItem synchr = new JMenuItem("Start Synchronizer", 'S');   
        synchr.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK));  
        run.add(synchr);

        JMenu help = new JMenu("Help");     
        JMenuItem h = new JMenuItem("Help Contents", 'H');   
        h.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_MASK));  
        help.add(h);
        help.add(new JMenuItem("Keyboard Shortcut Card"));
        help.add(new JMenuItem("About Application"));        

        m.add(op);
        m.add(run);
        m.add(help);        
        setJMenuBar(m);  

        //Toolbar
        JToolBar toolBar = new JToolBar("Draggable");
        JButton subject = new JButton("Subject");
        toolBar.add(subject);
        add(toolBar, "span, height 20:35:50, wrap");

        JPanel table = new JPanel();
        JPanel sideBar = new JPanel();
        add(table, "width 400:600:, dock center, growy");
        add(sideBar, "width 250:300:350, dock west, growy");    
    }

    public static void main(String args[]){
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new test1("ResearchSoft",1300,700).setVisible(true);
            }
        });
    }
}

- 甚至将工具栏按钮更改为“主题”,但显示“选择”

有人能找出为什么会发生这种奇怪的行为吗?

1 个答案:

答案 0 :(得分:2)

您已创建

的对象
 new test1("ResearchSoft",1300,700).setVisible(true);

但你想创建

的对象
new test11("ResearchSoft",1300,700).setVisible(true);