采用java窗口编程建立一个简单的调查界面
1、1.打开IED:
所谓,IED就是java程序的编程界面

2、2.建立一个java工程
采用eclipse建立一个java工程,相信,大家都会了吧;
单击“file”-“new”-“java project”;
然后,选中工程,单击鼠标右键,在下拉菜单中选中“new”-“class”;

1、1.类的模型:
确定该类需要用的属性,和方法以及用到的包的声明,代码如下所示:
package Checkmod;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Checkmod extends JFrame implements ActionListener {
private JPanel jp=new JPanel();
private JCheckBox[] jcba={new JCheckBox("豫菜"),new JCheckBox("川菜"),new JCheckBox("粤菜"),new JCheckBox("西餐"),new JCheckBox("其他")};
private JRadioButton[] jrba={new JRadioButton("5~15岁"),new JRadioButton("16~35岁"),new JRadioButton("26~35岁"),new JRadioButton("36~45岁"),new JRadioButton("36~45岁"),new JRadioButton("46~55岁")};
private JButton[] jba={new JButton("确定"),new JButton("取消")};
private JLabel[] jla={new JLabel("年龄段:"),new JLabel("爱好:"),new JLabel("调查结果:")};
private ButtonGroup bg=new ButtonGroup();
public Checkmod(){
private JTextField jtf=new JTextField();
}
public void actionPerformed(ActionEvent e){
}
}
public static void main(String[] args) {
}
}

2、2.构造方法:
用来,对类进行的属性进行初始化的定义,在该例中是一个对窗口界面的初始化,代码如下:
public Checkmod(){
jp.setLayout(null);
for(int i=0;i<5;i++){
jrba[i].setBounds(40+i*100,40,80,30);
jcba[i].setBounds(40+i*120,100,120,30);
jp.add(jrba[i]);jp.add(jcba[i]);
jrba[i].addActionListener(this);
jcba[i].addActionListener(this);
bg.add(jrba[i]);
if(i>1)
continue;
jla[i].setBounds(20, 20+i*50, 80, 30);
jba[i].setBounds(400+i*120, 200, 80,26);
jp.add(jla[i]);jp.add(jba[i]);
jba[i].addActionListener(this);
}
jla[2].setBounds(20, 150, 120, 30);jp.add(jla[2]);
jtf.setBounds(120, 150, 500, 26);jp.add(jtf);
jtf.setEditable(false);
this.add(jp);this.setTitle("食物调查表");
this.setBounds(100, 100, 700, 280);
this.setVisible(true);this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

3、3.事件方法:
对监听到的数据进行处理,也就是对按钮触发的事件进行相关的相应,代码如下:
public void actionPerformed(ActionEvent e){
if(e.getSource()==jba[1]){
for(int i=0;i<jcba.length;i++){
jcba[i].setSelected(false);
jtf.setText("");
}
}else{
StringBuffer temp1=new StringBuffer("你是一个");
StringBuffer temp2=new StringBuffer();
for(int i=0;i<5;i++){
if(jrba[i].isSelected()){
temp1.append(jrba[i].getText());
}
if(jcba[i].isSelected()){
temp2.append(jcba[i].getText()+".");
}
}
if(temp2.length()==0){
jtf.setText("爱好为空???");
}else{
temp1.append("的人,比较喜欢");
temp1.append(temp2.substring(0, temp2.length()-1));
jtf.setText(temp1.append("。").toString());
}
}
}

4、4.main方法:
在本例中仅仅是参考实例化自己的类就可以了,代码如下:
new Checkmod();

1、1.编译运行:
单击“编译运行”按钮,可以看到如下界面。

2、2.查看结果:
选定,相关的按钮,如图所示,我们可以看到该按钮的效果。
仅供参考啊。
