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 JTextField jtf=new JTextField(); private ButtonGroup bg=new ButtonGroup(); 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); } public static void main(String[] args) { // TODO Auto-generated method stub new Checkmod(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub 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()); } } } }
来源连接:https://jingyan.baidu.com/article/f79b7cb319780c9144023ec1.html