创建一个信息栏,输入和选择自己的信息,将信息显示在文本框内。
代码:
public class test {
public static void main(String args[]) {
ComponentInWindow win=new ComponentInWindow();
win.setTitle("个人信息");
win.setBounds(100,100,310,260);
}
}
import java.awt.*;
import javax.swing.*;
public class ComponentInWindow extends JFrame {
JTextField textfield;//文本框
JButton button;//按钮
JRadioButton radioM,radioF;//单选按钮
ButtonGroup group;//单选按钮组
JComboBox comboBox1,comboBox2;//下拉列表
JCheckBox checkbox1,checkbox2,checkbox3;//复选框
JTextArea textshow;//文本区域
OperatorListener operator;//监视ItemEvent事件的监视器
ComputerListener computer;//监视ActiongEvent事件的监视器
public ComponentInWindow() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout());
add(new JLabel("姓名:"));
textfield=new JTextField(10);
add(textfield);
add(new JLabel("按钮:"));
button=new JButton("确定");
add(button);
add(new JLabel("性别:"));
group=new ButtonGroup();
radioM=new JRadioButton("男");
radioF=new JRadioButton("女");
group.add(radioM);
group.add(radioF);
add(radioM);
add(radioF);
add(new JLabel("系别:"));
comboBox1=new JComboBox();
comboBox1.addItem("数学与统计学院");
comboBox1.addItem("外国语学院");
comboBox1.addItem("体育学院");
add(comboBox1);
add(new JLabel("专业:"));
comboBox2=new JComboBox();
comboBox2.addItem("统计学");
comboBox2.addItem("数学与应用数学");
comboBox2.addItem("信息与计算科学");
add(comboBox2);
add(new JLabel("爱好:"));
checkbox1=new JCheckBox("喜欢音乐");
checkbox2=new JCheckBox("喜欢旅游");
checkbox3=new JCheckBox("喜欢篮球");
add(checkbox1);
add(checkbox2);
add(checkbox3);
textshow=new JTextArea(10,20);
operator=new OperatorListener();
computer=new ComputerListener();
operator.setJComboBox(comboBox1,comboBox2);
operator.setJCheckBox(checkbox1,checkbox2,checkbox3);
operator.setWorkTogether(computer);
computer.setJTextField(textfield);
computer.setJRadioButton1(radioM);
computer.setJRadioButton2(radioF);
computer.setJTextArea(textshow);
textfield.addActionListener(computer);
button.addActionListener(computer);
comboBox1.addItemListener(operator);
comboBox2.addItemListener(operator);
checkbox1.addItemListener(operator);
checkbox2.addItemListener(operator);
checkbox3.addItemListener(operator);
add(new JLabel("个人信息:"));
add(textshow);
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class ComputerListener implements ActionListener {
JRadioButton radioM,radioF;
JTextField textfield;
JTextArea textshow;
String xueyuan,zhuanye,aihao1,aihao2,aihao3;
public void setJTextField(JTextField s) {
textfield=s;
}
public void setJRadioButton1(JRadioButton g) {
radioM=g;
}
public void setJRadioButton2(JRadioButton g) {
radioF=g;
}
public void setJTextArea(JTextArea s) {
textshow=s;
}
public void setaihao1(String s){
aihao1=s;
}
public void setaihao2(String s){
aihao2=s;
}
public void setaihao3(String s){
aihao3=s;
}
public void setxueyuan(String s) {
xueyuan=s;
}
public void setzhuanye(String s) {
zhuanye=s;
}
public void actionPerformed(ActionEvent e){
String name=textfield.getText();
String sex;
if(radioM.isSelected()){
sex = radioM.getText();
}
else if(radioF.isSelected()) {
sex = radioF.getText();
}
else {
sex = "";
}
textshow.append("名字:"+name+"\n"+"性别:"+sex+"\n"+"学院:"+xueyuan+"\n"+"专业:"+zhuanye+"\n"+"爱好:"+aihao1+aihao2+aihao3);
}
}
import java.awt.event.*;
import javax.swing.*;
public class OperatorListener implements ItemListener {
JComboBox comBox1,comBox2;
JCheckBox aihao1,aihao2,aihao3;
ComputerListener workTogether;
public void setJComboBox(JComboBox a,JComboBox b) {
comBox1=a;
comBox2=b;
}
public void setJCheckBox(JCheckBox a,JCheckBox b,JCheckBox c) {
aihao1=a;
aihao2=b;
aihao3=c;
}
public void setWorkTogether(ComputerListener computer){
workTogether=computer;
}
public void itemStateChanged(ItemEvent e) {
String xueyuan=comBox1.getSelectedItem().toString();
String zhuanye=comBox2.getSelectedItem().toString();
String a1=null;
String a2=null;
String a3=null;
if(aihao1.isSelected()){
a1=aihao1.getText();
}
else{
a1="";
}
if(aihao2.isSelected()){
a2=aihao2.getText();
}
else{
a2="";
}
if(aihao3.isSelected()){
a3=aihao3.getText();
}
else{
a3="";
}
workTogether.setxueyuan(xueyuan);
workTogether.setzhuanye(zhuanye);
workTogether.setaihao1(a1);
workTogether.setaihao2(a2);
workTogether.setaihao3(a3);
}
}