我现在正在研究如何使用摆动计时器2天,并试图弄清楚如何更改我在JFrame中心的图像。截至目前,我的程序运行正常,但图像并没有改变我想要的方式。该类仅用作测试,因此可能没有正确的java语法。
package hauntedHouseAdventure;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class timertest {
static JFrame sceneOne = new JFrame();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ImageIcon image = new ImageIcon(
"/Users/computerscience2/Desktop/dark-forest-night-image.jpg");
JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add(imageLabel, BorderLayout.NORTH);
sceneOne.add(panel);
sceneOne.setResizable(false);
imageLabel.setVisible(true);
sceneOne.pack();
JButton leave=new JButton("Leave");
JButton stay= new JButton ("Stay");
leave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
sceneOne.setVisible(false);
}
});
stay.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent f)
{
//Execute when button is pressed
sceneOne.setVisible(false);
}
});
panel.add(leave, BorderLayout.EAST);
panel.add(stay, BorderLayout.WEST);
JLabel label1 = new JLabel("Test");
label1.setText("<html><font color='red'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness"
+ "except the thick and powerful moonlight."
+ "<br>"
+ "You are alone leaving a costume party at a friend's place."
+ "It was rather boring and you decided to leave early."
+ "A stutter is heard and your"
+ "<br>"
+ "car begins to shake"
+ "Your headlights and car lights crack. The engine is left dead silent."
+ "You are left in a total silence"
+ "and baked in merely the moonlight."
+ "<br>"
+ "There is a mere second of silence till a harsh chill ripes through the"
+ "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setVerticalAlignment(JLabel.BOTTOM);
label1.setVisible(true);
label1.setOpaque(false);
panel.add(label1);
final ActionListener updater = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
ImageIcon image = new ImageIcon(
"/Users/computerscience2/Desktop/image-slider-5.jpg");
JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add(imageLabel, BorderLayout.NORTH);
sceneOne.add(panel);
}
};
Timer timer = new Timer(1000, updater);
timer.start();
sceneOne.setSize(2000,1000);
sceneOne.setTitle("The Car");
sceneOne.setVisible(true);
sceneOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sceneOne.setLocation(250, 200);
}
}
答案 0 :(得分:0)
我没有详细查看但是要更改标签上的图像,那么您不需要创建新控件,只需要更改现有图像。
public void actionPerformed(ActionEvent event) {
ImageIcon image = new ImageIcon(
"/Users/computerscience2/Desktop/image-slider-5.jpg");
imageLabel.setIcon(image);
}
您还需要将imageLabel设置为成员变量或最终变量,以便actionPerformed
方法可以看到它。
答案 1 :(得分:0)
我在你的代码中看到了两个问题。
我认为最好的方法就是更改imageLabel的图标。像这样:
final JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
......
final ActionListener updater = new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon image = new ImageIcon("/Users/computerscience2/Desktop/image-slider-5.jpg");
imageLabel.setIcon(image);
imageLabel.updateUI(); //Tell to your frame he needs to redraw the image
}
};