Java JFrame上JMenuBar中的空JMenuItems
我在使用JMenuBar创建JFrame时遇到问题。 我在这里有一个MenuBarBuilder类:Java JFrame上JMenuBar中的空JMenuItems,java,swing,jframe,jmenuitem,jmenubar,Java,Swing,Jframe,Jmenuitem,Jmenubar,我在使用JMenuBar创建JFrame时遇到问题。 我在这里有一个MenuBarBuilder类: public class MenuBuilder extends JMenuBar { private Model model; public MenuBuilder(Model model) { this.model = model; buildMenuBar(); } public void buildMenuBar() { JMenu m
public class MenuBuilder extends JMenuBar
{
private Model model;
public MenuBuilder(Model model)
{
this.model = model;
buildMenuBar();
}
public void buildMenuBar()
{
JMenu menuFile = new JMenu("File");
JMenu menuEdit = new JMenu("Edit");
JMenu menuHelp = new JMenu("Help");
menuHelp.setMnemonic('H');
menuFile.setMnemonic('F');
menuEdit.setMnemonic('E');
JMenuItem menuItemExit = new JMenuItem("Exit");
menuItemExit.setAccelerator(model.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
menuItemExit.setAction(new ActionExit(model));
menuFile.add(menuItemExit);
add(menuFile);
add(menuEdit);
add(menuHelp);
}
}
JFrame是在另一个类中创建的:
public MainGUI(boolean loadConfig, String loadConfigDir, Model model)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
{
GlobalVariables.LOGGING_logger.error("Something went wrong while getting the Look and Feel of current Windows Version for Userinterface", e);
}
try
{
this.model = model;
frameMain = new JFrame("MainFrame");
frameMain.setJMenuBar(new MenuBuilder(model));
frameMain.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frameMain.addWindowListener(this);
frameMain.setMinimumSize(new Dimension(500, 500));
frameMain.pack();
frameMain.setSize(800, 800);
frameMain.setLocationRelativeTo(null);
frameMain.setVisible(true);
}
catch (Exception e)
{
GlobalVariables.LOGGING_logger.error("Error while seeting up the main GUI.", e);
MessagesToUser.errorMessageBothFilesIssue(true);
}
}
显示JFrame
后,所有菜单项都是空的,但都存在,并且功能(ActionExit
)也正常工作。使用以下代码menuFile.add(newjmenuItem(“Exit”))设置新的JMenuItem
代码>按预期工作,JFrame具有正确的JMenuBar
。为什么会这样
编辑:
下面是刚刚退出程序的ActionExit类:
public class ActionExit extends AbstractAction
{
private Model model;
public ActionExit(Model model)
{
this.model = model;
}
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
JMenuItem
(事实上,AbstractButton
的所有子项)都从Action.NAME
属性派生其显示文本
试试更像
public class ActionExit extends AbstractAction
{
private Model model;
public ActionExit(Model model)
{
this.model = model;
putValue(NAME, "Exit");
}
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
另外,Action
类还定义了助记符
和加速器
值
查看API以了解更多详细信息检查这一个,我可爱的小代码,这个程序在JFrame中只有一个JMenuBar,在JMenuBar中只有一个JMenuItem,这可能会对您有所帮助。当您单击菜单栏中的菜单项时,退出该框架。为了更好地理解,请运行此程序
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Class5 extends JFrame
{
static JMenuBar exampleMenuBar = new JMenuBar();
static JMenu fileMenu = new JMenu("Help");
static JMenuItem Exit = new JMenuItem("Exit");
public static void main(String args[])
{
// construct frame
new Class5().show();
}
public Class5()
{
// frame constructor
setTitle("Menu Example");
setSize(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// build menu
setJMenuBar(exampleMenuBar);
exampleMenuBar.add(fileMenu);
fileMenu.add(Exit);
Exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
}
您实际上没有向菜单添加任何内容(然后退出)?这只是一个示例。所有菜单都有三个以上的条目。那么,您是如何填充菜单的呢?与“menuItemExit”相同,然后将其添加到菜单中,然后再将其添加到JMenuBar中。JMenuItems都存在,函数正常工作,但项上没有文本。我们可以看到ActionExit
或任何其他Action
类的代码吗?