如何在菜单栏中添加一个JDialog盒子?

问题描述:

我想知道是否有人可以帮我解决我遇到的问题?所以我创建了一个跳棋游戏,我希望在我的窗口顶部添加一个菜单栏,其代码可以在下面看到,但是在其中一个菜单栏选项卡中,我想添加游戏规则,以便以清晰的格式显示规则。我决定使用一个我创建的JDialog盒子,并且可以看到它的代码用于编码盒子的这一部分。我的问题是我如何将它添加到第一部分中的代码中,我标记为1),因为它们是java中的不同类,我希望将JDialog框添加到代码段中,其中声明:“Where Dialog盒子应该去!!“,这是第9行,如果有人可能提供代码来解决这个问题,我会非常感激,我试图在网上寻找帮助,但无法直接从这个问题找到任何东西,谢谢。如何在菜单栏中添加一个JDialog盒子?

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); 

您完全按照“退出”菜单项所做的操作。您将创建一个ActionListener并将ActionListener添加到“Rules”菜单项。

然后在ActionListener代码中创建并显示选项窗格。

您遇到问题的原因是您的应用程序的整个设计都是错误的。你不应该在主(...)方法中编写你的应用程序。主要方法仅用于创建应用程序的实例。我建议你看看How to Use Menus的Swing教程。 MenuLookDemo会给你一个关于如何更好地构建你的代码的想法。

此外,与变量名称保持一致。变量名称不应以大写字符开头。

+0

有没有什么可以给我我应该添加的代码?我一直在努力让自己的头脑清楚,但说实话,我仍然对如何添加它感到困惑,你能否快速写出一两行? 无论如何谢谢你的时间:) – Hawk97 2015-02-12 14:26:21

+0

@ Hawk97,你已经有了你需要添加的代码。您已经为“退出”按钮编写了一个ActionListener。为rules按钮创建另一个ActionListener,并用“JOptionPane.showMessageDialog(...)”语句替换“System.exit()”语句。尝试一下,如果您有问题,请使用您尝试使用的代码更新您的问题。 – camickr 2015-02-12 15:34:49