我正在尝试使用一个消息框来解释我从游戏菜单栏中填充的游戏规则。当我使用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();
}
}
}
我知道,很多缺失的东西,但试图保持简短。
在同一个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中调用它?
答案 0 :(得分:1)
您可以像在JFrame中一样向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);
}
}
答案 1 :(得分:1)
问题是,您正在创建第二个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();
}
}