gpt4 book ai didi

Java while循环画线

转载 作者:行者123 更新时间:2023-12-02 07:33:42 25 4
gpt4 key购买 nike

大家好,我正在尝试做一个 Java 书中的练习,我需要做的是画线,在文本字段中我必须说明线之间的距离必须有多远。所有这些都必须在循环中,因此没有硬编码行。

我已经按照我认为应该完成的方式进行了所有操作,但是我的控制台中不断出现一些奇怪的错误。错误代码如下:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:470) at java.lang.Integer.parseInt(Integer.java:499) at h03.LinePanel.paintComponent(LinePanel.java:30) at javax.swing.JComponent.paint(JComponent.java:1037) at javax.swing.JComponent._paintImmediately(JComponent.java:5106) at javax.swing.JComponent.paintImmediately(JComponent.java:4890) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:812) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694) at javax.swing.RepaintManager.access$700(RepaintManager.java:41) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:702) at java.awt.EventQueue.access$400(EventQueue.java:82) at java.awt.EventQueue$2.run(EventQueue.java:663) at java.awt.EventQueue$2.run(EventQueue.java:661) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:672) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

由于我是 Java 新手,所以我不知道我做错了什么以及如何解决此错误,因此我将不胜感激!我在面板中使用的代码是

DrawLines lines = new DrawLines();

public void paintComponent(Graphics g) {

super.paintComponent(g);
int positionY = getHeight() - Integer.parseInt(afstand.getText()); // absolut positioning

int yPos = 0;

while(yPos <= positionY) {

lines.drawLines(g, 0, yPos, getWidth(), yPos);
yPos = yPos + Integer.parseInt(afstand.getText());
}

}

public void actionPerformed(ActionEvent e) {

try {

repaint();


}
catch(NumberFormatException err) {

JOptionPane.showMessageDialog(null, "something went wrong! heeft u wel een waarde opgegeven?");

}

}

最佳答案

问题正是它所说的:您在空字符串上调用 Integer.parseInt() ,在您的示例中:afstand.getText(),它确实不喜欢。

java.lang.NumberFormatException: For input string: "" at
[...]
java.lang.Integer.parseInt(Integer.java:470) at java.lang.Integer.parseInt(Integer.java:499)

要避免异常,您可以捕获异常:

try {
int input = Integer.parseInt(afstand.getText());
//rest of your code
} catch (NumberFormatException e) {
//let the use know that the entry is invalid
}

关于Java while循环画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12554450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com