JComboBox有一个
SelectedItem属性,所以使用getSelectedItem()就可以得到当前选中值.
1 package ltb20180106; 2 3 import javax.swing.*; 4 import java.awt.event.*; 5 import java.awt.*; 6 7 8 public class UserLoginApp { 9 10 private JFrame jf; 11 private JLabel name; 12 private JLabel user; 13 private JLabel password; 14 private JButton confirm; 15 private JButton cancel; 16 private JButton quit; 17 18 private JPanel p1; 19 private JPanel p2; 20 private JpanelAction p3; 21 private JPanel p4; 22 23 private JComboBoxjuser; 24 private JTextField jname; 25 private JPasswordField jpassword; 26 27 private String[] s= {"学生用户","教师用户"}; 28 29 30 31 private String ss; 32 private char[] c; 33 private String bname; 34 private String item; 35 36 37 public UserLoginApp() { 38 39 try { 40 41 jf=new JFrame("用户登录"); 42 jf.setSize(250, 160); 43 jf.setLayout(new BorderLayout()); 44 45 46 name=new JLabel("名字:"); 47 password=new JLabel("密码:"); 48 user=new JLabel("用户类型"); 49 50 juser=new JComboBox (s); 51 jname=new JTextField(); 52 jpassword=new JPasswordField(); 53 54 confirm=new JButton("确定"); 55 cancel=new JButton("取消"); 56 quit=new JButton("退出"); 57 58 p1=new JPanel(); 59 p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));//允许垂直或水平布置多个组件的布局管理器 60 p1.add(user); 61 p1.add(name); 62 p1.add(password); 63 p1.setSize(100,100); 64 65 p2=new JPanel(); 66 p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS)); 67 p2.add(juser); 68 p2.add(jname); 69 p2.add(jpassword); 70 p2.setSize(150,100); 71 72 73 74 p3=new JpanelAction(); 75 p3.setLayout(new FlowLayout()); 76 p3.add(confirm); 77 p3.add(cancel); 78 p3.add(quit); 79 confirm.addActionListener(p3); 80 cancel.addActionListener(p3); 81 quit.addActionListener(p3); 82 83 84 p4=new JPanel(); 85 p4.setLayout(new FlowLayout()); 86 p4.add(p1); 87 p4.add(p2); 88 89 jf.add(p4,BorderLayout.NORTH); 90 jf.add(p3,BorderLayout.CENTER); 91 92 jf.setVisible(true); 93 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 94 jf.setLocationRelativeTo(null); 95 96 97 }catch(Exception e) { 98 99 System.out.println(e.getMessage());100 101 }102 103 104 }105 106 107 108 109 @SuppressWarnings("serial")110 class JpanelAction extends JPanel implements ActionListener {111 112 113 public void actionPerformed(ActionEvent e) 114 {115 116 bname=e.getActionCommand();//关键的地方117 118 if(bname.equals("确定")) {119 120 ss=jname.getText(); 121 c=jpassword.getPassword();122 item=(String)juser.getSelectedItem();// JComboBox 当前所选项123 124 125 if(ss.equals("")) {126 127 name.setText("用户名不能为空");128 129 } else if(c.length==0) { 130 131 password.setText("密码不能为空");132 133 }else if(item.equals("学生用户")) {134 135 if(ss.equals("s")&&new String(c).equals("s")) { //密码字符转化字符串处理136 137 name.setText("登录成功");138 password.setText("登录成功");139 } 140 System.out.println("学生");141 142 }else if(item.equals("教师用户")) {143 144 if(ss.equals("t")&&new String(c).equals("t")) { //密码字符转化字符串处理145 146 name.setText("登录成功");147 password.setText("登录成功");148 149 }150 151 System.out.println("教师");152 }153 154 155 }else if (bname.equals("取消")) {156 157 jname.setText("");158 jpassword.setText("");159 160 }else if(bname.equals("退出")){161 162 jf.dispose();//释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。163 }164 }165 166 167 }168 169 public static void main(String[] args) {170 171 new UserLoginApp();172 }173 174 }