gpt4 book ai didi

java - 在两个不同的 JFrame 中打开的相同菜单仅适用于最后一个

转载 作者:行者123 更新时间:2023-12-02 01:31:44 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的 Paint 应用程序,其中用户将能够使用绘图面板打开新的 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,都不起作用。有人看到错误吗???

最佳答案

however, only the last open frame has a functional menu bar

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

但是,JMenuItem 使用的Action 可以共享。

private static JMenuItem menu_New;

public static JMenuItem getMenu_New() {
return menu_New;
}

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

关于java - 在两个不同的 JFrame 中打开的相同菜单仅适用于最后一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com