我有一个带有帮助按钮的JMenuBar
。当用户点击"帮助"时,我需要打开一个不同的窗口,上面有一个显示游戏说明的.jpg
。当用户单击该窗口外部时,可以关闭同一窗口。我想我缺少代码,因为它不起作用:
窗口.java
public class Window extends JWindow
{
//java.net.URL imgIntro = getClass().getResource("/images/intro.jpg");
ImageIcon imIntro = new ImageIcon(getClass().getResource("/images/intro.jpg"));
//java.net.URL imgRegles = getClass().getResource("/images/rules.jpg");
ImageIcon imRules = new ImageIcon(getClass().getResource("/images/rules.jpg"));
static Thread t = new Thread();
static int thread = 0;
static JButton bouton;
static int X;
static int Y;
public Window( int X, int Y, int type) {
super();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(X,Y);
setLocation( (dim.width - X)/2 , (dim.height - Y)/2);
setVisible(true);
Container fen = getContentPane();
if (type == 1 ) bouton = new JButton(imIntro);
else bouton = new JButton(imRules);
bouton.setPreferredSize(new Dimension(X, Y) );
fen.add( bouton);
bouton.setVisible( true );
show();
/* if window introduction,
just display for 5 secondes */
if( type == 1 ) {
try {
Thread.sleep(5000);
thread = 1;
}
catch( java.lang.InterruptedException ex ) {
JOptionPane.showMessageDialog(null, "erreur");
}
dispose();
}
/* if window of rules
only close it when user clicks */
else if ( type == 2 ) {
bouton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}
菜单.java
public class Menu extends JMenuBar implements ActionListener{
Interface map;
JMenu m5;
public Menu(Interface map){
super();
this.map=map;
m5 = new JMenu ("Help");//dislay instructions / rules
this.add(m5);
public void actionPerformed(ActionEvent evt){
//.../
else if(evt.getSource () == m5){
//new JWindow
Window rules = new Window( 700, 457, 2);
}
}
}
使用 MCVE 编辑
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
/**
*
* @author Mark
*/
public class Test {
JFrame mainFrame = new JFrame ();
JFrame instructions = new JFrame();
public Test (){
gui ();
}
public void gui (){
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600, 400);
mainFrame.setVisible(true);
instructions.setSize(200, 200);
JMenuBar mb = new JMenuBar ();
JMenu help = new JMenu("Help");
mb.add(help);
JMenuItem instructionsMenu = new JMenuItem ("Instructions");
help.add(instructions);
mainFrame.setJMenuBar(mb);
instructions.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
instructions.setVisible(true);
mainFrame.dispose();
}
});
}
public static void main(String[] args) throws IOException, URISyntaxException
{
new Test ();
}
}
我建议您对说明窗口使用JDialog
而不是JFrame
。例如,请参阅此处。
public class Test {
JFrame mainFrame = new JFrame();
JDialog instructions = new JDialog(mainFrame);
public Test() {
gui();
}
public void gui() {
instructions.setSize(200, 200);
JMenuBar mb = new JMenuBar();
JMenu help = new JMenu("Help");
mb.add(help);
JMenuItem instructionsMenu = new JMenuItem("Instructions");
help.add(instructionsMenu);
instructionsMenu.addActionListener(e -> instructions.setVisible(true));
mainFrame.setJMenuBar(mb);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600, 400);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test());
}
}
笔记:
- 调用
setVisible
作为您为窗口执行的最后一件事。 - 在实际程序中,窗口中有组件,调用它们
pack()
而不是设置它们的大小。 - 从 EDT 开始摆动。
为什么你有一个没有 if 条件的 else-if 条件?这将创建语法错误。
此外,显示窗口的代码应如下所示:
dispose();
Window rules = new Window( 700, 457, 2);
rules.show();