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(); } }