在同一个jlabel上重新显示其他img的困难 [英] difficulty with redisplaying other img on the same jlabel
本文介绍了在同一个jlabel上重新显示其他img的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点卡住了。当我按下按钮时,提交应该重新显示JLabel图像在同一位置的另一张图片,所以如果有人有任何想法我会很感激他们我正在使用eclipse并且程序正在编译和运行。这是代码
I am a bit of stuck. When I press the button submit is should redisplay another picture on the same position the JLabel images so if anyone have any ideas i would appreciate them I am using eclipse and the program is compiling and running. Here is the code
/** 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");
}
这篇关于在同一个jlabel上重新显示其他img的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文