我已经起草了下面的应用程序,循环遍历一个字符串数组(我是初学者)。目的是使用JLabel在JFrame上显示数字1到5;改变每一秒。
当JFrame弹出屏幕时,会奇怪地显示数字" 2"位于阵列位置一。这与我的期望相反,我无法弄清楚原因。
我发起1;它存储在" temp&#34 ;;然后将其添加到JLabel,从而添加到JFrame。是否延迟使用这种方法;即JLabel更新时; for循环已经提前了吗?或者,我理解Java.swing有线程问题......?
非常感谢您的帮助。
Ĵ
package testApplication;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Count {
public static void main (String args[]){
String[] numbers = {"one","two","three","four","five"};
JLabel numToDisplay = new JLabel("");
String temp;
//Initiate JFrame
JFrame frame = new JFrame("Counting Application");
frame.setSize(275,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
for (int i=0; i < numbers.length; i++){
temp = numbers[i];
numToDisplay.setText(temp);
frame.add(numToDisplay);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:3)
不要使用Thread.sleep而是使用Swing Timer,以免让你的GUI进入睡眠状态。
如,
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
public class Count {
private static int index = 0;
public static void main(String args[]) {
final String[] numbers = { "one", "two", "three", "four", "five" };
final JLabel numToDisplay = new JLabel("", SwingConstants.CENTER);
String temp;
// Initiate JFrame
JFrame frame = new JFrame("Counting Application");
frame.setSize(275, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(numToDisplay);
numToDisplay.setText(numbers[index]);
frame.setVisible(true);
int delay = 1000;
new Timer(delay, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
index++;
if (index >= numbers.length) {
((Timer) e.getSource()).stop();
} else {
numToDisplay.setText(numbers[index]);
}
}
}).start();
}
}
或
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class Count2 extends JPanel {
private static final String[] NUMBERS = { "one", "two", "three", "four", "five" };
private static final int PREF_W = 300;
private static final int PREF_H = 100;
private static final int TIMER_DELAY = 1000;
private JLabel numToDisplay = new JLabel("", SwingConstants.CENTER);
private int index = 0;
public Count2() {
numToDisplay.setFont(numToDisplay.getFont().deriveFont(Font.BOLD, 60f));
setLayout(new BorderLayout());
numToDisplay.setText(NUMBERS[index]);
add(numToDisplay);
new Timer(TIMER_DELAY, new TimerListener()).start();
}
@Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSize;
}
int w = Math.max(PREF_W, superSize.width);
int h = Math.max(PREF_H, superSize.height);
return new Dimension(w, h);
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
index++;
if (index >= NUMBERS.length) {
((Timer) e.getSource()).stop();
} else {
numToDisplay.setText(NUMBERS[index]);
}
}
}
private static void createAndShowGui() {
Count2 mainPanel = new Count2();
JFrame frame = new JFrame("Count2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}