gpt4 book ai didi

java - 单击 JButton 时如何在 JWIndow 上显示图像?

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:01 27 4
gpt4 key购买 nike

我有一个带有帮助按钮的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。请参阅here例如。

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() 而不是设置它们的大小。
  • Start Swing from the EDT .

关于java - 单击 JButton 时如何在 JWIndow 上显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com