这次老师让我们学习一种常用的使用界面组件的使用方法,我下面用到了复选框(JCheckBox)和组合框(JComboBox)并且有了事件响应,这个实现了用户名,密码的登录,这个界面一部分是同学帮改进的,对于我来说一周做这样的界面是一个研究和探索过程,不断地失败,有些里面的代码我也有些不理解,只是能请教在这方面很有优势的同学来帮助我,还是得多看书,多敲代码才行
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CheckBoxTest {
public static
final JFrame frm=new JFrame("组建的使用");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel topPanel=new JPanel();
frm.add(topPanel,BorderLayout.CENTER);
topPanel.setLayout(new GridLayout(3,1));
JPanel topPanel1=new JPanel();
JPanel topPanel2=new JPanel();
JPanel topPanel3=new JPanel();
JLabel show=new JLabel();
show.setText("密 码");
final JComboBox usernameInput = new JComboBox();
usernameInput.addItem("张三的账户");
usernameInput.addItem("李四的账户");
usernameInput.addItem("赵五的账户");
final JTextField input=new JTextField();
input.setEditable(true);
input.setHorizontalAlignment(SwingConstants.LEFT);
input.setColumns(10);
JButton b1=new JButton("登 陆");
JLabel show1=new JLabel();
show1.setText("用户名");
final JPasswordField input1 =new JPasswordField(10);
input1.setEditable(true);
input1.setHorizontalAlignment(SwingConstants.LEFT);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String accout = usernameInput.getSelectedItem().toString();
if(accout.equals("张三的账户")&&
String.valueOf(input1.getPassword()).equals("3"))
{
frm.setVisible(false);
JOptionPane.showMessageDialog(null, "欢迎登陆张三的账户",
"欢迎您",JOptionPane.INFORMATION_MESSAGE);
}else{
if(accout.equals("李四的账户")&&
String.valueOf(input1.getPassword()).equals("4"))
{
frm.setVisible(false);
JOptionPane.showMessageDialog(null, "欢迎登陆李四的账户",
"欢迎您",JOptionPane.INFORMATION_MESSAGE);
}
else{
if(accout.equals("赵五的账户")&&
String.valueOf(input1.getPassword()).equals("5"))
{
frm.setVisible(false);
JOptionPane.showMessageDialog(null, "欢迎登陆赵五的账户",
"欢迎您",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "密码错误",
"Warning",JOptionPane.ERROR_MESSAGE);
}} }}
});
//
topPanel1.add(show);
topPanel1.add(input1);
topPanel2.add(show1);
topPanel2.add(usernameInput);
topPanel.add(topPanel1);
topPanel.add(topPanel2);
topPanel.add(topPanel1);
topPanel.add(topPanel3);
topPanel3.add(b1);
topPanel1.setOpaque(false);
topPanel2.setOpaque(false);
topPanel3.setOpaque(false);
ImageIcon icon = new ImageIcon("p=0.jpg");
JLabel j1 = new JLabel(icon);
topPanel1.add(j1);
j1.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
//
ImageIcon icon1 = new ImageIcon("p=01.jpg");
JLabel j11 = new JLabel(icon1);
topPanel2.add(j11);
j11.setBounds(0,0,icon1.getIconWidth(),icon1.getIconHeight());
//
JPanel bottomPanel=new JPanel();
topPanel.setBackground(Color.YELLOW);
bottomPanel.setBackground(Color.YELLOW);
frm.add(bottomPanel,BorderLayout.SOUTH);
frm.setSize(400,200);
frm.setLocation(200,200);
JCheckBox mCheckBox = new JCheckBox("记住账号");
JCheckBox mCheckBox1 = new JCheckBox("记住密码");
mCheckBox.setSelected(true);
bottomPanel.add(mCheckBox);
bottomPanel.add(mCheckBox1);
frm.setVisible(true);
}
private static void setVisible(boolean b) {
// TODO Auto-generated method stub
}
private static Container getContentpane() {
// TODO Auto-generated method stub
return null;
}}
{
,