共計(jì) 806 個字符,預(yù)計(jì)需要花費(fèi) 3 分鐘才能閱讀完成。
在 Java 中,可以使用 isSelected() 方法來判斷單選框是否被選中。
以下是一個示例代碼:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RadioButtonExample extends JFrame implements ActionListener {
JRadioButton radioButton;
JButton button;
public RadioButtonExample() {setTitle(" 單選框示例 ");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
radioButton = new JRadioButton(" 選項(xiàng) ");
button = new JButton(" 判斷選中 ");
button.addActionListener(this);
JPanel panel = new JPanel();
panel.add(radioButton);
panel.add(button);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {if (radioButton.isSelected()) {System.out.println(" 單選框選中 ");
} else {System.out.println(" 單選框未選中 ");
}
}
public static void main(String[] args) {new RadioButtonExample();}
}
在上述代碼中,使用 isSelected() 方法來判斷單選框是否選中。在 actionPerformed(ActionEvent e) 方法中,根據(jù) isSelected() 的返回值來輸出相應(yīng)的信息。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完