Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在另一个包的JFrame中使用JDialog?_Java_Popup_Jframe_Messagebox_Jdialog - Fatal编程技术网

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

Java 如何在另一个包的JFrame中使用JDialog?,java,popup,jframe,messagebox,jdialog,Java,Popup,Jframe,Messagebox,Jdialog,我试图从菜单栏中获取一个解释游戏规则的消息框。当我使用JFrame时,我可以让它正确填充,但是,当我单击Ok或X时,它在我的游戏之外仍然保持打开状态。为了再次使用游戏,我必须再次关闭它。我读到我不想使用JFrame,我需要使用JDialog,但我遇到了麻烦 我试图弄清楚如何使用JDialog,并从另一个包中把它放在JFrame的中心。我认为我没有正确使用JDialog,因为我首先无法让它填充。我的主游戏运行于另一个名为simonish的包,它有自己的类,名为MainGame。在MainGame中

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

我试图弄清楚如何使用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();
        }
    }
}
我知道,有很多遗漏的东西,但尽量简短

在同一个组件文件夹中,我有一个名为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);
    }
}
这就是我的问题所在,我试图在类弹出菜单中乱搞,但我似乎根本无法让JDialog正常工作。我尝试将它们更改为JDialog,并更改其中的参数,但我无法让它填充,或让它填充到主游戏中


它是否需要在主游戏中调用?或者是否需要在弹出菜单中调用?

您可以像在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);
    }

}
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.Frame;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JDialog;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类JDialogDemo扩展了JFrame{
公共JDialogDemo(){
最终MyDialog=新建MyDialog(此);
JButton按钮=新JButton(“显示对话框”);
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
dialog.pack();
setLocationRelativeTo(JDialogDemo.this);
对话框.setVisible(true);
}
});
setLayout(新的FlowLayout());
添加(按钮);
设置大小(400400);
setVisible(真);
}
公共静态void main(字符串[]args){
新JDialogDemo();
}
}
类MyDialog扩展了JDialog{
公共MyDialog(框架所有者){
超级(所有者,真实);
设置标题(“关于”);
添加(新JLabel(“版本2

”+”添加了新功能:

“+”颜色自定义
” +“不同的游戏模式”
“+”菜单栏
“+”图像
“+”音效
” +"成绩纪录);; JButton-okButton=新JButton(“Ok”); okButton.addActionListener(新ActionListener(){ @凌驾 已执行的公共无效操作(操作事件e){ setVisible(假); } }); 添加(OK按钮,BorderLayout.SOUTH); } }
您可以像在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);
    }

}
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.Frame;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JDialog;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类JDialogDemo扩展了JFrame{
公共JDialogDemo(){
最终MyDialog=新建MyDialog(此);
JButton按钮=新JButton(“显示对话框”);
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
dialog.pack();
setLocationRelativeTo(JDialogDemo.this);
对话框.setVisible(true);
}
});
setLayout(新的FlowLayout());
添加(按钮);
设置大小(400400);
setVisible(真);
}
公共静态void main(字符串[]args){
新JDialogDemo();
}
}
类MyDialog扩展了JDialog{
公共MyDialog(框架所有者){
超级(所有者,真实);
设置标题(“关于”);
添加(新JLabel(“版本2

”+”添加了新功能:

“+”颜色自定义
” +“不同的游戏模式”
“+”菜单栏
“+”图像
“+”音效
” +"成绩纪录);; JButton-okButton=新JButton(“Ok”); okButton.addActionListener(新ActionListener(){ @凌驾 已执行的公共无效操作(操作事件e){ setVisible(假); } }); 添加(OK按钮,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
不需要
组件
引用来工作(尽管它可以帮助),因此实际上不需要
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();
    }
}

问题是,您正在创建第二个
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
不需要
组件
引用来工作(尽管它可以帮助),因此实际上不需要
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);
    }
}
可以解决你的一般问题

您还可以将引用传递给一些