线程中的异常" AWT-EventQueue-0" java.lang.IllegalArgumentException:大小无效

时间:2017-12-09 21:24:34

标签: java swing netbeans jpanel

我正在Netbeans的一个学校项目上工作。 试图构建一个简单的GUI面板和框架,其中包含一些选项来绘制一些对象。我开始添加一些新的文本字段和一个按钮,让用户绘制一个具有高度,宽度和数量等输入的对象。

在此之后程序将不再运行,它会抛出一个看起来像某种非法论点的异常? 这是错误:

Exception in thread "AWT-EventQueue-0" 
java.lang.IllegalArgumentException: Invalid size
at javax.swing.GroupLayout.checkResizeType(GroupLayout.java:354)
at javax.swing.GroupLayout.checkSize(GroupLayout.java:339)
at javax.swing.GroupLayout.access$500(GroupLayout.java:208)
at javax.swing.GroupLayout$GapSpring.<init>(GroupLayout.java:3173)
at javax.swing.GroupLayout$Group.addGap(GroupLayout.java:1550)
at 
javax.swing.GroupLayout$SequentialGroup.addGap(GroupLayout.java:1855)
at ProjectPanel.initComponents(ProjectPanel.java:322)
at ProjectPanel.<init>(ProjectPanel.java:26)
at ProjectFrame.<init>(ProjectFrame.java:18)
at ProjectFrame$1.run(ProjectFrame.java:78)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
atjava.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD STOPPED (total time: 9 seconds)

我尝试调试并查看此错误的创建位置,但我似乎无法找到特定的行。

我很抱歉发布这个完整的代码,但我似乎无法找到另一个选项来让它重新运行..

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class ProjectPanel extends javax.swing.JPanel {
private int kind = 0;
private int strandstoel;
private String vliegerKleur = "Groen";
private int aantal, hoogte, breedte;

public ProjectPanel() {
    initComponents();
    repaint();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    //Strand
    Color myBeach = new Color(240, 230, 140);
    g.setColor(myBeach);
    g.fillRect(0, 600, 1500, 400);

    //Sky
    Color mySky = new Color(245, 255, 250);
    g.setColor(mySky);
    g.fillRect(0, 0, 1500, 600);

    //SunShine
    if (jCheckBoxZon.isSelected()) {
        Color sunShine = new Color(255, 215, 0);
        g.setColor(sunShine);
        g.fillOval(500, 150, 200, 200);

    } else {
        // REGEN + DONKERE LUCHT
        Color darkSky = new Color(128, 128, 128);
        g.setColor(darkSky);
        g.fillRect(0, 0, 1500, 600);

        Color rain = new Color(30, 144, 255);
        g.setColor(rain);
        g.drawLine(100, 50, 100, 200);
        g.drawLine(200, 50, 200, 1);
        g.drawLine(300, 50, 300, 100);
        g.drawLine(400, 50, 400, 1);
        g.drawLine(500, 50, 500, 100);
        g.drawLine(600, 50, 600, 1);
        g.drawLine(700, 50, 700, 150);
        g.drawLine(800, 50, 800, 1);
        g.drawLine(850, 50, 850, 200);
        g.drawLine(900, 50, 900, 150);
        g.drawLine(1000, 50, 1000, 1);
        g.drawLine(1100, 50, 1100, 100);
        g.drawLine(1150, 50, 1150, 200);
        g.drawLine(1200, 50, 1200, 1);
        g.drawLine(1300, 50, 1300, 150);
        g.drawLine(1400, 50, 1400, 1);
        g.drawLine(1500, 50, 1500, 100);
    }

    //Zee
    Color seaShore = new Color(0, 191, 255);
    g.setColor(seaShore);
    g.fillRect(0, 400, 1500, 200);

    //Kite
    Color flyKite = new Color(0, 255, 127);
    g.setColor(flyKite);
    g.fillPolygon(new int[]{100, 300, 350}, new int[]{100, 300, 65}, 
3);

    // Kinderen
    int x1 = 125;
    int x2 = 150;
    int x3 = 130;
    int x4 = 200;
    int x5 = 95;
    int x6 = 175;
    int x7 = 205;

    int y1 = 40;
    int y2 = 150;
    int y3 = 50;
    int y4 = 200;
    int y5 = 250;
    int y6 = 95;
    int y7 = 75;
    int y8 = 205;
    int y9 = 225;


    for (int i = 0; i < kind; i++) {
        g.setColor(Color.black);
        g.fillOval( x3,330, 40,40 );      // Hoofd
        g.drawLine( x2,370, y2,380 );     // Nek
        g.fillRect( x1,380, 50,150 );    // Lichaam
        g.drawLine( x2,400, y4,470 );   // R Arm
        g.drawLine( x4,430, y5,470 );
        g.drawLine( x1,430, y1,490 );    // L Arm
        g.drawLine( x1,530, y6,620 );    // L Been
        g.drawLine( x5,620, y7,620 );
        g.drawLine( x6,530, y8,620 );   // R Been
        g.drawLine( x7,620, y9,620 );


        x1 = 100 + x1;
        x2 = x2 + 100;
        x3 = x3 + 100;
        x4 = x4 + 100;
        x5 = x5 + 100;
        x6 = x6 + 100;
        x7 = x7 + 100;

        y1 = 100 + y1;
        y2 = y2 + 100;
        y3 = y3 + 100;
        y4 = y4 + 100;
        y5 = y5 + 100;
        y6 = y6 + 100;
        y7 = y7 + 100;
        y8 = y8 + 100;
    }
    //afbeelding
    int tel;
    int xSS = 100;
    int ySS = 550;
    for (tel = 1; tel <= strandstoel; tel++) {
        Graphics2D g2d = (Graphics2D) g;
        ImageIcon strandstoel = new 
ImageIcon(this.getClass().getResource("/images/beach-chair.png"));
        g2d.drawImage(strandstoel.getImage(), xSS, ySS, null);
        xSS = xSS + 250;
        ySS = ySS + 0;
    }

    //RookPluim
    int teller;
    for (teller = 1; teller <= aantal; teller++) {
        g.setColor(Color.GRAY);

    }

    // Versiering Vlieger
        if (null != vliegerKleur) {
            switch (vliegerKleur) {
                case "Rood":
                    g.setColor(Color.RED);
                    g.fillOval(120, 100, 10, 10);
                    g.fillOval(160, 95, 10, 10);
                    g.fillOval(200, 90, 10, 10);
                    g.fillOval(240, 85, 10, 10);
                    g.fillOval(280, 80, 10, 10);
                    g.fillOval(320, 70, 20, 20);
                    g.fillOval(320, 120, 10, 10);
                    g.fillOval(310, 160, 10, 10);
                    g.fillOval(305, 200, 10, 10);
                    g.fillOval(299, 240, 10, 10);
                    g.fillOval(294, 280, 10, 10);
                    break;
                case "Oranje":
                    g.setColor(Color.ORANGE);
                    g.fillOval(120, 100, 10, 10);
                    g.fillOval(160, 95, 10, 10);
                    g.fillOval(200, 90, 10, 10);
                    g.fillOval(240, 85, 10, 10);
                    g.fillOval(280, 80, 10, 10);
                    g.fillOval(320, 70, 20, 20);
                    g.fillOval(320, 120, 10, 10);
                    g.fillOval(310, 160, 10, 10);
                    g.fillOval(305, 200, 10, 10);
                    g.fillOval(299, 240, 10, 10);
                    g.fillOval(294, 280, 10, 10);
                    break;
                case "Blauw": // Was normaal groen vlgs de opdracht, 
maar dit was niet zo goed zichtbaar
                    g.setColor(Color.BLUE);
                    g.fillOval(120, 100, 10, 10);
                    g.fillOval(160, 95, 10, 10);
                    g.fillOval(200, 90, 10, 10);
                    g.fillOval(240, 85, 10, 10);
                    g.fillOval(280, 80, 10, 10);
                    g.fillOval(320, 70, 20, 20);
                    g.fillOval(320, 120, 10, 10);
                    g.fillOval(310, 160, 10, 10);
                    g.fillOval(305, 200, 10, 10);
                    g.fillOval(299, 240, 10, 10);
                    g.fillOval(294, 280, 10, 10);
                    break;
                default:
                    break;
            }
        }

    repaint();
}


private void jCheckBoxZonActionPerformed(java.awt.event.ActionEvent 
evt) {                                             
    // TODO add your handling code here:
    repaint();
}                                            

private void jSliderKinderenMouseReleased(java.awt.event.MouseEvent 
evt) {                                              
    // TODO add your handling code here:
    kind = this.jSliderKinderen.getValue();
    repaint();
}                                             

private void 
jTextFieldAantalStoelenActionPerformed(java.awt.event.ActionEvent evt) 
{                                                        
    // TODO add your handling code here:
    try{
    strandstoel = 
Integer.parseInt(this.jTextFieldAantalStoelen.getText());
    } catch(Exception e){
        JOptionPane.showMessageDialog(null,"Vul een getal in");
    }
    repaint();

}                                                       

private void jList1MouseClicked(java.awt.event.MouseEvent evt) {                                    
    // TODO add your handling code here:
    vliegerKleur = this.jList1.getSelectedValue();
    repaint();
}                                   

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
     try{
         aantal = Integer.parseInt(this.jTextFieldAantal.getText());
        breedte = Integer.parseInt(this.jTextFieldBreedte.getText());
        hoogte = Integer.parseInt(this.jTextFieldHoogte.getText());
     } catch(Exception e){
         JOptionPane.showMessageDialog(null,"Vul een aantal + Hoogte en 
Breedte in");
     }

    repaint();
}                                        


// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBoxZon;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JList<String> jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSlider jSliderKinderen;
private javax.swing.JTextField jTextFieldAantal;
private javax.swing.JTextField jTextFieldAantalStoelen;
private javax.swing.JTextField jTextFieldBreedte;
private javax.swing.JTextField jTextFieldHoogte;
// End of variables declaration                   
}

2 个答案:

答案 0 :(得分:0)

好吧,该错误是因为有时当您使用textBox和Labels或其他组件创建jFrame时,却忘记了例如label的标识符

答案 1 :(得分:0)

该错误的另一个可能原因(例如我的情况)是您可能在不知不觉中添加了诸如JLabelJTextField等元素,但它很小很小您无法识别它的存在,因此在编译时它的大小较小是无效的。

要解决此问题,请打开导航器,然后单击每个元素,检查其在JFrame上的位置,以便您也可以看到该微小的元素。

我让它那样工作。