Java while循环绘制线条

时间:2012-09-23 17:19:01

标签: java

大家好我想尝试用Java书做练习,我需要做的是绘制线条,在文本区域内我必须说明线条之间的距离必须有多远。所有这一切都必须在一个循环中,所以没有硬编码的行。

我已经做了一切,就像我认为它应该完成但我在我的控制台中得到一些奇怪的错误。错误代码如下:

  

线程中的异常“AWT-EventQueue-0”java.lang.NumberFormatException:对于输入字符串:“”       at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)       在java.lang.Integer.parseInt(Integer.java:470)       在java.lang.Integer.parseInt(Integer.java:499)       在h03.LinePanel.paintComponent(LinePanel.java:30)       在javax.swing.JComponent.paint(JComponent.java:1037)       在javax.swing.JComponent._paintImmediately(JComponent.java:5106)       在javax.swing.JComponent.paintImmediately(JComponent.java:4890)       在javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:812)       在javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)       at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)       在javax.swing.RepaintManager.access $ 700(RepaintManager.java:41)       在javax.swing.RepaintManager $ ProcessingRunnable.run(RepaintManager.java:1672)       在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)       在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?");

    }

}

2 个答案:

答案 0 :(得分:2)

问题正是它所说的:你在一个空字符串上调用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
}

答案 1 :(得分:0)

getText()。toString()获取Textfield的值。您可以尝试使用System.out.println(afstand.getText)和toString来查看差异。

否则Textfield中没有整数值。