简体   繁体   English

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

[英]How to display image on JWIndow when a JButton is clicked?

I have a JMenuBar with a help button. 我有一个带有帮助按钮的JMenuBar When user clicks on "help" I need to open a different window with a .jpg showing the game instructions. 当用户单击“帮助”时,我需要打开一个带有显示游戏说明的.jpg的其他窗口。 That same window can be closed when the user clicks outside of it. 当用户在窗口外部单击时,可以关闭该窗口。 I think I'm missing code because it does not work: 我认为我缺少代码,因为它不起作用:

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

 }
}

Menu.java Menu.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);

                      }
    }
    }

EDIT WITH MCVE 用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 ();
    }
}

I suggest you use a JDialog for the instructions window and not a JFrame . 我建议您将JDialog用作指令窗口,而不是JFrame See here for example. 例如参见这里

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

Notes: 笔记:

  • Call setVisible as the last thing you do for a window. 调用setVisible是您对窗口所做的最后一件事。
  • In your real program, where you have components in the windows, call their pack() instead of setting their size. 在实际的程序中,如果您的组件位于窗口中,请调用其pack()而不是设置其大小。
  • Start Swing from the EDT . 从EDT开始摇摆

Why do you have a else-if condition without the if condition? 为什么没有if条件的else-if条件? This would create a syntax error. 这将产生语法错误。

Also, your code to show your window should be something like this: 另外,显示窗口的代码应如下所示:

dispose();
Window rules = new Window( 700, 457, 2);
rules.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM