java学生信息输入窗口

源代码

package Experiments;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Experiment71 {
     
    public static void main(String[] args) {
     
        new Student2().display();
    }
}

class Student2 implements ActionListener, ItemListener{
     

    JFrame f;
    JTextField number, name;
    JRadioButton sex1, sex2;
    JComboBox dept, major;  //系别专业 组合框
    List info;
    JButton addInfo, deleteInfo;

    public void display(){
     
        JPanel p1, p2, p3;
        ButtonGroup cg;
        f = new JFrame("学生信息输入窗口");
        f.setBounds(200, 140, 480, 200);
        f.setBackground(Color.lightGray);
        f.setLayout(new GridLayout(1, 2));
        info = new List();  //列表框
        f.add(info);

        p1 = new JPanel();
        p1.setLayout(new GridLayout(6, 1));
        f.add(p1);
        number = new JTextField("学号");
        name = new JTextField("姓名");
        cg = new ButtonGroup();
        sex1 = new JRadioButton("男", true);
        sex2 = new JRadioButton("女", false);
        cg.add(sex1);
        cg.add(sex2);
        dept = new JComboBox();
        dept.addItem("计算机系");
        dept.addItem("金融系");
        dept.addItemListener(this);
        major = new JComboBox();
        major.addItem("软件工程");
        major.addItem("计算机科学与技术");
        addInfo = new JButton("增加");
        deleteInfo = new JButton("删除");
        addInfo.addActionListener(this);
        deleteInfo.addActionListener(this);

        p1.add(number);
        p1.add(name);

        p2 = new JPanel();
        p2.setLayout(new GridLayout(1, 2));
        p2.add(sex1);
        p2.add(sex2);

        p1.add(p2);
        p1.add(dept);
        p1.add(major);

        p3 = new JPanel();
        p3.setLayout(new GridLayout(1, 2));
        p3.add(addInfo);
        p3.add(deleteInfo);

        p1.add(p3);


        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
     
        if(e.getSource() == addInfo){
     
            String str;
            str = number.getText() + " " + name.getText();

            if(sex1.isSelected())
                str += " " + sex1.getText();
            if(sex2.isSelected())
                str += " " + sex2.getText();

            str += " " + dept.getSelectedItem();
            str += " " + major.getSelectedItem();

            info.add(str);
        }
        if(e.getSource() == deleteInfo){
     
            if(info.getSelectedIndex() >= 0)
                info.remove(info.getSelectedIndex());
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
     
        if(dept.getSelectedIndex() == 0){
     
            major.removeAllItems();
            major.addItem("软件工程");
            major.addItem("计算机科学与技术");
        }
        if(dept.getSelectedIndex() == 1){
     
            major.removeAllItems();
            major.addItem("国际金融");
            major.addItem("金融理财");
        }
    }
}

运行结果

java学生信息输入窗口_第1张图片

你可能感兴趣的:(java,java图形用户界面,swing,java,JAVA,图形用户界面设计,源代码)