Swing 消息录入

package Swing;

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

@SuppressWarnings({ "serial", "unused" })
public class Message1 extends JFrame {
    JFrame jf = new JFrame("信息录入");
    JPanel p = new JPanel();
    JLabel l = new JLabel("姓名:");
    JLabel l1 = new JLabel("性别: ");
    JLabel l2 = new JLabel("爱好:");
    JLabel l3 = new JLabel("籍贯:");
    ButtonGroup bg = new ButtonGroup();
    JRadioButton b = new JRadioButton("");
    JRadioButton b1 = new JRadioButton("");
    JCheckBox fx[] = new JCheckBox[4];
    JButton jb = new JButton("肯定");
    JButton jb1 = new JButton("下一位");
    JTextField t = new JTextField();
    JTextArea t1 = new JTextArea(10, 15);

    @SuppressWarnings("rawtypes")
    JComboBox comboBox = new JComboBox(); // 下拉列表
    JScrollPane jsp = new JScrollPane(jb); // 滚动条
    int i = 1;

    @SuppressWarnings("unchecked")
    public Message1() {
        super();

        jsp.setViewportView(t1); // 增长滚动条

        comboBox.addItem("潍坊"); // 下拉
        comboBox.addItem("烟台"); // 列表
        comboBox.addItem("济南"); // 赋值

        fx[0] = new JCheckBox("");
        fx[1] = new JCheckBox("");
        fx[2] = new JCheckBox("rap");
        fx[3] = new JCheckBox("篮球");

        jf.setBounds(600, 175, 400, 400);
        l.setBounds(20, 10, 100, 20);
        l1.setBounds(20, 40, 100, 20);
        l2.setBounds(20, 70, 100, 20);
        l3.setBounds(20, 100, 100, 20);
        t.setBounds(65, 10, 100, 20);
        b.setBounds(65, 40, 40, 20);
        b1.setBounds(105, 40, 40, 20);
        fx[0].setBounds(65, 70, 60, 20);
        fx[1].setBounds(125, 70, 60, 20);
        fx[2].setBounds(185, 70, 60, 20);
        fx[3].setBounds(245, 70, 60, 20);
        comboBox.setBounds(65, 100, 70, 20);
        jb.setBounds(110, 140, 70, 20);
        jb1.setBounds(200, 140, 73, 20);
        jsp.setBounds(0, 200, 385, 160);

        bg.add(b);
        bg.add(b1);
        jf.add(p);
        p.add(l);
        p.add(t);
        p.add(l1);
        p.add(b);
        p.add(b1);
        p.add(l2);
        p.add(fx[0]);
        p.add(fx[1]);
        p.add(fx[2]);
        p.add(fx[3]);
        p.add(l3);
        p.add(comboBox);
        p.add(jb);
        p.add(jb1);
        p.add(jsp);

        p.setLayout(null);
        jf.setVisible(true);
        t1.setLineWrap(true); //文本域值满自动换行
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //// 把滚动条添加到容器里面
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                t1.append("" + i++ + "个学生的信息\n");
                t1.append(l.getText() + "  "); // append是添加语句,意为把某某加入t1.append中
                t1.append(t.getText() + "  ");
                t1.append(l1.getText() + "  ");
                if (b.isSelected())
                    t1.append(b.getText() + "  ");
                else
                    t1.append(b1.getText() + "  ");
                t1.append(l2.getText() + "  ");
                for (int j = 0; j < 4; j++)
                    if (fx[j].isSelected())
                        t1.append(fx[j].getText() + "  ");
                t1.append(l3.getText() + "  ");
                String jg = (String) comboBox.getSelectedItem(); // 获得下拉列表的值
                t1.append(jg + " \n ");

            }
        });
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                t.setText("");
                //t1.setText("");
                bg.clearSelection();                 // 清除单选框选中状态
                for (int j = 0; j < 4; j++)
                    fx[j].setSelected(false);      // 清除复选框选中状态
                comboBox.setSelectedIndex(0);     //下拉列表恢复默认
            }
        });
    }

    public static void main(String[] args) {
        new Message1();
    }

}
相关文章
相关标签/搜索