如何将 JDialog 框添加到菜单栏中?
How to add a JDialog box into a menu bar?
我想知道是否有人可以帮助我解决我遇到的问题?所以我正在创建一个跳棋游戏,我希望在 window 的顶部添加一个菜单栏,其代码如下所示,但是在其中一个菜单栏选项卡中,我想添加游戏规则,以便以清晰的格式显示规则。我决定使用我创建的 JDialog 框,其代码可以在框的这一部分中看到。我的问题是如何将它添加到我标记为 1) 的第一部分中的代码中,因为它们在 java 中是不同的 类,我希望将 JDialog 框添加到代码指出:"Where my Dialog Box should go!!",这是第 9 行,如果有人可以向我提供解决此问题的代码,我将不胜感激,我已尝试在线查找帮助,但无法直接从这个问题,谢谢。
1) public static void main(String[] args) {
JFrame window = new JFrame("Checkers"); // Sets the title at the top of the window as 'Checkers'
JMenuBar bar = new JMenuBar(); // Adds the Menu Bar
JMenu fileMenu = new JMenu("File"); // Adds a File Tab to the Menu Bar
JMenu HelpMenu = new JMenu("Help"); // Adds a Help Tab to the Menu Bar
JMenuItem Exit = new JMenuItem("Exit"); // Adds the Exit sub-tab as an Item of the JMenu
JMenuItem MainMenu = new JMenuItem("Main Menu"); // Adds the Main Menu sub-tab as an Item of the JMenu
JMenu Rules = new JMenu("Rules of Checkers"); // Adds the Rules of Checkers sub-tab as an Item of the JMenu
JMenuItem RulesText = new JMenuItem("Where my Dialog Box should go!");
Rules.add(RulesText); // Adds the Rules Text Item into the Rules of Checkers tab.
HelpMenu.add(Rules); // Adds the Rules of Checkers tab into the Help tab
bar.add(HelpMenu); // Adds the Help tab to the Menu Bar
fileMenu.add(MainMenu);// Adds the Main Menu sub-tab into the File tab
fileMenu.addSeparator(); // Adds a line in between the Main Menu sub-tab and the Exit sub-tab
fileMenu.add(Exit); // Adds the Exit sub-tab into the Menu tab
bar.add(fileMenu); // Adds the Menu tab to the Menu bar
bar.add(HelpMenu); // Adds the Help tab to the Menu Bar
window.setJMenuBar(bar); // Adds the Menu Bar to the application window
Exit.addActionListener(new ActionListener() // Adds an ActionListener to the Exit Sub-tab
{
public void actionPerformed(ActionEvent ev)
{
System.exit(0); // This means that when the Exit sub-tab is clicked, it will exit the application
}
});
我的 JDialog 框的代码,如果对您有帮助,谢谢。
2) static JFrame frame;
public static void main(String args[])
{
JOptionPane.showMessageDialog(frame,
"- Pieces must always move diagonally\n" +
"- Single pieces are limited to forward moves\n" +
"- Kings may move both forward and backward\n" +
"- When a piece is captured, it is removed from the board\n" +
"- If a player is able to make a capture, there is no option, the jump must be made\n" +
"- When a piece reaches the opponents end of the board, it is crowned and becomes a King",
"Rules for Checkers",
JOptionPane.PLAIN_MESSAGE);
您所做的与您对 "Exit" 菜单项所做的完全相同。那就是你创建一个 ActionListener 并将 ActionListener 添加到 "Rules" 菜单项。
然后在 ActionListener 代码中创建并显示选项窗格。
你有问题的原因是你的应用程序的整个设计是错误的。你永远不应该在 main(...) 方法中编写你的应用程序。主要方法仅用于创建应用程序的实例。我建议您查看 How to Use Menus 上的 Swing 教程。 MenuLookDemo
会让您了解如何更好地构建代码。
另外,与变量名保持一致。变量名称不应以大写字符开头。
我想知道是否有人可以帮助我解决我遇到的问题?所以我正在创建一个跳棋游戏,我希望在 window 的顶部添加一个菜单栏,其代码如下所示,但是在其中一个菜单栏选项卡中,我想添加游戏规则,以便以清晰的格式显示规则。我决定使用我创建的 JDialog 框,其代码可以在框的这一部分中看到。我的问题是如何将它添加到我标记为 1) 的第一部分中的代码中,因为它们在 java 中是不同的 类,我希望将 JDialog 框添加到代码指出:"Where my Dialog Box should go!!",这是第 9 行,如果有人可以向我提供解决此问题的代码,我将不胜感激,我已尝试在线查找帮助,但无法直接从这个问题,谢谢。
1) public static void main(String[] args) {
JFrame window = new JFrame("Checkers"); // Sets the title at the top of the window as 'Checkers'
JMenuBar bar = new JMenuBar(); // Adds the Menu Bar
JMenu fileMenu = new JMenu("File"); // Adds a File Tab to the Menu Bar
JMenu HelpMenu = new JMenu("Help"); // Adds a Help Tab to the Menu Bar
JMenuItem Exit = new JMenuItem("Exit"); // Adds the Exit sub-tab as an Item of the JMenu
JMenuItem MainMenu = new JMenuItem("Main Menu"); // Adds the Main Menu sub-tab as an Item of the JMenu
JMenu Rules = new JMenu("Rules of Checkers"); // Adds the Rules of Checkers sub-tab as an Item of the JMenu
JMenuItem RulesText = new JMenuItem("Where my Dialog Box should go!");
Rules.add(RulesText); // Adds the Rules Text Item into the Rules of Checkers tab.
HelpMenu.add(Rules); // Adds the Rules of Checkers tab into the Help tab
bar.add(HelpMenu); // Adds the Help tab to the Menu Bar
fileMenu.add(MainMenu);// Adds the Main Menu sub-tab into the File tab
fileMenu.addSeparator(); // Adds a line in between the Main Menu sub-tab and the Exit sub-tab
fileMenu.add(Exit); // Adds the Exit sub-tab into the Menu tab
bar.add(fileMenu); // Adds the Menu tab to the Menu bar
bar.add(HelpMenu); // Adds the Help tab to the Menu Bar
window.setJMenuBar(bar); // Adds the Menu Bar to the application window
Exit.addActionListener(new ActionListener() // Adds an ActionListener to the Exit Sub-tab
{
public void actionPerformed(ActionEvent ev)
{
System.exit(0); // This means that when the Exit sub-tab is clicked, it will exit the application
}
});
我的 JDialog 框的代码,如果对您有帮助,谢谢。
2) static JFrame frame;
public static void main(String args[])
{
JOptionPane.showMessageDialog(frame,
"- Pieces must always move diagonally\n" +
"- Single pieces are limited to forward moves\n" +
"- Kings may move both forward and backward\n" +
"- When a piece is captured, it is removed from the board\n" +
"- If a player is able to make a capture, there is no option, the jump must be made\n" +
"- When a piece reaches the opponents end of the board, it is crowned and becomes a King",
"Rules for Checkers",
JOptionPane.PLAIN_MESSAGE);
您所做的与您对 "Exit" 菜单项所做的完全相同。那就是你创建一个 ActionListener 并将 ActionListener 添加到 "Rules" 菜单项。
然后在 ActionListener 代码中创建并显示选项窗格。
你有问题的原因是你的应用程序的整个设计是错误的。你永远不应该在 main(...) 方法中编写你的应用程序。主要方法仅用于创建应用程序的实例。我建议您查看 How to Use Menus 上的 Swing 教程。 MenuLookDemo
会让您了解如何更好地构建代码。
另外,与变量名保持一致。变量名称不应以大写字符开头。