Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JFrame中添加图像,单击时显示图像_Java_Swing_Jframe_Jpanel_Actionlistener - Fatal编程技术网

Java 在JFrame中添加图像,单击时显示图像

Java 在JFrame中添加图像,单击时显示图像,java,swing,jframe,jpanel,actionlistener,Java,Swing,Jframe,Jpanel,Actionlistener,我想做一个“简单”的程序。它由三个按钮组成,当你点击其中一个按钮时,我想显示一张图片,但我不知道如何正确添加图像。 如果有人玩过口袋妖怪,我想从你选择你的入门口袋妖怪开始 这是我的密码 public LayoutLek(){ super("Starter"); panel=new JPanel(); panel.setLayout(new GridLayout(2,1)); top_p=new JPanel();

我想做一个“简单”的程序。它由三个按钮组成,当你点击其中一个按钮时,我想显示一张图片,但我不知道如何正确添加图像。 如果有人玩过口袋妖怪,我想从你选择你的入门口袋妖怪开始

这是我的密码

public LayoutLek(){

    super("Starter");
    panel=new JPanel();
    panel.setLayout(new GridLayout(2,1));
    top_p=new JPanel();                             

    label1=new JLabel("Make a choice");
    label1.setFont(new Font("Arial", Font.BOLD, 30));
    label1.setForeground(Color.black);

    ImageIcon green = new ImageIcon("Bilder/bulbasaur.jpg");   //Dont know if it is correct but...
    JLabel label2 = new JLabel(green);

    top_p.setBackground(Color.yellow);
    top_p.add(label1);
    bottom_p=new JPanel();                          
    bottom_p.setLayout(new GridLayout(1,3));

    panel.add(top_p);
    panel.add(bottom_p);

    button1=new JButton("Button 1");
    button1.setBackground(Color.green);
    button1.setForeground(Color.black);
    button1.setFont(new Font("Arial", Font.BOLD, 24));
    button2=new JButton("Button 2");
    button2.setBackground(Color.red);
    button2.setForeground(Color.black);
    button2.setFont(new Font("Arial", Font.BOLD, 24));
    button3=new JButton("Button 3");
    button3.setBackground(Color.blue);
    button3.setForeground(Color.black);
    button3.setFont(new Font("Arial", Font.BOLD, 24));

    bottom_p.add(button1);
    bottom_p.add(button2);
    bottom_p.add(button3);

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);

    this.add(panel);
    //this.setSize(350, 300);
    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setAlwaysOnTop(true);

}

public static void main(String[] args) {
    new LayoutLek();

}

public void actionPerformed(ActionEvent e) {
    System.out.println("Clicked");      //Just to test
    Object src = e.getSource();
    if(src==button1){
                       //Here should the image show up
    }
    else if(src==button2){

    }
    else if(src==button3){

    }

}
如果有人能帮助我,我将不胜感激

首先,
src.equals(按钮1)
。最好使用基于对象的equals方法,==更好地应用于基本比较(即
int
long
boolean
,等等)

其次,你可以做几件事

  • 将图像添加到容器中,然后在每次单击按钮时将其删除并添加另一个

  • 将所有三个图像添加到一个容器中,将它们全部设置为不可见(
    setVisible(false)
    ),然后在
    src.equals(button1/2/3)
    中将相应的图像设置为可见。容器可能需要重新喷漆

  • 希望这有帮助

  • 嵌入到程序中的图像应该从类路径加载,而不是从文件系统加载。当您将字符串传递给
    图像图标时,
    会告诉程序查看文件系统。要从类路径加载,请使用

    new ImageIcon(getClass().getResource("/Bilder/bulbasaur.jpg");
    
    其中,
    Bilder
    需要位于
    src

  • 您的
    JLabel label2
    在构造函数中是局部作用域,因此您无法从构造函数外部访问它,即执行的
    操作
    。您需要在构造函数之外将其声明为类成员,就像您对其他对象所做的那样

  • 将所有三个
    ImageIcons
    也初始化为类成员

  • 只需使用
    label2.setIcon(图像图标之一)操作中的code>更改
    JLabel的图标

  • Swing应用程序应该从事件调度线程运行。您可以通过包装
    新的LayoutLek()来实现这一点
    SwingUtilities.invokeLater..
    中。有关完整的详细信息,请参阅

  • 您从未将
    标签2
    添加到可见的conainter

  • 在解决了上述所有问题之后,下面是一个可运行的重构。您只需要相应地更改文件路径

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class LayoutLek extends JFrame implements ActionListener {
    
        JPanel panel;
        JPanel top_p;
        JLabel label1;
        JPanel bottom_p;
        JButton button1;
        JButton button2;
        JButton button3;
    
        ImageIcon green;
        ImageIcon blue;
        ImageIcon red;
        JLabel label2;
    
        public LayoutLek() {
            super("Starter");
    
            green = new ImageIcon(getClass().getResource("/path/to/imgage"));
            blue = new ImageIcon(getClass().getResource("/path/to/imgage"));
            red = new ImageIcon(getClass().getResource("/path/to/imgage"));
            label2 = new JLabel(green);
    
            panel = new JPanel();
            panel.setLayout(new GridLayout(2, 1));
            top_p = new JPanel();
    
            label1 = new JLabel("Make a choice");
            label1.setFont(new Font("Arial", Font.BOLD, 30));
            label1.setForeground(Color.black);
    
            top_p.setBackground(Color.yellow);
            top_p.add(label1);
            bottom_p = new JPanel();
            bottom_p.setLayout(new GridLayout(1, 3));
    
            panel.add(top_p);
            panel.add(bottom_p);
    
            button1 = new JButton("Button 1");
            button1.setBackground(Color.green);
            button1.setForeground(Color.black);
            button1.setFont(new Font("Arial", Font.BOLD, 24));
            button2 = new JButton("Button 2");
            button2.setBackground(Color.red);
            button2.setForeground(Color.black);
            button2.setFont(new Font("Arial", Font.BOLD, 24));
            button3 = new JButton("Button 3");
            button3.setBackground(Color.blue);
            button3.setForeground(Color.black);
            button3.setFont(new Font("Arial", Font.BOLD, 24));
    
            bottom_p.add(button1);
            bottom_p.add(button2);
            bottom_p.add(button3);
    
            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
    
            this.add(panel);
            this.add(label2, BorderLayout.PAGE_START);
            //this.setSize(350, 300);
            this.pack();
            this.setVisible(true);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setAlwaysOnTop(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    new LayoutLek();
                }
            });
        }
    
        public void actionPerformed(ActionEvent e) {
            System.out.println("Clicked");      //Just to test
            Object src = e.getSource();
            if (src == button1) {
                label2.setIcon(green);
            } else if (src == button2) {
                label2.setIcon(blue);
            } else if (src == button3) {
                label2.setIcon(red);
            }
        }
    }
    

    谢谢label2实际上不是nessecary。我在label1中添加了我应该猜到的图像。。。谢谢你的帮助!