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

时间:2012-03-08 10:23:29

标签: 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);


    }

}

3 个答案:

答案 0 :(得分:5)

更改此

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

答案 1 :(得分:5)

在某些情况下,Icon / ImageIcon不起作用(从网络或硬盘驱动器中吸取)

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

答案 2 :(得分:3)

据我所知,你不是在你的听众中添加新图像。我还认为你需要调用重绘,以便显示新图像:

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

试试这个,它应该可行