java - 单击菜单按钮时尝试显示其中包含文本的框架

标签 java swing jframe actionlistener jmenu

当用户单击标题为“规则”的菜单按钮时,我试图在框架中显示一段文本。当我单击规则时,不知何故没有任何显示。

所以我的问题是,为什么点击规则后没有任何显示。 我认为将 ActionListener 添加到规则将意味着当单击规则时,它将弹出一个标题为“规则”的新框架,并且在该框架内会有一条消息说“在此处编写规则...”。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class CheckerProgram{
    public static int rows = 8;
    public static int columns = 8;
    public static Color color1 = Color.BLACK;
    public static Color color2 = Color.RED;

    public static void main( String[] args ){
        JFrame window = new JFrame("Checkers");
        window.setSize( 800, 800 );
        window.setTitle("Checkers!");
        Container pane = window.getContentPane();
        pane.setLayout(new GridLayout(rows, columns));
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        Color temp;
        for( int i=0; i<rows; i++ ){
            if( i%2 == 0 ){
                temp = color1;
            }
            else{
                temp = color2;
            }

            for( int j=0; j<columns; j++ ){
                JPanel panel = new JPanel();
                panel.setBackground(temp);

                if( temp.equals(color1)){
                    temp = color2;
                }
                else{
                    temp = color1;
                }

                pane.add(panel);
            }
        }
        window.setVisible(true);

        JMenuBar menuBar = new JMenuBar();
        window.setJMenuBar(menuBar);

        // Display options ex, new game, surrender, close game.
        JMenu options = new JMenu("Options");
        menuBar.add(options);
        JMenuItem newGame = new JMenuItem("New Game");
        JMenuItem surrender = new JMenuItem("Surrender");
        JMenuItem closeGame = new JMenuItem("Close Game");
        options.add(newGame);
        options.add(surrender);
        options.add(closeGame);

        // Action button for Close Game under Options menu
        class closeGameAction implements ActionListener{
            public void actionPerformed( ActionEvent e ){
                System.exit(0);
            }
        }
        closeGame.addActionListener( new closeGameAction() );

        // Display scores for both players
        JMenu scores = new JMenu("Scores");
        menuBar.add(scores);

        // Display rules for game
        JMenu rules = new JMenu("Rules");
        menuBar.add(rules);
        rules.addActionListener( new rulesAction() );


    } // End of main


    static class rulesAction implements ActionListener{
            public void actionPerformed( ActionEvent e ){
                JFrame displayRules = new JFrame("Rules");
                displayRules.setVisible(true);
                displayRules.setSize( 300, 300 );
                JLabel label = new JLabel("Write rules here...");
                JPanel panel = new JPanel();
                panel.add(label);
                displayRules.add(panel);

            }
     }
}

这是主框架的屏幕截图。所以上面有一个名为“规则”的菜单选项,当我点击它时没有任何反应。

enter image description here

最佳答案

rules 是一个 JMenu:

JMenu rules = new JMenu("Rules");

我想你的意思是它是一个 JMenuItem。

您还应该考虑使用 JDialog 来显示规则。参见 "The Use of Multiple JFrames, Good/Bad Practice?"

一般来说,您还应该在创建它们之后将它们设置为可见。当我在 OSX 上运行它时,菜单栏没有出现,它(松散地)是因为您在添加栏之前将 JFrame 设置为可见。

关于java - 单击菜单按钮时尝试显示其中包含文本的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20308343/

相关文章:

java - 当我不能使用 "this"作为上下文时如何使用 Toast

java - 未修饰的 JFrame 显示不需要的图标

Java : JPanel GridBagLayout issue

java - Apache Tomcat 日志记录默认权限

Java读取文本文件以获取特定格式的值

java - Java 操作系统的不同图标大小

java - JTable insertRow ArrayIndexOutOfBoundsException

java - 我如何开始优化我的 Java 代码? - CPU 处于 100%

java - Java 日期验证的正则表达式

java - 从外部重写 JPanel 的 getPreferredSize() 和 setPreferredSize() 之间的区别