java中怎么编写多项选择题代码

如题所述

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());// 得到选中的下拉列表值

 }

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-10
这个涉及GUI,控件的监听事件
第2个回答  2015-05-10
你是前台用jsp还是gui?jsp我这里倒是有现成的追问

我刚学,不知道是哪个

追答

jsp就是网页类型的,gui就是安装了软件(QQ)那样的就是弹窗口的

追问

我用eclipse编写的

追答

那就是gui了

追问

你怎么写呢

那怎么编写呢

第3个回答  2015-05-10
前台是什么呢追问

jsp

错了,是GUI

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网