-1

感谢您抽出宝贵时间阅读本文,但我会让它变得简单。我遇到的问题是我有一个程序可以改变一种温度并将其转换为另一种温度。我要添加到程序中的是,当用户单击按钮输入时,当用户单击键盘上的“输入”时,它会在另一个框中显示新温度。这是下面的代码:

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.InputMismatchException;


public class Testlab {

    static String celciusString = "Celcius";
    static String fahrenheitString = "Fahrenheit";
    static String kelvinString = "Kelvin";

    static String celcius1String = "Celcius2";
    static String fahrenheit1String = "Fahrenheit2";
    static String kelvin1String = "Kelvin2";

    private JLabel in = new JLabel("Input Scale");
    private JLabel out = new JLabel("Output Scale");

    private JLabel in1 = new JLabel("Input");
    private JLabel out1 = new JLabel("Output");

    private JTextField input = new JTextField(7);
    private JTextField output = new JTextField(7);

    private JFrame f = new JFrame("BasicGui"); // Create Frame
    private JPanel pnlNorth = new JPanel(); //North Quadrant
    private JPanel pnlSouth = new JPanel(); //South Quadrant
    private JPanel pnlEast = new JPanel(); //East Quadrant
    private JPanel pnlWest = new JPanel(); // West Quadrant
    private JPanel pnlCenter = new JPanel(); // Center Quadrant


    private JRadioButton celciusButton = new JRadioButton(celciusString);
    private JRadioButton fahrenheitButton = new JRadioButton(fahrenheitString);
    private JRadioButton kelvinButton = new JRadioButton(kelvinString);

    private JRadioButton celcius1Button = new JRadioButton(celciusString);
    private JRadioButton fahrenheit1Button = new JRadioButton(fahrenheitString);
    private JRadioButton kelvin1Button = new JRadioButton(kelvinString);

    //Menu
    private JMenuBar mb = new JMenuBar(); // Menu Bar
    private JMenu mnuFile = new JMenu("File"); // File Entry of Menu Bar
    private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit Sub item
    private JMenu mnuHelp = new JMenu("Help"); //Help Menu Entry
    private JMenuItem mnuItemAbout = new JMenuItem("About"); //About Entry

    //Constructor for GUI
    public Testlab(){
        //Set Menubar
        pnlEast.setLayout(new GridLayout (4,1));
        pnlWest.setLayout(new GridLayout (4,1));

        f.setJMenuBar(mb);

        //Radio Button --New
        celciusButton.setMnemonic(KeyEvent.VK_C);
        celciusButton.setActionCommand(celciusString);
        celciusButton.setSelected(true);

        fahrenheitButton.setMnemonic(KeyEvent.VK_F);
        fahrenheitButton.setActionCommand(fahrenheitString);

        kelvinButton.setMnemonic(KeyEvent.VK_K);
        kelvinButton.setActionCommand(kelvinString);

        celcius1Button.setMnemonic(KeyEvent.VK_C);
        celcius1Button.setActionCommand(celcius1String);
        celcius1Button.setSelected(true);

        fahrenheit1Button.setMnemonic(KeyEvent.VK_F);
        fahrenheit1Button.setActionCommand(fahrenheit1String);

        kelvin1Button.setMnemonic(KeyEvent.VK_K);
        kelvin1Button.setActionCommand(kelvin1String);

        //Group the radio Buttons.

        ButtonGroup group = new ButtonGroup();
        group.add(celciusButton);
        group.add(fahrenheitButton);
        group.add(kelvinButton);

        ButtonGroup group2 = new ButtonGroup();
        group2.add(celcius1Button);
        group2.add(fahrenheit1Button);
        group2.add(kelvin1Button);


        //Register A listener for the radio Buttons.
        celciusButton.addActionListener(new RadioButtonEvent());
        fahrenheitButton.addActionListener(new RadioButtonEvent());
        kelvinButton.addActionListener(new RadioButtonEvent());

        celcius1Button.addActionListener(new RadioButtonEvent());
        fahrenheit1Button.addActionListener(new RadioButtonEvent());
        kelvin1Button.addActionListener(new RadioButtonEvent());


        //Build Menus
        mnuFile.add(mnuItemQuit); //Create Quit Line
        mnuHelp.add(mnuItemAbout); //Create About Line
        mb.add(mnuFile);        // Add Menu Items to form
        mb.add(mnuHelp);

        //Add Buttons
        //pnlNorth.add(btnNorth);

        pnlNorth.add(in1);
        pnlSouth.add(out1);
        pnlNorth.add(input);
        pnlSouth.add(output);

        pnlWest.add(in);
        pnlWest.add(celciusButton);
        pnlWest.add(fahrenheitButton);
        pnlWest.add(kelvinButton);

        pnlEast.add(out);
        pnlEast.add(celcius1Button);
        pnlEast.add(fahrenheit1Button);
        pnlEast.add(kelvin1Button);


        //Setup Main Frame
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(pnlNorth, BorderLayout.NORTH);
        f.getContentPane().add(pnlSouth,BorderLayout.SOUTH );
        f.getContentPane().add(pnlEast,BorderLayout.EAST );
        f.getContentPane().add(pnlWest,BorderLayout.WEST );
        f.getContentPane().add(pnlCenter,BorderLayout.CENTER);

        //Allows the Swing App to be closed
        f.addWindowListener(new ListenCloseWdw());

        // Add Menu Listener
        mnuItemQuit.addActionListener(new ListenMenuQuit());
    }

    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }   

    }



    //NEW STUFF TO ADD
    public class RadioButtonEvent implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String action = e.getActionCommand();

            int temperture;
            double newtemperture = 0.00;
            try {
            temperture = Integer.parseInt(input.getText());

        if (celciusButton.isSelected() & celcius1Button.isSelected())

             newtemperture = temperture;


        else if (celciusButton.isSelected() & fahrenheit1Button.isSelected())

            newtemperture = ((temperture * 1.8)) + 32;

            else if (celciusButton.isSelected() & kelvin1Button.isSelected())

                newtemperture = temperture + 273.15;

            else if (fahrenheitButton.isSelected() & celcius1Button.isSelected())

                newtemperture = (temperture - 32) * .55555555555;

            else if (fahrenheitButton.isSelected() & fahrenheit1Button.isSelected())

                newtemperture = temperture;

            else if (fahrenheitButton.isSelected() & kelvin1Button.isSelected())

                newtemperture = (temperture + 459.67) * 5/9;

            else if (kelvinButton.isSelected() & celcius1Button.isSelected())

                newtemperture = (temperture - 273.15);

            else if (kelvinButton.isSelected() & fahrenheit1Button.isSelected())

                newtemperture = (temperture * 9/5) - 459.67;

            else if (kelvinButton.isSelected() & kelvin1Button.isSelected())

                newtemperture = temperture;


            } catch (Exception a) {
                JOptionPane.showMessageDialog(null, "You did not input a value");
            }



         output.setText(new DecimalFormat("##.##").format(newtemperture));
            }
    }



    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);

        }
    }

    public void launchFrame(){
        //Display Frame
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack(); // Adjusting panel to components for Display
        f.setVisible(true);
    }
    public static void main(String args[]){

        Testlab gui = new Testlab();
        gui.launchFrame();
    }
}
4

1 回答 1

3

只需添加Testlab()

input.addActionListener(new RadioButtonEvent());

也不要为每个组件创建相同的 ActionListener。您只能创建一个并将其注册到任意数量的组件。

于 2013-07-31T15:45:21.090 回答