经过第二周的学习,我基本可以独立完成一个简单的GUI面板的制作,我在这次的作业中使用了JcomboBox这个组件,这个组件很方便,可以给用户省去很多时间
import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; public class D2 { public static void main(String[] args){ EventQueue.invokeLater(new Runnable(){ public void run(){ JFrame frame = new Register(); frame.setTitle("班级管理系统"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class Register extends JFrame{ private static final long serialVersionUID = 1L; private JPanel all = new JPanel(); private JComboBox jcb = new JComboBox(); private JPasswordField pwf = new JPasswordField(10); private JButton confirm = new JButton("确定"); private JButton cancel = new JButton("取消"); Register(){ GridLayout Gl = new GridLayout(3,2,30,10); all.setLayout(Gl); addU(); addP(); addB(); add(all); pack(); } private void addU(){ jcb.addItem("导员"); jcb.addItem("班长"); jcb.addItem("学委"); jcb.addItem("团支"); jcb.addItem("学生"); jcb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a3){ } }); all.add(new Label("请输入用户名")); all.add(jcb); } private void addP(){ all.add(new Label("请输入密码")); all.add(pwf); } private void addB(){ confirm.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent a1) { String p = new String(pwf.getPassword()); String s = (String)jcb.getSelectedItem(); JOptionPane.showMessageDialog(null,"你选择的职务:"+s+" 你输入的密码:"+p); } }); cancel.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a2){ JOptionPane.showMessageDialog(null,"退出"); dispose(); } }); all.add(confirm); all.add(cancel); } }