java - 在同一个 jlabel 上重新显示其他 img 存在困难

标签 java swing

我有点卡住了。当我按下提交按钮时,应该在 JLabel 图像的同一位置重新显示另一张图片,因此如果有人有任何想法,我将不胜感激,我正在使用 eclipse 并且程序正在编译和运行。这是代码

/** Here is the GUI of the program
 * class name SlideShowGui.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;   

public class SlideShowGui extends JPanel  implements ActionListener
{
    JLabel name, comments, images;
    JTextField namejtf, commentsjtf, captionjtf;
    JButton submit;
    ImageIcon pictures;


    SlideShowGui()
    {


        name = new JLabel("Name:");
        this.add(name);

        namejtf = new JTextField(15);
        this.add(namejtf);

        comments = new JLabel("Comments:");
        this.add(comments);

        commentsjtf = new JTextField(15);
        this.add(commentsjtf);

        submit = new JButton("Submit");
        this.add(submit);
        submit.addActionListener(this);


        pictures = new ImageIcon("galileo1.jpg");
        images = new JLabel(pictures);
        this.add(images);


//      pictures2 = new ImageIcon("galileo2.jpg");
//      images2 = new JLabel(pictures2);
//      this.add(images2);



        captionjtf = new JTextField(24);
        this.add(captionjtf);

           public void actionPerformed(ActionEvent ae)
        {
        if(ae.getSource() == submit)
        {

            pictures = new ImageIcon("galileo2.jpg");
            images = new JLabel(pictures);

            System.out.println("test");
        }

    }
}

    }

/**The driver class of the program. Here is the JFrame 
 * class name TestSlideShow.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import javax.swing.*;
public class TestSlideShow 
{
    public static void main(String[] args) 
    {
        JFrame application = new JFrame();
        SlideShowGui panel = new SlideShowGui();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(300,600);
        application.setLocation(400,100);
        application.setVisible(true);


    }

}

最佳答案

改变这个

if(ae.getSource() == submit)
    {

        pictures = new ImageIcon("galileo2.jpg");
        images = new JLabel(pictures);

        System.out.println("test");
    }

if(ae.getSource() == submit)
    {

        pictures = new ImageIcon("galileo2.jpg");
        images.setIcon(pictures);

        System.out.println("test");
    }

关于java - 在同一个 jlabel 上重新显示其他 img 存在困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9616078/

相关文章:

java - 如何在用户点击的 TextView 上显示数字?

java - 预触发 propertyChangeEvent 的危险

java - 在 JFileChooser 保存对话框中强制使用 ".png"

java - 如何向采用值 id 的每一行添加删除操作

java - 使用 Struts 2 xml 验证的自定义 validator 错误消息

java - "if"语句的问题

java - JTextArea 的输入

java - 使用 BorderLayout 绘制图形并将其放置在面板内

java - 如何设置环境变量以使javac能够找到导入的包?

java - Material DateRangePicker 设置默认范围