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

JAVA GUI距离转换器不使用单选按钮进行转换,java,swing,Java,Swing,当您运行文件时,它将一直工作,直到您在文本字段中输入一个数字,然后它会中断并显示空字符串。我只是需要这部分的帮助。我在英寸字段中使用1英寸转换为厘米。我把其余的注释掉了,只是为了测试和关注厘米。没有评论,它仍然以同样的方式崩溃。问题似乎来自529行区域,该区域是动作侦听器。任何和所有的帮助都将是美妙和感激的 package hardingconversiongui; import javax.swing.*; import java.awt.event.*; import java.awt.*

当您运行文件时,它将一直工作,直到您在文本字段中输入一个数字,然后它会中断并显示空字符串。我只是需要这部分的帮助。我在英寸字段中使用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();
}
}

构建屏幕时,textField使用1个变量。您可以使用它创建一个新的文本字段,然后将其添加到面板1。然后使用它创建一个新的文本字段(丢失对上一个文本字段的引用)。这会重复,直到你找到最后一个。这意味着,当您从“textField”对象获取“input”值时,它只查看您最后生成的值(屏幕上的最后一个)。您需要创建单独的变量,每个文本字段一个。文本英寸、文本英尺、文本码等

更新:这里有一些代码可以帮助你理顺思路

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();
}
}
package net.bcr666.javatest;
导入java.awt.BorderLayout;
导入java.awt.CardLayout;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.ButtonGroup;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JRadioButton;
导入javax.swing.JTextField;
公共类GUI转换扩展了JFrame{
私有静态最终长serialVersionUID=1L;
私有JRadioButton rdoLength=新JRadioButton(“长度”);
私有JRadioButton rdoArea=新JRadioButton(“区域”);
私有JRadioButton rdoVolume=新JRadioButton(“卷”);
私有JRadioButton rdoWeight=新JRadioButton(“重量”);
私人JRadioButton rdoLiquidVolume=新JRadioButton(“液体体积”);
ButtonGroup=新建ButtonGroup();
{
添加组(rdoLength);
添加组(rdoArea);
添加组(rdoVolume);
添加组(rdoWeight);
组。添加(rdoLiquidVolume);
rdoLength.setSelected(真);
}
CardLayout布局Card=新的CardLayout();
JPanel pnlCard=新的JPanel(布局卡);
公共GUI转换(){
//设置标题。
setTitle(“转换器”);
setLayout(新的BorderLayout());
//指定关闭按钮的操作。
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(buildSelectionPanel(),BorderLayout.NORTH);
getContentPane().add(pnlCard,BorderLayout.CENTER);
pnlCard.add(buildLengthPanel(),“LENGTH”);
pnlCard.add(buildAreaPanel(),“AREA”);
添加(buildVolumePanel(),“卷”);
pnlCard.add(buildWeightPanel(),“WEIGHT”);
pnlCard.add(buildLiquidVolumePanel(),“液体体积”);
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
展示(pnlCard,“长度”);
}
});
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
展示(pnlCard,“区域”);
}
});
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
展示(pnlCard,“卷”);
}
});
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
展示(pnlCard,“重量”);
}
});
rdoLiquidVolume.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
展示(pnlCard,“液体体积”);
}
});
包装();
//显示窗口。
setVisible(真);
}
专用JPanel buildSelectionPanel(){
JPanel panel=newjpanel(newflowlayout());
添加(新的JLabel(“请选择转换类型”);
面板。添加(rdoLength);
添加面板(rdoArea);
panel.add(rdoVolume);
面板。添加(rdoWeight);
面板。添加(rdoLiquidVolume);
返回面板;
}
私人JPanel buildLengthPanel(){
JPanel panel=newjpanel(newflowlayout());
字符串[]输入类型={“英寸”、“英尺”、“码”};
JComboBox cboInputType=新的JComboBox(输入类型);
JTextField txtInputLength=新的JTextField(10);
字符串[]convertTypes={“厘米”、“米”、“英尺”、“码”、“公里”};
JComboBox cboConvertType=新的JComboBox(convertTypes);
JTextField txtConvertLength=新的JTextField(10);
txtConvertLength.setEnabled(false);
JLabel lblConvertType=新的JLabel();
JButton BTnConConvertLength=新JButton(“转换”);
btnConvertLength.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
试一试{
txtConvertLength.setText(“”);
lblConvertType.setText(“”);
double inputLength=double.parseDouble(txInputLength.getText());
strInputType=(String)cboInputType.getSelectedItem();
String strConvertType=(String)cboConvertType.getSelectedItem();
双转换系数=0.0d;
如果(“英寸”。等于(strInputType)){
如果(“厘米”。等于(strConvertType)){
换算系数=2.54;
}否则,如果(“米”。等于(strConvertType)){
换算系数=0.0254;
}否则,如果(“公里数”。等于(strConvertType)){
换算系数=0.0000254;
}否则,如果(“英寸”。等于(strConvertType)){
换算系数=1.0;
}否则,如果(“英尺”。等于(strConvertType)){
换算系数=0.0833333;
}否则,如果(“码”。等于(strConvertType)){
换算系数=0.0277778;
}
}else如果(“英尺”。等于(strInputType)){
//一些转换
}否则,如果“码”。等于(strIn