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

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

我有一个带帮助按钮的 JMenuBar。当用户点击 "help" 时,我需要打开一个不同的 window,其中 .jpg 显示游戏说明。当用户在其外部单击时,可以关闭相同的 window。我想我缺少代码,因为它不起作用:

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

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

为什么没有 if 条件的 else-if 条件?这会造成语法错误。

此外,显示 window 的代码应该是这样的:

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

我建议您使用 JDialog 作为说明 window 而不是 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 作为 window 的最后一件事。
  • 在您的真实程序中,如果您在 windows 中有组件,请调用它们的 pack() 而不是设置它们的大小。
  • Start Swing from the EDT.