swing做的界面,为啥要拖动边框才能显示里面的内容??
代码如下:
package frames;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
static final Random random = new Random();
static final StringBuffer sb = new StringBuffer("");
static private int charCounter = 1;
static JFrame frame = new JFrame();
static JPanel panel = new JPanel();
static String letters;
static Label[] labels = new Label[11];
static Timer timer = new Timer();
public GameFrame() {
frame.setBounds(200, 200, 600, 200);
frame.setLayout(new FlowLayout());
panel.setBackground(Color.YELLOW);
panel.setSize(200, 100);
timer.schedule(new TimerTask() {
@Override
public void run() {
panel.removeAll();
letters = makeLetter();
for (int i = 0; i < letters.length(); i++) {
labels[i] = new Label(letters.charAt(i) + "");
System.out.println(labels[i].getText());
panel.add(labels[i]);
}
}
}, 0, 3000);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
}
public static String makeLetter() {
sb.delete(0, sb.length());
for (int i = 0; i < charCounter; i++) {
int ranNum = random.nextInt(26);
char character = (char) (ranNum + 'A');
sb.append(character);
}
charCounter++;
return sb.toString();
}
}
------解决方案--------------------
可能是窗口太小,或者是layout的问题