使用GUI时出错:IllegalStateException



我在尝试运行程序时不断出错,即使在netbeans中制作程序时没有错误。只有当我试图编译时,才会弹出50个错误。有人解释说,我的JTextField没有连接到Horizontal组,我以为我解决了它,但似乎没有。

public class TrianglePerimeter extends javax.swing.JFrame {
public TrianglePerimeter() {
utilizeComponents();
}
@SuppressWarnings("Not Checked")
private void utilizeComponents() {
sideOneInput = new javax.swing.JTextField();
sideTwoInput = new javax.swing.JTextField();
sideThreeInput = new javax.swing.JTextField();
sideOneLabel = new javax.swing.JLabel();
sideTwoLabel = new javax.swing.JLabel();
sideThreeLabel = new javax.swing.JLabel();
findPerimeterButton = new javax.swing.JButton();
perimeterLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Perimeter of a Triangle");
findPerimeterButton.setText("Find the Perimeter");
findPerimeterButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
findPerimeterButtonActionPerformed(evt);
}
}
);
sideOneLabel.setText("Side One");
sideTwoLabel.setText("Side Two");
sideThreeLabel.setText("Side Three");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(findPerimeterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sideOneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sideTwoInput)
.addComponent(sideThreeInput)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(sideOneLabel, javax.swing.GroupLayout.Alignment.TRAILING)
      .addComponent(sideTwoLabel, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(sideThreeLabel)))
.addGap(56, 56, 56)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(perimeterLabel)))))
.addContainerGap(67, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideOneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sideOneLabel))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideTwoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sideTwoLabel))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideThreeInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sideThreeLabel))
.addGap(18, 18, 18)
.addComponent(findPerimeterButton)
.addContainerGap(27, Short.MAX_VALUE)
));
pack();
}
private void findPerimeterButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(evt);
try {
double tempPerimeter = (Double.parseDouble(sideOneInput.getText())) + (Double.parseDouble(sideTwoInput.getText())) + (Double.parseDouble(sideThreeInput.getText()));
perimeterLabel.setText(tempPerimeter + " is the perimeter");
} catch (NumberFormatException e) {
System.err.println("NumberFormatException: " + e.getMessage());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new TrianglePerimeter().setVisible(true);
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel sideOneLabel;
private javax.swing.JLabel sideTwoLabel;
private javax.swing.JLabel sideThreeLabel;
private javax.swing.JButton findPerimeterButton;
private javax.swing.JLabel perimeterLabel;
private javax.swing.JTextField sideOneInput;
private javax.swing.JTextField sideTwoInput;
private javax.swing.JTextField sideThreeInput;
private javax.swing.JTextField tempTextField;
// End of variables declaration//GEN-END:variables
}

这些是的错误

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@3566d06c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1086)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:878)
at java.awt.Container.preferredSize(Container.java:1799)
at java.awt.Container.getPreferredSize(Container.java:1783)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1664)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:920)
at java.awt.Container.preferredSize(Container.java:1799)
at java.awt.Container.getPreferredSize(Container.java:1783)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1664)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
at java.awt.Container.preferredSize(Container.java:1799)
at java.awt.Container.getPreferredSize(Container.java:1783)
at java.awt.Window.pack(Window.java:809)
at TrianglePerimeter.TrianglePerimeter.utilizeComponents(TrianglePerimeter.java:96)
at TrianglePerimeter.TrianglePerimeter.<init>(TrianglePerimeter.java:16)
at TrianglePerimeter.TrianglePerimeter.lambda$main$0(TrianglePerimeter.java:111)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
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)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
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)

有人能告诉我哪里出了问题吗?

Yo Briana,

我没有足够的时间来解决所有问题,但你最大的两个问题包括没有将perimeterLabel组件添加到垂直组中,以及用平行组启动每个布局。如果你阅读了oracle中关于组布局的教程,你会注意到每个布局都以一个SequentialGroup而不是一个并行组开始。这是因为在引用平行元素之前,需要定义布局中的元素序列。这类似于2d数组首先说明它包含的数组数量,然后说明每个数组中的元素。

无论如何,我没有添加周长标签,这样你就可以看到答案,并试图解决问题,弄清楚它应该如何定位。我还删除了所有的空白(但将您的代码留在那里进行注释,以便您阅读(。如果您将自动间隙设置为true,则会为您处理带有的大小调整,并且它会做出响应。

请随意提问。我认为这应该是解决问题的一个良好开端。

public class TrianglePerimeter extends javax.swing.JFrame {
public TrianglePerimeter() {
utilizeComponents();
}
@SuppressWarnings("Not Checked")
private void utilizeComponents() {
sideOneInput = new javax.swing.JTextField();
sideTwoInput = new javax.swing.JTextField();
sideThreeInput = new javax.swing.JTextField();
sideOneLabel = new javax.swing.JLabel();
sideTwoLabel = new javax.swing.JLabel();
sideThreeLabel = new javax.swing.JLabel();
findPerimeterButton = new javax.swing.JButton();
perimeterLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Perimeter of a Triangle");
findPerimeterButton.setText("Find the Perimeter");
findPerimeterButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
findPerimeterButtonActionPerformed(evt);
}
}
);
sideOneLabel.setText("Side One");
sideTwoLabel.setText("Side Two");
sideThreeLabel.setText("Side Three");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
//                                .addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
//                                                                .addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  .addComponent(sideOneLabel, javax.swing.GroupLayout.Alignment.TRAILING)
  .addComponent(sideTwoLabel, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(layout.createSequentialGroup()
//                                                                .addGap(18, 18, 18)
.addComponent(sideThreeLabel)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sideOneInput)
.addComponent(sideTwoInput)
.addComponent(sideThreeInput))
//                                                .addGap(56, 56, 56)
)
.addComponent(findPerimeterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)
//                                .addContainerGap(67, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
//                                .addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideOneLabel)
.addComponent(sideOneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//                                .addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideTwoLabel)
.addComponent(sideTwoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//                                .addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sideThreeLabel)
.addComponent(sideThreeInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//                                .addGap(18, 18, 18)
.addComponent(findPerimeterButton)
//                                .addContainerGap(27, Short.MAX_VALUE)
);
pack();
}
private void findPerimeterButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(evt);
try {
double tempPerimeter = (Double.parseDouble(sideOneInput.getText())) + (Double.parseDouble(sideTwoInput.getText())) + (Double.parseDouble(sideThreeInput.getText()));
perimeterLabel.setText(tempPerimeter + " is the perimeter");
} catch (NumberFormatException e) {
System.err.println("NumberFormatException: " + e.getMessage());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new TrianglePerimeter().setVisible(true);
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel sideOneLabel;
private javax.swing.JLabel sideTwoLabel;
private javax.swing.JLabel sideThreeLabel;
private javax.swing.JButton findPerimeterButton;
private javax.swing.JLabel perimeterLabel;
private javax.swing.JTextField sideOneInput;
private javax.swing.JTextField sideTwoInput;
private javax.swing.JTextField sideThreeInput;
private javax.swing.JTextField tempTextField;
// End of variables declaration//GEN-END:variables
}

相关内容

  • 没有找到相关文章

最新更新