所以我做了一个程序,它应该在屏幕上显示一系列 14 X 14 长的字符串。它曾经在您更改大小时起作用,但我将 setResizable 设为 false,所以现在我什么都看不到。我感谢您的所有帮助。这是代码:
private Color textColor = Color.BLACK;
private Random random = new Random();
String[][] letters = new String[14][14];
float fontsize = Toolkit.getDefaultToolkit().getScreenSize().width / 50;
public void paint(Graphics g) {
g.setColor(textColor);
setFont(getFont().deriveFont(fontsize));
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 13; j++) {
if ((int) (Math.random() * 100) > 97) {
letters[i][j] = "w";
textColor = Color.BLUE;
System.out.println("asd");
} else {
letters[i][j] = "l";
textColor = new Color(0, 100, 0);
}
g.drawString(letters[i][j], i * 34, j * 34);
if (i >= 99)
System.out.println("asdasdasd");
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// repaint();
}
public mainClass() {
setSize(Toolkit.getDefaultToolkit().getScreenSize().width / 3,
Toolkit.getDefaultToolkit().getScreenSize().width / 3 + 80);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Timer timer = new Timer(500, this);
timer.setInitialDelay(0);
timer.start();
setResizable(false);
this.setBackground(Color.BLACK);
setVisible(true);
}
public static void main(String[] args) {
mainClass myWindow = new mainClass();
}