简体   繁体   English

Java JmenuItem在单击时不起作用

[英]Java JmenuItem do something when clicked not working

I am currently trying to make a GUI with a menu that has 2 options you can select from. 我目前正在尝试制作一个带有2个选项的菜单的GUI。 One being "Default Settings" and one being "Custom Settings." 一种是“默认设置”,另一种是“自定义设置”。 When you click on either one, it will take you to the new jPanel that will display the proper windows, text boxes, etc for that panel. 当您单击任意一个时,它将带您到新的jPanel,它将显示该面板的正确窗口,文本框等。 However, I cannot seem to get the mouseClicked action to actually switch between the panels. 但是,我似乎无法让mouseClicked动作真正在面板之间切换。 As a test, I have a simple jLabel on each panel that says "Default" for the default panel and "custom" for the custom panel, and each menu item, when clicked respectively, should switch between them. 作为测试,我在每个面板上都有一个简单的jLabel,它在默认面板上显示“ Default”,在自定义面板上显示“ custom”,并且分别单击每个菜单项时,都应该在它们之间切换。 Here is my current code: 这是我当前的代码:

frmLegitServerAdder = new JFrame();
frmLegitServerAdder.setTitle("Legit Server Adder 5 Million");
frmLegitServerAdder.setBounds(100, 100, 546, 468);
frmLegitServerAdder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();
frmLegitServerAdder.setJMenuBar(menuBar);

JMenu mnNewMenu = new JMenu("Settings");
menuBar.add(mnNewMenu);

JMenuItem menuItemDefaultSettings = new JMenuItem("Default Settings");
mnNewMenu.add(menuItemDefaultSettings);

JMenuItem menuItemCustomSettings = new JMenuItem("Custom Logon Settings");
mnNewMenu.add(menuItemCustomSettings);


frmLegitServerAdder.getContentPane().setLayout(new CardLayout(0, 0));

final JPanel defaultSettingsPanel = new JPanel();
frmLegitServerAdder.getContentPane().add(defaultSettingsPanel, "name_416522810155567");
defaultSettingsPanel.setLayout(null);

JLabel lblDefaultArea = new JLabel("Default Area");
lblDefaultArea.setBounds(217, 11, 90, 14);
defaultSettingsPanel.add(lblDefaultArea);

final JPanel customSettingsPanel = new JPanel();
frmLegitServerAdder.getContentPane().add(customSettingsPanel, "name_416549691176064");
customSettingsPanel.setLayout(null);

JLabel lblCustomArea = new JLabel("Custom Area");
lblCustomArea.setBounds(235, 21, 46, 14);
customSettingsPanel.add(lblCustomArea);

menuItemDefaultSettings.addMouseListener(new MouseAdapter()
{
    @Override
    public void mouseClicked(MouseEvent e)
    {
        defaultSettingsPanel.setVisible(true);
        customSettingsPanel.setVisible(false);

    }
});

menuItemCustomSettings.addMouseListener(new MouseAdapter()
{
    @Override
    public void mouseClicked(MouseEvent e)
    {
        defaultSettingsPanel.setVisible(false);
        customSettingsPanel.setVisible(true);
    }
});

The code runs and the GUI displays just fine, but nothing actually happens when I click on either menu items, as it should. 代码运行并且GUI正常显示,但是当我按任意一个菜单项单击时,实际上什么也没有发生。 Any ideas? 有任何想法吗?

  1. You should NOT be using a MouseListener. 您不应该使用MouseListener。 Instead you should be adding an ActionListener to the menu item. 相反,您应该将ActionListener添加到菜单项。 Read the section from the Swing tutorial on How to Use Menus for more information. 阅读Swing教程中有关如何使用菜单的部分, 获取更多信息。

  2. You should be using a CardLayout when you want to swap components. 要交换组件时,应该使用CardLayout。 See How to Use Card Layout from the same tutorial. 请参阅同一教程中的“ 如何使用卡片布局 ”。

You need ActionListener 您需要ActionListener

menuItemDefaultSettings.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        defaultSettingsPanel.setVisible(true);
        customSettingsPanel.setVisible(false);

    }
});

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM