Java 在两个不同的JFrames中打开的同一菜单仅适用于最后一个

Java 在两个不同的JFrames中打开的同一菜单仅适用于最后一个,java,swing,jmenuitem,jmenubar,Java,Swing,Jmenuitem,Jmenubar,我正在尝试编写一个简单的绘图应用程序,其中用户可以使用绘图面板打开新的JFrames(在新线程中),并且每个框架的顶部都有一个JMenuBar,但是,只有最后一个打开的框架有一个功能菜单栏,所有剩余的(打开的)框架都显示菜单,但菜单不起作用(当我点击时,什么也没有发生)。有人知道如何解决这个问题吗 我简化了代码,只留下了关于JMenuBar的部分 该代码由以下类别组成: Main.java package sample; public class Main { Main() {

我正在尝试编写一个简单的绘图应用程序,其中用户可以使用绘图面板打开新的
JFrames
(在新线程中),并且每个框架的顶部都有一个
JMenuBar
,但是,只有最后一个打开的框架有一个功能菜单栏,所有剩余的(打开的)框架都显示菜单,但菜单不起作用(当我点击时,什么也没有发生)。有人知道如何解决这个问题吗

我简化了代码,只留下了关于
JMenuBar
的部分

该代码由以下类别组成:

Main.java

package sample;

public class Main {

    Main() {

        MainFrameThread.getMainFrameThread().run();

    }//end of Main()

    public static void main(String[] args) {
        new Main();
    }

}//end of Main class
TopMenu.java

package sample;

import javax.swing.*;

public class TopMenu extends JMenuBar {

    private JMenu menu_File;
    private static JMenuItem menu_New;

    public static JMenuItem getMenu_New() {
        return menu_New;
    }

    public TopMenu() {

        menu_File = new JMenu("File");
        menu_New = new JMenuItem("New");
        this.add(menu_File);
        menu_File.add(menu_New);

    }//end of TopMenu()

}//end of TopMenu extends JMenuBar
MainFrameThread.java

package sample;

public class MainFrameThread extends Thread {

    private static MainFrameThread mainFrameThread = new MainFrameThread();

    public static MainFrameThread getMainFrameThread() {
        return mainFrameThread;
    }

    public MainFrameThread() {}

    @Override
    public void run() {

        MainFrame mainFrame = new MainFrame();

    }//end of public void run()

}//end of public class FrameSizeDialogThread
ActionController.java

package sample;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ActionController {

    private static ActionController actionController = new ActionController();
    private ListenForMenu listenForMenu = new ListenForMenu();

    public static ActionController getActionController() {
        return actionController;
    }

    public ActionController() {}

    public void clickOnMenu(TopMenu topMenu) {
        TopMenu.getMenu_New().addActionListener(listenForMenu);
    }

    //listener for menu
    public class ListenForMenu implements ActionListener {
        public void actionPerformed(ActionEvent ev) {

            if(ev.getSource() == TopMenu.getMenu_New()) {
                MainFrame newMainFrame = new MainFrame();
            }//end of if(ev.getSource() == TopMenu.getMenu_New())

        }//end of public void actionPerformed(ActionEvent ev)
    }//end of public class ListenForMenu

}//end of ActionController class
和MainFrame.java

package sample;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {

    public MainFrame() {

        JFrame frame = new JFrame("Paint Application");

        //creating menu
        TopMenu topMenu = new TopMenu();
        ActionController.getActionController().clickOnMenu(topMenu);
        frame.setJMenuBar(topMenu);

        //frame properties
        frame.setSize(800, 600);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }//end of public MainFrame()

}//end of public class MainFrame
我被卡住了,不管我在哪里初始化MainFrame.java,什么都不起作用。有人看到错误了吗

但是,只有最后一个打开的框架有功能菜单栏

无法共享Swing组件。Swing组件只能有一个父窗口。因此,对于每个子窗口,您都需要创建一个新的
JMenuBar
JMenu
JMenuItem

但是,
JMenuItem
使用的
操作
可以共享

private static JMenuItem menu_New;

public static JMenuItem getMenu_New() {
    return menu_New;
}
与菜单相关的变量或方法都不应是静态的。同样,您需要为每个变量或方法创建一个唯一的实例

但是,只有最后一个打开的框架具有功能菜单栏

无法共享Swing组件。Swing组件只能有一个父窗口。因此,对于每个子窗口,您都需要创建一个新的
JMenuBar
JMenu
JMenuItem

但是,
JMenuItem
使用的
操作
可以共享

private static JMenuItem menu_New;

public static JMenuItem getMenu_New() {
    return menu_New;
}

与菜单相关的任何变量或方法都不应是静态的。同样,您需要为每个变量或方法创建一个唯一的实例。

一个组件只能出现一次。“用户将能够打开新的
JFrame
窗口(在新线程中)”1)请参见2)所有Swing组件都应在(单个)上创建和更新事件调度线程。安德鲁说的。您将遇到并发问题。此外,您的特殊问题可能与框架的焦点有关。感谢您的解释以及与我分享该帖子,它为我提供了许多关于如何重新设计我的应用程序以及未来其他选择的想法。非常感谢。一个组件只能出现一次。“用户将能够打开新的
JFrame
窗口(在新线程中)”1)参见2)所有Swing组件都应该在(单个)事件调度线程上创建和更新。您将遇到并发问题。此外,您的特殊问题可能与框架的焦点有关。感谢您的解释以及与我分享该帖子,它为我提供了许多关于如何重新设计我的应用程序以及未来其他选择的想法。非常感谢。谢谢你的解释!我对Java还比较陌生,所以像这样的每一条评论都让我更容易学习!非常感谢。谢谢你的解释!我对Java还比较陌生,所以像这样的每一条评论都让我更容易学习!非常感谢。