问题
I keep getting errors when trying to run my program even though when making the program in net beans I have no errors. It's only when I try to compile that 50 errors pop up. Someone explained that my JTextField
wasn't attached to the Horizontal group and I thought I fixed it but it seems not.
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
}
These are the errors
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)
Can anyone please tell me where I am going wrong?
回答1:
Yo Briana,
I didn't have enough time to fix everything but your two biggest problems include not adding the perimeterLabel
component to the vertical group and starting every layout with parallel groups. If you read the tutorial on group layout from oracle you'll notice that each layout starts with a SequentialGroup
instead of a parallel group. That is because you need to define the sequence of elements in a layout before you refer to parallel elements. It is similar to how 2d arrays first state the number of arrays it contains and then the elements in each array.
Anyway I did not add the perimeter label so that you could look at the answer and try to solve figure out how it should be positioned. I also removed all the gaps (but left your code there commented so that you read it). If you set auto gaps to true
the sizing with be handled for you and it will be responsive.
Feel free to ask question. I think this should be a good start to figuring things out.
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
}
来源:https://stackoverflow.com/questions/53328079/errors-when-using-gui-illegalstateexception