Java | อยากให้ JRadioButton ติ๊กได้แค่อันเดียว พอไปติ๊กอีกอันอันเดิมจะหายไป เป็นอย่างนี้ไปเรื่อยๆ ช่วยทีครับ

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;

public class C2F extends JPanel{
    JTextField tfInputTemp;
    JLabel lInputTemp, lAnswer;
    JButton bCalc;
    JPanel InputTemp, collections;
    JRadioButton radButton1, radButton2;
    JOptionPane op;
    ButtonGroup group1, group2;
    
    public C2F(){
        tfInputTemp = new JTextField(15);
        lInputTemp = new JLabel("Enter your Data: ");
        lAnswer = new JLabel("Answer: ");
        bCalc = new JButton("Convert");
        InputTemp = new JPanel();
        group1 = new ButtonGroup();
        group2 = new ButtonGroup();
        group1.add(radButton1 = new JRadioButton("Celsius"));
        group2.add(radButton2 = new JRadioButton("Fahrenheit"));
        //radButton1 = new JRadioButton("Celsius");
        //radButton2 = new JRadioButton("Fahrenheit");
        collections = new JPanel();
        collections.setLayout(new BoxLayout(collections, BoxLayout.Y_AXIS));
        InputTemp.add(lInputTemp);
        InputTemp.add(tfInputTemp);
        collections.add(radButton1);
        collections.add(radButton2);
        collections.add(InputTemp);
        collections.add(bCalc);
        collections.add(lAnswer);
        bCalc.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(radButton1.isSelected()){
                    double temp1 = Double.parseDouble(tfInputTemp.getText());
                    double fahrenheit = ((temp1 * 9) / 5) + 32;
                    lAnswer.setText("Answer: " + fahrenheit + " F");
                }
                else if(radButton2.isSelected()){
                    double temp2 = Double.parseDouble(tfInputTemp.getText());
                    double celsius = ((temp2 - 32) * 5) / 9;
                    lAnswer.setText("Answer: " + celsius + " C");
                }
                else if(tfInputTemp.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "Please input data !");
                }
                else{
                    JOptionPane.showMessageDialog(null, "Please check accuracy !");
                }
            }
        });
        add(radButton1);
        add(radButton2);
        add(collections);
    }
    public static void main(String[] args){
        JFrame c2fFrame = new JFrame("Convert to Celsius & Fahrenheit");
        C2F c2f = new C2F();
        c2fFrame.add(c2f);
        c2fFrame.setSize(400, 150);
        c2fFrame.setLocation(600, 375);
        c2fFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c2fFrame.setVisible(true);
    }
}
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่