[英]Same menu opened in two different JFrames works only on the last one
I'm trying to write a simple Paint application in which the user will be able to open new JFrames
(in new Threads) with Drawing Panels, and each of those frames will have a JMenuBar
on top, however, only the last open frame has a functional menu bar, all the remaining (open) frames show menu but the menu doesn't work (nothing happens when I click). 我正在尝试编写一个简单的Paint应用程序,在该应用程序中,用户将能够使用绘图面板打开新的
JFrames
(在新线程中),并且每个框架的顶部都有一个JMenuBar
,但是,只有最后一个打开的框架具有功能菜单栏,其余所有(打开的)框架均显示菜单,但菜单不起作用(单击后无任何反应)。 Does anyone know how to fix this? 有谁知道如何解决这一问题?
I've simplified the code to leave only the sections regarding JMenuBar
. 我简化了代码,仅保留有关
JMenuBar
的部分。
The code consists of the following classes: 该代码包含以下类:
Main.java 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 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 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 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
and MainFrame.java 和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
I'm stuck, nothing works, regardless where I initialise the MainFrame.java. 我被困住了,不管我在哪里初始化MainFrame.java,什么都行不通。 Does anyone see the mistake???
有人看到错误了吗???
however, only the last open frame has a functional menu bar
但是,只有最后一个打开的框架具有功能菜单栏
Swing components can't be shared. Swing组件无法共享。 A Swing component can only have a single parent.
Swing组件只能有一个父级。 So for every child window you will need to create a new
JMenuBar
and JMenu
and JMenuItem
. 因此,对于每个子窗口,您都需要创建一个新的
JMenuBar
, JMenu
和JMenuItem
。
However, the Action
used by the JMenuItem
can be shared. 但是,可以共享
JMenuItem
使用的Action
。
private static JMenuItem menu_New;
public static JMenuItem getMenu_New() {
return menu_New;
}
None of the variable or methods related to the menus should be static. 与菜单相关的变量或方法都不应该是静态的。 Again, you need to create a unique instance of each.
同样,您需要创建每个实例的唯一实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.