使用单选按钮在Java中设置标签

时间:2013-11-28 03:23:56

标签: java swing jradiobutton

因此,对于课程我想要制作一个华氏度到华氏度的转换器GUI。话虽如此,这是一个非常简单易用的程序。我想用它做更多的事情。我想要做的是有一个窗口根据在buttongroup中选择的单选按钮而改变。我希望选择的“To Fahrenheit”或“To Celsius”的radiobutton更新输入和输出的标签。我已经尝试使用actionlistener来选择一个,但它不会更改标签。我使用了错误的听众吗?这样做的正确方法是什么?

这是我的代码:

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


public class Converter extends JFrame {

private JPanel contentPane;
private JTextField tfNumber;
private JLabel lblCelsius;
private JLabel lblFahrenheit;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Converter frame = new Converter();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Converter() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }

    setTitle("Celsius Converter");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 308, 145);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    tfNumber = new JTextField();
    tfNumber.setText("0");
    tfNumber.setBounds(10, 11, 123, 20);
    contentPane.add(tfNumber);
    tfNumber.setColumns(10);

    lblCelsius = new JLabel("Celsius");
    lblCelsius.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblCelsius.setBounds(143, 12, 127, 14);
    contentPane.add(lblCelsius);

    lblFahrenheit = new JLabel("Fahrenheit");
    lblFahrenheit.setBounds(187, 46, 95, 14);
    contentPane.add(lblFahrenheit);

    final JLabel lblNum = new JLabel("32.0");
    lblNum.setBounds(143, 46, 43, 14);
    contentPane.add(lblNum);

    final JRadioButton rdbtnF = new JRadioButton("to Fahrenheit");
    rdbtnF.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rdbtnF.isSelected()) {
                lblCelsius.setText("Celsius");
                lblFahrenheit.setText("Fahrenheit");
            }
        }
    });
    rdbtnF.setSelected(true);
    rdbtnF.setBounds(10, 72, 109, 23);
    contentPane.add(rdbtnF);

    final JRadioButton rdbtnC = new JRadioButton("to Celsius");
    rdbtnC.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rdbtnF.isSelected()) {
                lblCelsius.setText("Fahrenheit");
                lblFahrenheit.setText("Celsius");
            }
        }
    });
    rdbtnC.setBounds(121, 72, 109, 23);
    contentPane.add(rdbtnC);

    ButtonGroup bg = new ButtonGroup();
    bg.add(rdbtnF);
    bg.add(rdbtnC);



    JButton btnConvert = new JButton("Convert");
    btnConvert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rdbtnF.isSelected()) {
                String num = String.valueOf(convertToF(tfNumber.getText()));
                lblNum.setText(num);
            }
            if(rdbtnC.isSelected()) {
                String num = String.valueOf(convertToC(tfNumber.getText()));
                lblNum.setText(num);
            }
        }
    });
    btnConvert.setBounds(10, 42, 123, 23);
    contentPane.add(btnConvert);
}

private double convertToF(String celsius) {
    double c = Double.parseDouble(celsius);
    double fahren = c * (9/5) + 32;
    return fahren;
}

private double convertToC(String fahren) {
    double f = Double.parseDouble(fahren);
    double celsius = (f - 32) * 5 / 9;
    return celsius;
    }
}

0 个答案:

没有答案