求一段JAVA代码(实例代码)
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
new F();
}
}
class F extends JFrame implements ActionListener {
JCheckBox[] jCheckBoxs;
JRadioButton[] JRadioButtons;
ButtonGroup g;
JButton bt1,bt2;
F() {
this.setBounds(100, 300, 400, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new java.awt.GridLayout(3, 3));
jCheckBoxs = new JCheckBox[3];
jCheckBoxs[0] = new JCheckBox("足球", true);// 默认为选中
jCheckBoxs[1] = new JCheckBox("篮球");
jCheckBoxs[2] = new JCheckBox("羽毛球");
jCheckBoxs[0].addActionListener(this);
jCheckBoxs[1].addActionListener(this);
jCheckBoxs[2].addActionListener(this);
add(jCheckBoxs[0]);
add(jCheckBoxs[1]);
add(jCheckBoxs[2]);
JRadioButtons = new JRadioButton[3];
JRadioButtons[0] = new JRadioButton("足球", true);
JRadioButtons[1] = new JRadioButton("篮球");
JRadioButtons[2] = new JRadioButton("羽毛球");
JRadioButtons[0].addActionListener(this);
JRadioButtons[1].addActionListener(this);
JRadioButtons[2].addActionListener(this);
add(JRadioButtons[0]);
add(JRadioButtons[1]);
add(JRadioButtons[2]);
g = new ButtonGroup();
g.add(JRadioButtons[0]);
g.add(JRadioButtons[1]);
g.add(JRadioButtons[2]);
bt1 = new JButton("OK");
add(bt1);
bt2 = new JButton("Exit");
bt1.addActionListener(this);
bt2.addActionListener(this);
setVisible(true);
}
private String checkSelectJRadioButton(){
String str = "你选择的单选框为:\n";
for(int i=0;i<JRadioButtons.length;i++){
if(JRadioButtons[i].isSelected()){
str = str+JRadioButtons[i].getText();
break;
}
}
return str;
}
private List checkSelectJCheckBox(){
List list = new ArrayList();
int num = 0;
for(num=0;num<3;num++){
if(jCheckBoxs[num].isSelected()){
list.add(jCheckBoxs[num].getText());
}
}
return list;
}
private String message(){
List list = checkSelectJCheckBox();
String str = "您选择的复选框为:\n";
for(int i=0;i<list.size();i++){
str = str+list.get(i)+"\n";
}
return str;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("OK")) {
JOptionPane.showConfirmDialog(null, message()+checkSelectJRadioButton(), "提示!",
JOptionPane.OK_OPTION);
return;
}
if(e.getActionCommand().equals("Exit")){
System.exit(1);
}
}
}
是这种吗?
多重随机标签