【问题标题】:Trying to display a frame with text inside it when menu button is clicked单击菜单按钮时尝试在其中显示带有文本的框架
【发布时间】:2013-12-01 03:18:08
【问题描述】:

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

所以我的问题是,为什么当我点击规则时没有显示任何内容。 我认为将 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);

            }
     }
}

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

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖 SSCCE。无需在标题中包含主要标签,并注意拼写为“Java”而不是“JAVA”(无需 SHOUT)。 “任何帮助将不胜感激:)” 任何问题。你有问题吗?这是什么?
  • 贴出完整的代码,方便大家理解
  • @peeskillet 试过了,没用。当我点击规则时没有任何反应

标签: java swing jframe actionlistener jmenu


【解决方案1】:

rules 是一个 JMenu:

JMenu rules = new JMenu("Rules");

我认为你的意思是它是一个 JMenuItem。

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

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

【讨论】:

  • 我也注意到,当我运行它时,有时我看不到分数或规则。感谢您的信息。
【解决方案2】:

您需要一个 JMenuItem 来提示窗口。

JMenu rules = new JMenu("Rules");
JMenuItem jmiRules = new JMenuItem("Rules");
rules.add(jmiRules);
menuBar.add(rules);
jmiRules.addActionListener( new rulesAction() );

还将displayRules.setVisible(true); 放在actionPerformed 的末尾,以便在显示之前添加所有内容

还将window.setVisible(true); 放在main 的末尾。它导致了菜单栏的延迟。您应该在使屏幕可见之前添加所有组件。

【讨论】:

  • 啊我明白你在说什么,菜单只是一个标题,它不会提示任何内容。菜单项实际上会提示打开某事或做某事。
【解决方案3】:

JMenu 不支持 ActionListener(即使 addActionListener() 方法有效)。

所以你需要在 JMenu 中添加一个MouseListener 来处理鼠标点击,否则你需要将“Rules”显示为另一个 JMenu 的菜单项。

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多