在JFrame中添加图像,单击图像

时间:2014-03-07 13:43:55

标签: java swing jframe jpanel actionlistener

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

这是我的代码。

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){

    }

}

如果有人可以提供帮助,我会感激不尽!

2 个答案:

答案 0 :(得分:2)

  1. 应该从类路径而不是文件系统加载嵌入到程序中的图像。将String传递给ImageIcon时,告诉程序查看文件系统。要从类路径加载,请使用

    new ImageIcon(getClass().getResource("/Bilder/bulbasaur.jpg");
    

    其中Bilder需要位于src

  2. 您的JLabel label2在构造函数中是本地范围的,因此您无法从构造函数外部访问它,即actionPerformed。您需要在构造函数外部声明它,作为类成员,就像您似乎已经使用其他对象一样。

  3. 让所有 ImageIcons已初始化为班级成员。

  4. 只需使用label2.setIcon(oneOfTheImageIcons);中的actionPerformed即可更改JLabel

  5. 的图标
  6. 应该从Event Dispatch Thread运行Swing应用程序。您可以将new LayoutLek();包裹在SwingUtilities.invokeLater..中。有关详细信息,请参阅Initial Threads

  7. 您永远不会将label2添加到可见的角色中。

  8. 在修正了上述所有要点之后,这里是一个可运行的重构器。您只需要相应地更改文件路径。

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

答案 1 :(得分:0)

首先,src.equals(button1)。最好使用基于对象的equals方法,==更好地应用于原始比较(即intlongboolean等。

其次,你可以做几件事。

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

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

  3. 希望这有帮助!