如何在另一个包的 JFrame 中使用 JDialog?

How to use JDialog inside a JFrame from another Package?

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

我正在尝试弄清楚如何使用 JDialog 并使其在另一个包的 JFrame 中居中。我认为我没有正确使用 JDialog,因为我无法首先填充它。我的主游戏从一个名为 simonish 的不同包运行,并且有自己的 class 称为 MainGame。而在MainGame中有一个私有变量JFrame frame.

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

当然,这不是全部代码,而是在 Simonish 包中。

现在我有另一个名为 Component 的包,里面有一个名为 MenuBar 的 class: public class MenuBar extends JMenuBar implements 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 的 class,这个名字很糟糕,我将其称为 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);
    }
}

所以这就是我的问题,我试图在 class PopUpMenu 中乱搞,但我似乎根本无法让 JDialog 工作。我尝试将它们更改为 JDialog,并更改其中的参数,但我无法将其填充到主游戏中,也无法将其填充到主游戏中。

需要在主游戏中调用吗?还是需要在PopUpMenu中调用?

您可以 add/remove 将组件添加到 JDialogs,就像在 JFrames 中一样。看下面的示例,您可以根据需要更改设计。

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

}

问题是,您正在创建第二个 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);
    }
}

将解决您的一般问题。

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

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