用Java编写一个多选一的小程序
发布网友
发布时间:2022-05-25 13:23
我来回答
共1个回答
热心网友
时间:2023-11-03 07:57
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
public class Choose extends JFrame implements ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Label l1, l2;
TextField t1, t2;
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("QQ", checkboxGroup, false);
Checkbox checkbox2 = new Checkbox("MSN", checkboxGroup, false);
Checkbox checkbox3 = new Checkbox("ICQ", checkboxGroup, false);
Choice c;
public Choose() {
super("简单小程序");
this.setLayout(new FlowLayout());
l1 = new Label("选择你常用的软件:");
l2 = new Label("选择你喜欢的水果:");
checkbox1.addItemListener(this);
checkbox2.addItemListener(this);
checkbox3.addItemListener(this);
t1 = new TextField(20);
t2 = new TextField(20);
c = new Choice();
c.addItemListener(this);
c.add("苹果");
c.add("橘子");
c.add("香蕉");
c.add("梨子");
this.add(l1);
this.add(checkbox1);
this.add(checkbox2);
this.add(checkbox3);
this.add(t1);
this.add(l2);
this.add(c);
this.add(t2);
this.setSize(450, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new Choose();
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == checkbox1) {
t1.setText("你常用的软件是:" + checkbox1.getLabel());
}
if (e.getSource() == checkbox2) {
t1.setText("你常用的软件是:" + checkbox2.getLabel());
}
if (e.getSource() == checkbox3) {
t1.setText("你常用的软件是:" + checkbox3.getLabel());
}
t2.setText("你喜欢的水果是:" + c.getSelectedItem());// 得到选中的下拉列表值
}
}