JAVA GUI距离转换器不使用单选按钮进行转换

时间:2017-03-12 21:27:14

标签: java swing

当你运行文件时它会工作,直到你在文本字段中放一个数字,然后它打破并说空字符串。我只需要帮助这部分。我在英寸字段中使用1英寸转换为厘米。我将其余部分评论为测试并专注于厘米。没有评论它仍然以同样的方式崩溃。问题似乎来自第529行,即行动监听者。任何和所有的帮助都会很精彩和苛刻

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();
}
}

1 个答案:

答案 0 :(得分:0)

在构建屏幕时,您使用的是textField的1个变量。您可以使用它来创建一个新的TextField,然后将其添加到面板1.然后使用它来创建一个新的TextField(丢失对最后一个的引用)。这一直重复,直到你到达最后一个。这意味着当您从“textField”对象获取“输入”值时,它只会查看您创建的最后一个(屏幕上的最后一个)。您需要为每个TextField创建一个单独的变量。 textInches,textFeet,textYards等。

更新:这里有一些代码可能会帮助你理顺。

package net.bcr666.javatest;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class GuiConversion extends JFrame {
private static final long serialVersionUID = 1L;

private JRadioButton rdoLength = new JRadioButton("Length");
private JRadioButton rdoArea = new JRadioButton("Area");
private JRadioButton rdoVolume = new JRadioButton("Volume");
private JRadioButton rdoWeight = new JRadioButton("Weight");
private JRadioButton rdoLiquidVolume = new JRadioButton("Liquid Volume");

ButtonGroup group = new ButtonGroup();
{
    group.add(rdoLength);
    group.add(rdoArea);
    group.add(rdoVolume);
    group.add(rdoWeight);
    group.add(rdoLiquidVolume);
    rdoLength.setSelected(true);
}

CardLayout layoutCard = new CardLayout();
JPanel pnlCard = new JPanel(layoutCard);

public GuiConversion() {
// Set the title.
setTitle("CONVERTER");

setLayout(new BorderLayout());

// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().add(buildSelectionPanel(), BorderLayout.NORTH);
getContentPane().add(pnlCard, BorderLayout.CENTER);

pnlCard.add(buildLengthPanel(), "LENGTH");
pnlCard.add(buildAreaPanel(), "AREA");
pnlCard.add(buildVolumePanel(), "VOLUME");
pnlCard.add(buildWeightPanel(), "WEIGHT");
pnlCard.add(buildLiquidVolumePanel(), "LIQUID VOLUME");

rdoLength.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        layoutCard.show(pnlCard, "LENGTH");
    }
});
rdoArea.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        layoutCard.show(pnlCard, "AREA");
    }
});
rdoVolume.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        layoutCard.show(pnlCard, "VOLUME");
    }
});
rdoWeight.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        layoutCard.show(pnlCard, "WEIGHT");
    }
});
rdoLiquidVolume.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        layoutCard.show(pnlCard, "LIQUID VOLUME");
    }
});

pack();
// Display the window.
setVisible(true);
}

private JPanel buildSelectionPanel() {
    JPanel panel = new JPanel(new FlowLayout());

    panel.add(new JLabel("Please select conversion type"));
    panel.add(rdoLength);
    panel.add(rdoArea);
    panel.add(rdoVolume);
    panel.add(rdoWeight);
    panel.add(rdoLiquidVolume);

    return panel;
}

private JPanel buildLengthPanel() {
    JPanel panel = new JPanel(new FlowLayout());

    String[] inputTypes = {"Inches","Feet","Yards"};
    JComboBox<String> cboInputType = new JComboBox<>(inputTypes);
    JTextField txtInputLength = new JTextField(10);
    String[] convertTypes = {"Centimeters","Meters","Feet","Yards","Kilometers"};
    JComboBox<String> cboConvertType = new JComboBox<>(convertTypes);
    JTextField txtConvertLength = new JTextField(10);
    txtConvertLength.setEnabled(false);
    JLabel lblConvertType = new JLabel();
    JButton btnConvertLength = new JButton("Convert");
    btnConvertLength.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                txtConvertLength.setText("");
                lblConvertType.setText("");
                double inputLength = Double.parseDouble(txtInputLength.getText());
                String strInputType = (String) cboInputType.getSelectedItem();
                String strConvertType = (String) cboConvertType.getSelectedItem();
                double convertFactor = 0.0d;
                if ("Inches".equals(strInputType)) {
                    if ("Centimeters".equals(strConvertType)) {
                        convertFactor = 2.54;
                    } else if ("Meters".equals(strConvertType)) {
                        convertFactor = 0.0254;
                    } else if ("Kilometers".equals(strConvertType)) {
                        convertFactor = 0.0000254;
                    } else if ("Inches".equals(strConvertType)) {
                        convertFactor = 1.0;
                    } else if ("Feet".equals(strConvertType)) {
                        convertFactor = 0.0833333;
                    } else if ("Yards".equals(strConvertType)) {
                        convertFactor = 0.0277778;
                    }
                } else if ("Feet".equals(strInputType)) {
                    // some conversion
                } else if ("Yards".equals(strInputType)) {
                    // some conversion
                }

                double convertLength = inputLength * convertFactor;
                txtConvertLength.setText(Double.toString(convertLength));
                lblConvertType.setText(strConvertType);
                GuiConversion.this.pack(); // resize the frame to account for added text
            } catch (Exception ex) {
                // could not convert text to number, should probably warn the user
            }
        }
    });

    panel.add(new JLabel("Convert "));
    panel.add(txtInputLength);
    panel.add(cboInputType);
    panel.add(new JLabel(" to "));
    panel.add(cboConvertType);
    panel.add(btnConvertLength);
    panel.add(new JLabel(" = "));
    panel.add(txtConvertLength);
    panel.add(lblConvertType);

    return panel;
}

private JPanel buildAreaPanel() {
    JPanel panel = new JPanel(new FlowLayout());

    panel.add(new JLabel("Area"));

    return panel;
}

private JPanel buildVolumePanel() {
    JPanel panel = new JPanel(new FlowLayout());

    panel.add(new JLabel("Volume"));

    return panel;
}

private JPanel buildWeightPanel() {
    JPanel panel = new JPanel(new FlowLayout());

    panel.add(new JLabel("Weight"));

    return panel;
}

private JPanel buildLiquidVolumePanel() {
    JPanel panel = new JPanel(new FlowLayout());

    panel.add(new JLabel("Liwuid Volume"));

    return panel;
}

public static void main(String[] args) {
    GuiConversion guiConversion = new GuiConversion();
}
}
相关问题