Java 不重新显示图像
大家好,我在这里有点累。当我运行程序并按下submit按钮时,它应该每2秒钟更改4张图片。但是它不会重新显示图片。如果有人能帮我一把,那就太好了。我正在使用eclipse,程序正在编译和运行。这是代码Java 不重新显示图像,java,swing,imageicon,Java,Swing,Imageicon,大家好,我在这里有点累。当我运行程序并按下submit按钮时,它应该每2秒钟更改4张图片。但是它不会重新显示图片。如果有人能帮我一把,那就太好了。我正在使用eclipse,程序正在编译和运行。这是代码 /** Here is the GUI of the program * class name SlideShowGui.java * @author Kiril Anastasov * @date 07/03/2012 */ import java.awt.*; import java
/** 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, Runnable
{
JLabel name, comments, images;
JTextField namejtf, commentsjtf, captionjtf;
JButton submit;
ImageIcon pictures1, pictures2, pictures3, pictures4;
//ImageIcon []pictures2 = {galileo1.jpg};
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);
pictures1 = new ImageIcon("galileo1.jpg");
images = new JLabel(pictures1);
this.add(images);
pictures2 = new ImageIcon("galileo2.jpg");
this.add(images);
pictures3 = new ImageIcon("galileo3.jpg");
this.add(images);
pictures4 = new ImageIcon("galileo4.jpg");
this.add(images);
captionjtf = new JTextField(24);
this.add(captionjtf);
//pictures = new ImageIcon("galileo1.jpg");
// images.setIcon(pictures);
}
public void actionPerformed(ActionEvent ae)
{
Thread t = new Thread(this);
t.start();
if(ae.getSource() == submit)
{
int i = 0;
boolean go = true;
while(go)
{
i++;
System.out.println(i);
try
{
Thread.sleep(2000);
if(i == 1)
{
pictures1 = new ImageIcon("galileo1.jpg");
images.setIcon(pictures1);
System.out.println("picture 1 should be displayed here");
}
if(i == 2)
{
pictures2 = new ImageIcon("galileo2.jpg");
images.setIcon(pictures2);
System.out.println("picture 2 should be displayed here");
}
if(i == 3)
{
pictures3 = new ImageIcon("galileo3.jpg");
images.setIcon(pictures3);
System.out.println("picture 3 should be displayed here");
}
if(i == 4)
{
pictures4 = new ImageIcon("galileo4.jpg");
images.setIcon(pictures4);
System.out.println("picture 4 should be displayed here");
}
if(i == 4)
{
i = 0;
}
}
catch (InterruptedException ie)
{
System.out.println("thread exception");
}
}
}
}
public void run()
{
}
}
如果i==,则整个。。。部分可以简化。在SlideShowGUI中声明静态类成员:
并将其用于替换if语句:
images.setIcon(icons[i-1]);
System.printf("Picture %s should be displayed%n", i-1);
if (i == 4) {
i = 0;
}
如果i==,则整个。。。部分可以简化。在SlideShowGUI中声明静态类成员:
并将其用于替换if语句:
images.setIcon(icons[i-1]);
System.printf("Picture %s should be displayed%n", i-1);
if (i == 4) {
i = 0;
}
您可以像Andreas_D提到的那样简化代码 当前设计将在调用thread.sleep时阻止主线程,这将冻结应用程序 如果要更新映像,应该在run方法中实现更新代码
因此,如果您检测到用户按下submit按钮,请创建并启动新线程以更新UI。您可以像Andreas\D提到的那样简化代码 当前设计将在调用thread.sleep时阻止主线程,这将冻结应用程序 如果要更新映像,应该在run方法中实现更新代码 因此,如果检测到用户按下submit JButton,请创建并启动新线程以更新UI。始终使用javax.swing.Timer从不使用Thread.sleep。。。至少在摇摆中。在此处尝试此代码,但一定要替换图像的路径:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SlideShow extends JPanel
{
private int i = 0;
private Timer timer;
private JLabel images = new JLabel();
private Icon bg = UIManager.getIcon("OptionPane.warningIcon");
private Icon red = UIManager.getIcon("OptionPane.errorIcon");
private Icon blue = UIManager.getIcon("OptionPane.informationIcon");
private ImageIcon pictures1, pictures2, pictures3, pictures4;
private ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
boolean go = true;
i++;
System.out.println(i);
if(i == 1)
{
images.setIcon(bg);
System.out.println("picture 1 should be displayed here");
}
if(i == 2)
{
images.setIcon(red);
System.out.println("picture 2 should be displayed here");
}
if(i == 3)
{
images.setIcon(blue);
System.out.println("picture 3 should be displayed here");
}
if(i == 4)
{
images.setIcon(bg);
System.out.println("picture 4 should be displayed here");
}
if(i == 5)
{
go = false;
timer.stop();
System.exit(0);
}
revalidate();
repaint();
}
};
public SlideShow()
{
JFrame frame = new JFrame("SLIDE SHOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.getContentPane().add(this);
add(images);
frame.setSize(300, 300);
frame.setVisible(true);
timer = new Timer(2000, action);
timer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new SlideShow();
}
});
}
}
始终使用javax.swing.Timer从不使用Thread.sleep。。。至少在摇摆中。在此处尝试此代码,但一定要替换图像的路径:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SlideShow extends JPanel
{
private int i = 0;
private Timer timer;
private JLabel images = new JLabel();
private Icon bg = UIManager.getIcon("OptionPane.warningIcon");
private Icon red = UIManager.getIcon("OptionPane.errorIcon");
private Icon blue = UIManager.getIcon("OptionPane.informationIcon");
private ImageIcon pictures1, pictures2, pictures3, pictures4;
private ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
boolean go = true;
i++;
System.out.println(i);
if(i == 1)
{
images.setIcon(bg);
System.out.println("picture 1 should be displayed here");
}
if(i == 2)
{
images.setIcon(red);
System.out.println("picture 2 should be displayed here");
}
if(i == 3)
{
images.setIcon(blue);
System.out.println("picture 3 should be displayed here");
}
if(i == 4)
{
images.setIcon(bg);
System.out.println("picture 4 should be displayed here");
}
if(i == 5)
{
go = false;
timer.stop();
System.exit(0);
}
revalidate();
repaint();
}
};
public SlideShow()
{
JFrame frame = new JFrame("SLIDE SHOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.getContentPane().add(this);
add(images);
frame.setSize(300, 300);
frame.setVisible(true);
timer = new Timer(2000, action);
timer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new SlideShow();
}
});
}
}
不,不,你又忘了,昨天同样的问题,被@mKorbel回答了,你甚至忘了应用他说的一句话。永远不要使用线程。睡眠。。。在Swing中,这将冻结您的应用程序。而是使用javax.swing.Timer来实现这种行为。你的EDT在哪里?@Gagandeep巴厘特技演员:-不,不,你又忘了,昨天同样的问题,被@mKorbel回答了,你甚至忘了应用他说的一句话。永远不要使用线程。睡眠。。。在Swing中,这将冻结您的应用程序。而是使用javax.swing.Timer来实现这种行为。你的EDT在哪里?@Gagandeep巴厘特技演员:-我的回答是一个一般性提示,它不应该解决重新显示图像的实际问题…对于基本内容,这是+1。我的回答是一个一般性提示,它不应该解决重新显示图像的实际问题…对于基本内容,这是+1stuff@Kiril:呵呵,记住-保持微笑:-@mKorbel:谢谢,保持微笑:-@Kiril:呵呵,记住-保持微笑:-@mKorbel:谢谢,保持微笑:-