java选择框、单选框和单选按钮
选择框、单选框和单选按钮都是选择组件,选择组件有两种状态,⼀种是选中(on),另⼀种是未选中(off),它们提供⼀种简单的 “on/off”选择功能,让⽤户在⼀组选择项⽬中作选择。
选择框
选择框(JCheckBox)的选中与否开状是⼀个⼩⽅框,被选中则在框中打勾。当在⼀个容器中有多个选择框,同时可以有多个选择框被选中,这样的选择框也称复选框。与选择框相关的接⼝是ItemListener,事件类是ItemEvent。
JCheckBox类常⽤的构造⽅法有以下3个:
1.JCheckBox():⽤空标题构造选择框。
2.JCheckBox(String s):⽤给定的标题s构造选择框。
3.JCheckBox(String s, boolean b):⽤给定的标题s构造选择框,参数b设置选中与否的初始状态。
JCheckBox类的其他常⽤⽅法如下:
2.setState(boolean b):设置选择框的状态
4.setLabel(String s):设置选择框的标题。
5.isSelected():获取选择框是否被选中的状态。
6.itemStateChanged(ItemEvent e):处理选择框事件的接⼝⽅法。
8.addItemListener(ItemListener l):为选择框设定监视器。
【例 11-11】声明⼀个⾯板⼦类,⾯板⼦类对象有3个选择框。
class Panel1 extends JPanel{
JCheckBox box1,box2,box3;
Panel1(){
box1 = new JCheckBox(“⾜球”);
box2 = new JCheckBox(“排球”);
box2 = new JCheckBox(“篮球”);
}
}
单选框
当在⼀个容器中放⼊多个选择框,且没有ButtonGroup对象将它们分组,则可以同时选中多个选择框。如果使⽤ButtonGroup 对象将选择框分组,同⼀时刻组内的多个选择框只允许有⼀个被选中,称同⼀组内的选择框为单选框。单选框分组的⽅法是先创建ButtonGroup对象,然后将希望为同组的选择框添加到同⼀个ButtonGroup对象中。参见例6.2程序的⾯板⼦类Panel2的声明,组内有3个单选框。
单选按钮
单选按钮(JRadioButton)的功能与单选框相似。使⽤单选按钮的⽅法是将⼀些单选按钮⽤ButtonGroup对象分组,使同⼀组的单选按钮只允许有⼀个被选中。单选按钮与单选框的差异是显⽰的样式不同,单选按钮是⼀个圆形的按钮,单选框是⼀个⼩⽅框。
JRadioButton类的常⽤构造⽅法有以下⼏个:
1.JRadioButton():⽤空标题构造单选按钮。
2.JRadioButton(String s):⽤给定的标题s构造单选按钮。
3.JRadioButton(String s,boolean b):⽤给定的标题s构造单选按钮,参数b设置选中与否的初始状态。
单选按钮使⽤时需要使⽤ButtonGroup将单选按钮分组,单选按钮的分组⽅法是先创建对象,然后将同组的单选按钮添加到同⼀个ButtonGroup对象中。参见例6.2程序的⼦类panel1的声明,组内有3个单选按钮。
选择项⽬事件处理
⽤户对选择框或单选按钮做出选择后,程序应对这个选择作出必要的响应,程序为此要处理选择项⽬事件。选择项⽬处理程序的基本内容有:
1.监视选择项⽬对象的类要实现接⼝ItemListener,
2.程序要声明和建⽴选择对象,
3.为选择对象注册监视器,
4.编写处理选择项⽬事件的接⼝⽅法itemStateChanged(ItemEvent e),在该⽅法内⽤getItemSelectable()⽅法获取事件源,并作相应处理。
【例 11-12】处理选择项⽬事件的⼩应⽤程序。⼀个由3个单选按钮组成的产品选择组,当选中某个产品时,⽂本区将显⽰该产品的信息。⼀个由3个选择框组成的购买产品数量选择框组,当选择了购买数量后,在另⼀个⽂本框显⽰每台价格。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Panel1 extends JPanel{
JRadioButton box1,box2,box3;
ButtonGroup g;
Panel1(){
setLayout(new GridLayout(1,3));
g = new ButtonGroup();
box1 = new JRadioButton(MyWindow.fName[0]+"计算机",false);
box2 = new JRadioButton(MyWindow.fName[1]+"计算机",false);
box3 = new JRadioButton(MyWindow.fName[2]+"计算机",false);
g.add(box1);g.add(box2);g.add(box3);
add(box1);add(box2);add(box3);
add(new JLabel("计算机3选1") );
}
}
class Panel2 extends JPanel{
JCheckBox box1,box2,box3;
ButtonGroup g;
Panel2(){
setLayout(new GridLayout(1,3));
g = new ButtonGroup();
box1 = new JCheckBox("购买1台 ");
box2 = new JCheckBox("购买2台 ");
box3 = new JCheckBox("购买3台 ");
g.add(box1);g.add(box2);g.add(box3);
add(box1);add(box2);add(box3);
add(new JLabel(" 选择1、2或3"));
}
}
class MyWindow extends JFrame implements ItemListener{
Panel1 panel1;
Panel2 panel2;
JLabel label1,label2;
JTextArea text1,text2;
static String fName[] = {"HP","IBM","DELL"};
static double priTbl[][]={{1.20,1.15,1.10},{1.70,1.65,1.60},{1.65,1.60,1.58}};
static int productin = -1;
MyWindow(String s){
super(s);
Container con = ContentPane();
con.setLayout(new GridLayout(3,2));
this.setLocation(100,100);
this.setSize(400,100);
panel1 = new Panel1();panel2 = new Panel2();
label1 = new JLabel("产品介绍",JLabel.CENTER);
label2 = new JLabel("产品价格",JLabel.CENTER);
text1 = new JTextArea();text2 = new JTextArea();
con.add(label1);con.add(label2);con.add(panel1);
con.add(panel2);con.add(text1);con.add(text2);
panel1.box1.addItemListener(this);
panel1.box2.addItemListener(this);
panel1.box3.addItemListener(this);
panel2.box1.addItemListener(this);
panel2.box2.addItemListener(this);
panel2.box3.addItemListener(this);
this.setVisible(true);this.pack();
}
public void itemStateChanged(ItemEvent e){ //选项状态已改变
ItemSelectable()==panel1.box1){ //获取可选项
production =0;
text1.setText(fName[0]+"公司⽣产");text2.setText("");
}
checkbox和radiobutton的区别
else ItemSelectable()==panel1.box2){
production =1;
text1.setText(fName[1]+"公司⽣产");text2.setText("");
}
else ItemSelectable()==panel1.box3){
production =2;
text1.setText(fName[2]+"公司⽣产");text2.setText("");
}
else{
if(production ==-1) return;
ItemSelectable()==panel2.box1){
text2.setText(""+priTbl[production][0]+"万元/台");
}
else ItemSelectable()==panel2.box2){
text2.setText(""+priTbl[production][1]+"万元/台");
}
else ItemSelectable()==panel2.box3){
text2.setText(""+priTbl[production][2]+"万元/台");
}
}
}
}
public class Example6_2 extends Applet{
MyWindow myWin = new MyWindow("选择项⽬处理⽰例程序"); }
以上所述就是本⽂的全部内容了,希望⼤家能够喜欢。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。