繁体   English   中英

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

[英]adding images in JFrame, image when clicked

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

这是我的代码。

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

    }

}

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

  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. Swing应用程序应该从Event Dispatch Thread运行。 您可以通过包装new LayoutLek(); SwingUtilities.invokeLater.. 有关完整详细信息,请参阅初始线程

  6. 您永远不会将label2添加到可见的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);
        }
    }
}

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

其次,你可以做几件事。

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

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

希望这可以帮助!

暂无
暂无

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

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