import java.awt.Dimension;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class RadioButtonDemo {
JRadioButton manRadioBuuon, womanRadioButton;
JLabel label;
JFrame frame;
JPanel contentPane;
private final JRadioButton manRadioButton;
public RadioButtonDemo() {
frame = new JFrame("RadioButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
frame.setContentPane(contentPane);
label = new JLabel();
label.setText("性别:");
manRadioButton = new JRadioButton("男");
manRadioButton.setMnemonic(KeyEvent.VK_M);
manRadioButton.setSelected(true);
womanRadioButton = new JRadioButton("女");
womanRadioButton.setMnemonic(KeyEvent.VK_W);
ButtonGroup group = new ButtonGroup();
group.add(manRadioButton);
group.add(womanRadioButton);
}
public void createAndShowGUI() {
contentPane.add(label);
contentPane.add(manRadioButton);
contentPane.add(womanRadioButton);
frame.setLocationRelativeTo(label);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RadioButtonDemo().createAndShowGUI();
}
});
}
}
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class RadioButtonDemo {
JRadioButton manRadioBuuon, womanRadioButton;
JLabel label;
JFrame frame;
JPanel contentPane;
private final JRadioButton manRadioButton;
public RadioButtonDemo() {
frame = new JFrame("RadioButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
frame.setContentPane(contentPane);
label = new JLabel();
label.setText("性别:");
manRadioButton = new JRadioButton("男");
manRadioButton.setMnemonic(KeyEvent.VK_M);
manRadioButton.setSelected(true);
womanRadioButton = new JRadioButton("女");
womanRadioButton.setMnemonic(KeyEvent.VK_W);
ButtonGroup group = new ButtonGroup();
group.add(manRadioButton);
group.add(womanRadioButton);
}
public void createAndShowGUI() {
contentPane.add(label);
contentPane.add(manRadioButton);
contentPane.add(womanRadioButton);
frame.setLocationRelativeTo(label);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RadioButtonDemo().createAndShowGUI();
}
});
}
}