当你运行该文件,它将工作,直到你把一个数字放在文本字段,然后它打破并说空字符串。我只需要帮助这部分。我在英寸字段中使用1英寸转换为厘米。其余评论我只是为了测试和专注于厘米。没有评论它仍然以同样的方式崩溃。这个问题似乎是来自作为行为监听者的行529区域。任何和所有的帮助将是美好的和appriciatedJAVA的GUI距离转换器不使用单选按钮转换
package hardingconversiongui;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GuiConversion extends JFrame {
private JPanel panel1; // A holding panel
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
private JPanel panel6;
private JPanel panel7;
private JPanel panel8;
private JPanel panel9;
private JPanel panel10;
private JPanel panel11;
private JLabel messageLabel;
private JTextField textField;
private ButtonGroup radioButtonGroup;
private final int WINDOW_WIDTH = 900;
private final int WINDOW_HEIGHT = 900;
private JRadioButton centimetersButton;
private JRadioButton metersButton;
private JRadioButton yardsButton;
private JRadioButton milesButton;
private JRadioButton feetButton;
private JRadioButton inchesButton;
private JRadioButton kilometersButton;
private JRadioButton squaremetersButton;
private JRadioButton squareyardsButton;
private JRadioButton squarefeetButton;
private JRadioButton squareinchesButton;
private JRadioButton cubicfeetButton;
private JRadioButton cubicinchesButton;
private JRadioButton cubicmetersButton;
private JRadioButton cubicyardsButton;
private JRadioButton ouncesButton;
private JRadioButton kilogramsButton;
private JRadioButton poundsButton;
private JRadioButton gramsButton;
private JRadioButton quartsButton;
private JRadioButton pintsButton;
private JRadioButton cupsButton;
private JRadioButton gallonsButton;
/**
* Constructor
*/
public GuiConversion() {
// Set the title.
setTitle("CONVERTER");
// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLayout(new GridLayout(11, 1));
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the panel and add it to the frame.
buildPanel1();
// Add the panel to the frame's content pane.
add(panel1);
buildPanel2();
add(panel2);
buildPanel3();
add(panel3);
buildPanel4();
add(panel4);
buildPanel5();
add(panel5);
buildPanel6();
add(panel6);
buildPanel7();
add(panel7);
buildPanel8();
add(panel8);
buildPanel9();
add(panel9);
buildPanel10();
add(panel10);
buildPanel11();
add(panel11);
// Display the window.
setVisible(true);
}
/**
* The buildPanel method adds a label, text field, and and three buttons to
* a panel.
*/
private void buildPanel1() {
// Create the label, text field, and radio buttons.
messageLabel = new JLabel("Enter inches");
textField = new JTextField(10);
centimetersButton = new JRadioButton("Convert to centimeters");
metersButton = new JRadioButton("Convert to meters");
feetButton = new JRadioButton("Convert to feet");
yardsButton = new JRadioButton("Convert to yards");
kilometersButton = new JRadioButton("Convert to kilometers");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(centimetersButton);
radioButtonGroup.add(metersButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(yardsButton);
radioButtonGroup.add(kilometersButton);
// Add action listeners to the radio buttons.
centimetersButton.addActionListener(new RadioButtonListener());
metersButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
yardsButton.addActionListener(new RadioButtonListener());
kilometersButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel1 = new JPanel();
panel1.add(messageLabel);
panel1.add(textField);
panel1.add(centimetersButton);
panel1.add(metersButton);
panel1.add(feetButton);
panel1.add(yardsButton);
panel1.add(kilometersButton);
}
private void buildPanel2() {
// Create the label, text field, and radio buttons.
messageLabel = new JLabel("Enter feet");
textField = new JTextField(10);
metersButton = new JRadioButton("Convert to meters");
yardsButton = new JRadioButton("Convert to yards");
kilometersButton = new JRadioButton("Convert to kilometers");
milesButton = new JRadioButton("Convert to miles");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(metersButton);
radioButtonGroup.add(yardsButton);
radioButtonGroup.add(kilometersButton);
radioButtonGroup.add(milesButton);
// Add action listeners to the radio buttons.
metersButton.addActionListener(new RadioButtonListener());
yardsButton.addActionListener(new RadioButtonListener());
kilometersButton.addActionListener(new RadioButtonListener());
milesButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel2 = new JPanel();
panel2.add(messageLabel);
panel2.add(textField);
panel2.add(metersButton);
panel2.add(yardsButton);
panel2.add(kilometersButton);
panel2.add(milesButton);
}
private void buildPanel3() {
messageLabel = new JLabel("Enter yards");
textField = new JTextField(10);
metersButton = new JRadioButton("Convert to meters");
feetButton = new JRadioButton("Convert to feet");
kilometersButton = new JRadioButton("Convert to kilometers");
milesButton = new JRadioButton("Convert to miles");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(metersButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(kilometersButton);
radioButtonGroup.add(milesButton);
metersButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
kilometersButton.addActionListener(new RadioButtonListener());
milesButton.addActionListener(new RadioButtonListener());
panel3 = new JPanel();
panel3.add(messageLabel);
panel3.add(textField);
panel3.add(metersButton);
panel3.add(feetButton);
panel3.add(kilometersButton);
panel3.add(milesButton);
}
private void buildPanel4() {
messageLabel = new JLabel("Enter square yards");
textField = new JTextField(10);
squareinchesButton = new JRadioButton("Convert to square inches");
squaremetersButton = new JRadioButton("Convert to square meters");
squarefeetButton = new JRadioButton("Convert to square feet");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(squareinchesButton);
radioButtonGroup.add(squaremetersButton);
radioButtonGroup.add(squarefeetButton);
squareinchesButton.addActionListener(new RadioButtonListener());
squaremetersButton.addActionListener(new RadioButtonListener());
squarefeetButton.addActionListener(new RadioButtonListener());
panel4 = new JPanel();
panel4.add(messageLabel);
panel4.add(textField);
panel4.add(squareinchesButton);
panel4.add(squaremetersButton);
panel4.add(squarefeetButton);
}
private void buildPanel5() {
messageLabel = new JLabel("Enter square miles");
textField = new JTextField(10);
squareinchesButton = new JRadioButton("Convert to square inches");
squarefeetButton = new JRadioButton("Convert to square feet");
squareyardsButton = new JRadioButton("Convert to square yards");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(squareinchesButton);
radioButtonGroup.add(squarefeetButton);
radioButtonGroup.add(squareyardsButton);
squareinchesButton.addActionListener(new RadioButtonListener());
squarefeetButton.addActionListener(new RadioButtonListener());
squareyardsButton.addActionListener(new RadioButtonListener());
panel5 = new JPanel();
panel5.add(messageLabel);
panel5.add(textField);
panel5.add(squareinchesButton);
panel5.add(squarefeetButton);
panel5.add(squareyardsButton);
}
private void buildPanel6() {
messageLabel = new JLabel("Enter cubic feet");
textField = new JTextField(10);
cubicinchesButton = new JRadioButton("Convert to cubic inches");
cubicmetersButton = new JRadioButton("Convert to cubic meters");
cubicyardsButton = new JRadioButton("Convert to cubic yards");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(cubicinchesButton);
radioButtonGroup.add(cubicmetersButton);
radioButtonGroup.add(cubicyardsButton);
;
cubicinchesButton.addActionListener(new RadioButtonListener());
cubicmetersButton.addActionListener(new RadioButtonListener());
cubicyardsButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel6 = new JPanel();
panel6.add(messageLabel);
panel6.add(textField);
panel6.add(cubicinchesButton);
panel6.add(cubicmetersButton);
panel6.add(cubicyardsButton);
}
private void buildPanel7() {
messageLabel = new JLabel("Enter cubic yards");
textField = new JTextField(10);
cubicinchesButton = new JRadioButton("Convert to cubic inches");
cubicmetersButton = new JRadioButton("Convert to cubic meters");
cubicfeetButton = new JRadioButton("Convert to cubic feet");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(cubicinchesButton);
radioButtonGroup.add(cubicmetersButton);
radioButtonGroup.add(cubicfeetButton);
cubicinchesButton.addActionListener(new RadioButtonListener());
cubicmetersButton.addActionListener(new RadioButtonListener());
cubicfeetButton.addActionListener(new RadioButtonListener());
panel7 = new JPanel();
panel7.add(messageLabel);
panel7.add(textField);
panel7.add(cubicinchesButton);
panel7.add(cubicmetersButton);
panel7.add(cubicfeetButton);
}
private void buildPanel8() {
messageLabel = new JLabel("Enter ounces");
textField = new JTextField(10);
kilogramsButton = new JRadioButton("Convert to kilograms");
poundsButton = new JRadioButton("Convert to pounds");
gramsButton = new JRadioButton("Convert to grams");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(kilogramsButton);
radioButtonGroup.add(poundsButton);
radioButtonGroup.add(gramsButton);
kilogramsButton.addActionListener(new RadioButtonListener());
poundsButton.addActionListener(new RadioButtonListener());
gramsButton.addActionListener(new RadioButtonListener());
panel8 = new JPanel();
panel8.add(messageLabel);
panel8.add(textField);
panel8.add(kilogramsButton);
panel8.add(poundsButton);
panel8.add(gramsButton);
}
private void buildPanel9() {
messageLabel = new JLabel("Enter pounds");
textField = new JTextField(10);
kilogramsButton = new JRadioButton("Convert to kilograms");
ouncesButton = new JRadioButton("Convert to ounces");
gramsButton = new JRadioButton("Convert to grams");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(kilogramsButton);
radioButtonGroup.add(ouncesButton);
radioButtonGroup.add(gramsButton);
kilogramsButton.addActionListener(new RadioButtonListener());
ouncesButton.addActionListener(new RadioButtonListener());
gramsButton.addActionListener(new RadioButtonListener());
panel9 = new JPanel();
panel9.add(messageLabel);
panel9.add(textField);
panel9.add(kilogramsButton);
panel9.add(ouncesButton);
panel9.add(gramsButton);
}
private void buildPanel10() {
messageLabel = new JLabel("Enter pints");
textField = new JTextField(10);
ouncesButton = new JRadioButton("Convert to ounces");
cupsButton = new JRadioButton("Convert to cups");
quartsButton = new JRadioButton("Convert to quarts");
gallonsButton = new JRadioButton("Convert to gallons");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(ouncesButton);
radioButtonGroup.add(cupsButton);
radioButtonGroup.add(quartsButton);
radioButtonGroup.add(gallonsButton);
ouncesButton.addActionListener(new RadioButtonListener());
cupsButton.addActionListener(new RadioButtonListener());
quartsButton.addActionListener(new RadioButtonListener());
gallonsButton.addActionListener(new RadioButtonListener());
panel10 = new JPanel();
panel10.add(messageLabel);
panel10.add(textField);
panel10.add(ouncesButton);
panel10.add(cupsButton);
panel10.add(quartsButton);
panel10.add(gallonsButton);
}
private void buildPanel11() {
messageLabel = new JLabel("Enter quarts");
textField = new JTextField(10);
ouncesButton = new JRadioButton("Convert to ounces");
pintsButton = new JRadioButton("Convert to pints");
cupsButton = new JRadioButton("Convert to cups");
gallonsButton = new JRadioButton("Convert to gallons");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(ouncesButton);
radioButtonGroup.add(pintsButton);
radioButtonGroup.add(cupsButton);
radioButtonGroup.add(gallonsButton);
ouncesButton.addActionListener(new RadioButtonListener());
pintsButton.addActionListener(new RadioButtonListener());
cupsButton.addActionListener(new RadioButtonListener());
gallonsButton.addActionListener(new RadioButtonListener());
panel11 = new JPanel();
panel11.add(messageLabel);
panel11.add(textField);
panel11.add(ouncesButton);
panel11.add(pintsButton);
panel11.add(cupsButton);
panel11.add(gallonsButton);
}
/**
* Private inner class that handles the event when the user clicks one of
* the radio buttons.
*/
private class RadioButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String input; // To hold the user's input
String convertTo = ""; // The units we're converting to
double convertingNumber = 0.0; // To hold the conversion
// Get the inches entered.
input = textField.getText();
// Determine which radio button was clicked.
if (e.getSource() == centimetersButton) {
convertTo = "centimeters.";
convertingNumber = Double.parseDouble(input) * 2.54;
}/* else if (e.getSource() == metersButton) {
convertTo = " meters.";
convertingNumber = Double.parseDouble(input) * .0254;
} else if (e.getSource() == feetButton) {
convertTo = " feet.";
convertingNumber = Double.parseDouble(input)/12;
} else if (e.getSource() == yardsButton) {
convertTo = " yards.";
convertingNumber = Double.parseDouble(input)/36;
} else if (e.getSource() == kilometersButton) {
// Convert to miles.
convertTo = " kilometers.";
convertingNumber = Double.parseDouble(input) * 0.0000254;
}*/
// Display the conversion.
JOptionPane.showMessageDialog(null, convertingNumber);
}
}
public static void main(String[] args) {
GuiConversion guiConversion = new GuiConversion();
}
}
那么,你需要回答的第一个问题是,哪个字段实际上是输入字段。因为你已经附加了一个'ActionListener',所以每次选择时,它都会触发'ActionListener',而不管文本字段中输入什么。更好的解决方案可能是有一个“转换”按钮,它会触发,检查所选内容并从文本字段中读取值。在尝试转换值之前,您还应该检查输入是否为空 – MadProgrammer
您无法在每个面板中使用相同的变量'textField'。对于每件事你都需要不同的变量,否则你只是在下一个重写每个'buildPanelx'。如果你注释掉了构造函数中的所有buildPanel,但它是可行的。 – Kelo