Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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 在同一jlabel上重新显示其他img的困难_Java_Swing - Fatal编程技术网

Java 在同一jlabel上重新显示其他img的困难

Java 在同一jlabel上重新显示其他img的困难,java,swing,Java,Swing,我有点卡住了。当我按下submit按钮时,应该在同一位置重新显示另一张图片,即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.a

我有点卡住了。当我按下submit按钮时,应该在同一位置重新显示另一张图片,即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);


    }

}

据我所知,你没有在你的听众中添加新的图像。我还认为您需要调用repaint,以便显示新图像:

    public void actionPerformed(ActionEvent ae){
       if(ae.getSource() == submit){
        pictures = new ImageIcon("galileo2.jpg");
        images = new JLabel(pictures);
        this.add(images); //adding the new image to your JPanel
        repaint(); //repaint in order to show the image
        System.out.println("test");
    }
}
试试这个,它应该会起作用

改变这个

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

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

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


在某些情况下,Icon/ImageIcon不起作用(从web或HDD读取)


您确定要调用repaint()?如果没有,则需要最小化和最大化窗口以使其显示。要使更改生效,还必须在
repaint()
之前
revalidate()
+1尽管对于
repaint()
。我不知道这件事:-)而且OP也错过了他的事件调度程序线程。@Gagandeep Bali很高兴帮助您,不确定在这种情况下,EDT是否可以从ActionListener正确调用,我认为在这种情况下,答案是正确的……太正确了,毫无疑问,因为他想把同一个标签的图像换成新的东西。
if(ae.getSource() == submit)
    {

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

        System.out.println("test");
    }
pictures = new ImageIcon("galileo2.jpg");
pictures.getImage().flush();
images.setIcon(pictures);