2019.06.12

1.制作如图所示的界面,并实现功能.

package ZQ;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Q implements ActionListener {
	JFrame f;
	JPanel p;
	JLabel l1,l2,l3,l4;
	JTextField t;
	JRadioButton rb1,rb2;
	JCheckBox cb1,cb2,cb3;
	JButton b;
	JComboBox cb;
	JTextArea ta;
	ButtonGroup bg;
	JScrollPane sp;
	int sum=0;
	String []a={"常德","济南","烟台","潍坊","长沙"};
	int i=1;
	public Q() {
		f=new JFrame();
		ta=new JTextArea(50,40);
		sp=new JScrollPane(ta);
		p=new JPanel();
		t=new JTextField(5);
		b=new JButton("确定");
		b.addActionListener(this);
		rb1=new JRadioButton("男",false);
		rb2=new JRadioButton("女",false);
		cb1=new JCheckBox("体育",false);
		cb2=new JCheckBox("音乐",false);
		cb3=new JCheckBox("美术",false);
		cb=new JComboBox(a);
		
		l1=new JLabel("姓名:");
		l2=new JLabel("性别:");
		l3=new JLabel("爱好:");
		l4=new JLabel("籍贯:");
		l4=new JLabel("籍贯:");
		bg=new ButtonGroup();
		bg.add(rb1);
		bg.add(rb2);
		ta.setLineWrap(true);
		
		p.setLayout(null);
		p.setBounds(0, 0, 450, 390);
		f.setSize(450, 395);
		f.setVisible(true);;
		
		p.add(l1);
		p.add(l2);
		p.add(l3);
		p.add(l4);
		p.add(t);
		p.add(cb);
		p.add(cb1);
		p.add(cb2);
		p.add(cb3);
		p.add(rb1);
		p.add(rb2);
		p.add(b);
		p.add(sp);
		f.add(p);
		
		l1.setBounds(15, 10, 40, 20);
		t.setBounds(60, 10, 100, 25);
		l2.setBounds(15, 40, 40, 20);
		rb1.setBounds(60, 40, 40, 20);
		rb2.setBounds(100, 40, 40, 20);
		l3.setBounds(15, 70, 40, 20);
		cb1.setBounds(60, 70, 50, 20);
		cb2.setBounds(110, 70, 50, 20);
		cb3.setBounds(160, 70, 50, 20);
		l4.setBounds(15, 100, 40, 20);
		cb.setBounds(60, 100, 55, 20);
		b.setBounds(180, 130, 60, 30);
		sp.setBounds(0, 180,435,170);
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new Q();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		sum++;
		String s,ch1 = null,ch2 = "",ch3="",ch4="",ch5;
		String a="请输入姓名!",b="请选择性别!",c="请选择爱好";
		if(rb1.isSelected()) {
			ch1=rb1.getText();
		}else if(rb2.isSelected()){
			ch1=rb2.getText();
		}
		if(cb1.isSelected()) {
			ch2=cb1.getText();
		}
		if(cb2.isSelected()) {
			ch3=cb2.getText();
		}if(cb3.isSelected()){
			ch4=cb3.getText();
		}
		ch5=cb.getSelectedItem().toString();
		s=l1.getText()+t.getText()+"   "+l2.getText()+ch1+"   "+l3.getText()+ch2+" "+ch3+" "+ch4+"   "+l4.getText()+ch5+"\n";
		if(t.getText().length()!=0) {
			if(ch1!=null) {
				if(ch2!=""||ch3!=""||ch4!=""){
					ta.append("第"+i+"名同学的输入信息为:\n");
					ta.append(s);
					i++;
					t.setText(null);
					bg.clearSelection();
					cb1.setSelected(false);
					cb2.setSelected(false);
					cb3.setSelected(false);
				}else {
					ta.append(c+"\n");
				}
			}else {
				ta.append(b+"\n");
			}
		}else {
			ta.append(a+"\n");
		}
	}
}

posted @ 2019-06-12 17:11  z全  阅读(91)  评论(0)    收藏  举报