【问题标题】:JMenuBar not displaying right menu options [closed]JMenuBar 不显示正确的菜单选项 [关闭]
【发布时间】:2014-02-24 12:54:18
【问题描述】:

-我正在尝试使用包含“打开”、“运行”、“帮助”等菜单的菜单栏显示 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);
            }
        });
    }
}

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

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

【问题讨论】:

  • 没什么奇怪的。您正在 test11 中进行更改,但您正在创建 test1 的对象。因此,您没有看到所做的更改。

标签: java swing user-interface netbeans jmenu


【解决方案1】:

你已经创建了

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

但是你想创建的对象

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

【讨论】:

    猜你喜欢
    • 2023-01-15
    • 2014-02-02
    • 2023-03-11
    • 2013-03-08
    • 1970-01-01
    • 2014-04-07
    • 2014-11-29
    • 2014-02-08
    • 2018-08-19
    相关资源
    最近更新 更多