【问题标题】:How to use JDialog inside a JFrame from another Package?如何在另一个包的 JFrame 中使用 JDialog?
【发布时间】:2016-04-05 20:29:22
【问题描述】:

我正在尝试从菜单栏中获取一个解释我的游戏规则的消息框。当我使用 JFrame 时,我可以让它正确填充,但是,当我单击 Ok 或 X 时,它仍然在我的游戏之外保持打开状态。我必须再次关闭它才能再次使用游戏。我读到我不想使用需要使用 JDialog 的 JFrame,但我遇到了麻烦。

我正在尝试弄清楚如何使用 JDialog 并将其置于另一个包的 JFrame 中。我认为我没有正确使用 JDialog,因为我一开始就无法填充它。我的主游戏从另一个名为 simonish 的包运行,并且有自己的名为 MainGame 的类。而在 MainGame 中有一个私有变量 JFrame 框架。

public class MainGame implements MouseListener, ActionListener{
    private JFrame frame = new JFrame("Simonish");
    private MenuBar menuBar = new MenuBar();
}

当然,这不是所有代码,但这是在 Simonish 包中。

现在我有另一个名为 Component 的包,在里面我有一个名为 MenuBar 的类: 公共类 MenuBar 扩展 JMenuBar 实现 MenuListener、ActionListener{

JMenuBar menuBar;
JMenu settings, stats, help;
JMenuItem chooseColor, chooseMode, highScoreList, history, about, rules;

public MenuBar() {

    //adding help to the menu bar
    help = new JMenu("Help");
    help.addMenuListener(this);
    this.add(help);

    //sub categories for help
    about = new JMenuItem("About");
    about.addActionListener(this);
    help.add(about);

    rules = new JMenuItem("Rules");
    rules.addActionListener(this);
    help.add(rules);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(about)){
            new PopupMenu();
        }
        if(e.getSource().equals(rules)){
            new RulesPopup();
        }
    }
}

我知道,有很多缺失的东西,但尽量保持简短。

在同一个 Component 文件夹中,我有一个名为 PopUpMenu 的类,这是一个可怕的名字,我将称之为 PopUpAbout,但这里是代码。

public class PopupMenu {

    JFrame Popup;

    public PopupMenu(){
        Popup = new JFrame();
        Popup.pack();
        Popup.setVisible(true);


        JOptionPane.showMessageDialog(Popup, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

所以这就是我的问题所在,我试图在 PopUpMenu 类中搞砸,但我似乎根本无法让 JDialog 工作。我已经尝试将它们更改为 JDialog,并更改内部的参数,但我无法让它填充,或者让它填充到主游戏中。

是否需要在 MainGame 中调用?还是需要在PopUpMenu中调用?

【问题讨论】:

  • 为什么需要Popup?您可以将null 作为第一个参数传递给showMessageDialog,或者将MenuBar 的实例传递给它并改用它
  • @MadProgrammer 我使用 Popup 是因为我正在关注我在网上阅读的演示代码。我会尝试 null 或 MenuBar。

标签: java popup jframe messagebox jdialog


【解决方案1】:

您可以像在 JFrames 中一样向 JDialogs 添加/删除组件。看下面的例子,你可以随意改变设计。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JDialogDemo extends JFrame {

    public JDialogDemo() {

        final MyDialog dialog = new MyDialog(this);

        JButton button = new JButton("Show Dialog");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.pack();
                dialog.setLocationRelativeTo(JDialogDemo.this);

                dialog.setVisible(true);
            }
        });

        setLayout(new FlowLayout());
        add(button);

        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JDialogDemo();
    }
}

class MyDialog extends JDialog {
    public MyDialog(Frame owner) {
        super(owner, true);

        setTitle("About");

        add(new JLabel("<html>Version 2<br><br>" + "Added new features: <br><br>" + "Color Customization<br>"
                + "Different Game Modes<br>" + "Menu Bar<br>" + "Images<br>" + "Sound Effects<br>"
                + "History of Score"));

        JButton okButton = new JButton("Ok");
        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        add(okButton, BorderLayout.SOUTH);
    }

}

【讨论】:

  • 或者,正如 OP 所做的那样,您可以使用 JOptionPane,具体取决于您想要实现的目标
  • 没错,这是另一种方式。
【解决方案2】:

问题是,您正在创建第二个 JFrame,当您完成它时您并没有处理它...

public class PopupMenu {

    JFrame Popup;

    public PopupMenu(){
        Popup = new JFrame();
        Popup.pack();
        Popup.setVisible(true);


        JOptionPane.showMessageDialog(Popup, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

JOptionPane 不需要 Component 引用来工作(尽管它可以提供帮助),因此您实际上不需要 JFrame

public class PopupMenu {
    public PopupMenu(){
        JOptionPane.showMessageDialog(null, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

将解决您的一般问题。

您还可以传递对某些父组件的引用,该组件显示在您的父窗口中

public class PopupMenu {
    public PopupMenu(JComponent parent){
        JOptionPane.showMessageDialog(parent, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

然后您可以使用类似...的方式调用它

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(about)){
        new PopupMenu(this);
    }
    if(e.getSource().equals(rules)){
        new RulesPopup();
    }
}

【讨论】:

    猜你喜欢
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2013-10-27
    相关资源
    最近更新 更多