您好,我正在尝试在 Java 中绘制对角线,但这不会像它应该的那样工作..
“值”变量每次都在 for 循环中更新,但它获取下一个值
例如,如果我插入一个 1,我会在控制台中使用 system.out.println(value) 得到这个:
2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304
但变量“值”必须包含我插入的值。我为此使用的代码你可以在下面找到
DrawLines line = new DrawLines();
int value = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
int xPos = 0;
int yPos = getHeight() - (getHeight() / 2);
for(int aantalLines = 0; aantalLines < 10; aantalLines++ ) {
line.drawLines(g, xPos, yPos + value, getWidth(), getHeight() - value );
value += value;
System.out.println(value);
System.out.println(aantalLines);
}
}
public void actionPerformed(ActionEvent e) {
try {
value = Integer.parseInt(tussenRuimte.getText());
repaint();
}
catch(NumberFormatException err) {
JOptionPane.showMessageDialog(null, "Number Format Error: Vul alles goed in s.v.p");
}
}
问题是它不能像这样工作..有人可以解释我做错了什么以及如何解决这个问题吗?