checkAndRadio.zip

  • 丁军峰
    了解作者
  • Java
    开发工具
  • 1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2015-02-10 18:39
    上传日期
JAVA基础课,了解单选、多选按钮必备代码
checkAndRadio.zip
  • checkAndRadio.java
    2.5KB
内容介绍
import java.awt.event.*; import javax.swing.*; public class checkAndRadio extends JFrame implements ActionListener { private JPanel jp=new JPanel(); private JCheckBox[] jcbArray= { new JCheckBox("internet"), new JCheckBox("sport"), new JCheckBox("car"), new JCheckBox("victry"), new JCheckBox("fashion") }; private JRadioButton[] jrbArray= { new JRadioButton("5~14"), new JRadioButton ("16~25",true), new JRadioButton ("26~35"), new JRadioButton ("36~45"), new JRadioButton ("46~55") }; private JButton[] jbArray={new JButton("up"),new JButton("clear")}; private JLabel[] jlArray= {new JLabel("age"),new JLabel("hobes"),new JLabel("end")}; private JTextField jtf=new JTextField(); private ButtonGroup bg=new ButtonGroup(); public checkAndRadio() { jp.setLayout(null); for(int i=0;i<5;i++) { jrbArray[i].setBounds(40+i*100,40,80,30); jcbArray[i].setBounds(40+i*120,100,120,30); jp.add(jrbArray[i]); jp.add(jcbArray[i]); jrbArray[i].addActionListener(this); jcbArray[i].addActionListener(this); bg.add(jrbArray[i]); if(i>1)continue; jlArray[i].setBounds(20,20+i*50,80,30); jbArray[i].setBounds(400+i*120,200,80,26); jp.add(jlArray[i]); jp.add(jbArray[i]); jbArray[i].addActionListener(this); } jlArray[2].setBounds(20,150,120,30); jp.add(jlArray[2]); jtf.setBounds(120,150,500,26); jp.add(jtf); jtf.setEditable(false); this.add(jp); this.setTitle("information");////////////// this.setBounds(100,100,700,280); this.setVisible(true); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource()==jbArray[1]) { bg.clearSelection(); for(int i=0;i<jcbArray.length;i++) jcbArray[i].setSelected(false); jtf.setText(""); } else { StringBuffer temp1=new StringBuffer("you are a");/////////// StringBuffer temp2=new StringBuffer(); for(int i=0;i<5;i++) { if(jrbArray[i].isSelected()) { temp1.append(jrbArray[i].getText()); } if(jcbArray[i].isSelected()) { temp2.append(jcbArray[i].getText()+","); } } if(temp2.length()==0) { jtf.setText("intresting can not be null"); } else { temp1.append("s,you'd like");//// temp1.append(temp2.substring(0,temp2.length()-1)); jtf.setText(temp1.append(".").toString()); } } } public static void main(String[] args) { new checkAndRadio(); } }
评论
    相关推荐