Wenn Sie die Datei ausführen, wird es funktionieren, bis Sie eine Zahl in das Textfeld eingeben, und dann bricht es und sagt leere Zeichenfolge. Ich brauche nur Hilfe mit diesem Teil. Ich benutze 1 Zoll im Zollfeld, um in Zentimeter umzuwandeln. Ich habe den Rest auskommentiert, nur um Zentimeter zu testen und zu konzentrieren. ohne die Kommentare stürzt es immer noch auf die gleiche Weise ab. Das Problem scheint aus dem Bereich der Linie 529 zu stammen, der der Aktionshörer ist. alle und alle Hilfe wäre wunderbar und geschätztJAVA GUI Entfernungskonverter konvertiert nicht mit Radiobuttons
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();
}
}
Nun, die erste Frage, die Sie beantworten müssen, ist, welches Feld eigentlich das Eingabefeld ist. Da Sie einen 'ActionListener' angehängt haben, wird bei jeder Auswahl der 'ActionListener' ausgelöst, unabhängig davon, was in das Textfeld eingegeben wurde. Eine bessere Lösung könnte darin bestehen, eine "convert" -Schaltfläche zu haben, die auslöst, prüft, was ausgewählt ist und den Wert aus dem Textfeld liest. Sie sollten auch prüfen, ob die Eingabe leer ist oder nicht, bevor Sie versuchen, den Wert – MadProgrammer
zu konvertieren. Sie können nicht die gleiche Variable 'textField' in jedem Panel verwenden. Sie brauchen für alles unterschiedliche Variablen, sonst überschreiben Sie einfach jedes 'buildPanelx' im nächsten. Wenn Sie alle BuildPanels außer dem ersten im Konstruktor auskommentieren, wird es funktionieren. – Kelo