java - 在JFrame中添加图像,点击时图像

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

    }

}

如果有人能提供帮助,我将不胜感激!

最佳答案

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

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

    其中 Bilder 需要位于 src

  2. 您的 JLabel label2 位于构造函数内,因此您无法从构造函数外部(即 actionPerformed)访问它。您需要在构造函数外部将其声明为类成员,就像您对其他对象所做的那样。

  3. 所有三个 ImageIcons 也已初始化为类成员。

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

  5. Swing 应用程序应从事件调度线程运行。您可以通过将 new LayoutLek(); 包装在 SwingUtilities.invokeLater.. 中来实现此目的。请参阅Initial Threads了解完整详细信息。

  6. 您永远不会将 label2 添加到可见容器中。

修复上述所有问题后,这是一个可运行的重构。您只需相应地更改文件路径即可。

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

关于java - 在JFrame中添加图像,点击时图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22251767/

相关文章:

java - 当我尝试初始化 CategoryAxis xAxis 时出现 java.lang.ExceptionInInitializerError

java - 带有 Swing UI 的 Spring-Boot

java - 将此指针传递给构造函数的编译器错误?

java - 暂停执行直到子对话框关闭

java - Java中如何生成不重复的随机数

java - 使用低级数据存储 API 有什么问题

java - Atomikos vs. Bitronix vs. JBossTS - MVCC 和嵌套事务

java - 我应该尽可能使用并行流吗?

java - 定时器启动后可切换按钮不起作用

java - 在 JFrame 中分层多个组件