经过一下午的失败,很是伤心,在网上查了许多资料,但还是以失败为结局,但是我相信失败是成功之母,下次一定会成功的
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class shuaigexitong extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel p;
JButton a;
JButton b;
JLabel u;
JLabel m;
JLabel se;
JLabel t;
JLabel shfen;
JComboBox kuang3;
ImageIcon tu;
JTextField kuang1;
JPasswordField kuang2;
String role[] = { "帅哥", "靓女" };
JLabel jlb1 = new JLabel(new ImageIcon("toubu.png"));
public shuaigexitong() {
se = new JLabel("帅哥靓女登录系统");
se.setBounds(115, 20, 420, 50);
se.setFont(new Font("宋体", Font.CENTER_BASELINE, 20));
se.setForeground(Color.black);
u = new JLabel("用户名:");
u.setFont(new Font("宋体", Font.CENTER_BASELINE, 15));
u.setForeground(Color.black);
m = new JLabel("密码:");
m.setFont(new Font("宋体", Font.CENTER_BASELINE, 15));
m.setForeground(Color.black);
shfen = new JLabel("身份:");
shfen.setFont(new Font("宋体", Font.CENTER_BASELINE, 15));//设置字体
shfen.setBounds(130, 200, 100, 50);
kuang3 = new JComboBox(role);
kuang3.setForeground(Color.black);
kuang3.setBounds(210, 210, 100, 25);
kuang1 = new JTextField();
kuang1.setBounds(210, 100, 180, 25);
kuang1.setForeground(Color.black);
kuang2 = new JPasswordField();
kuang2.setBounds(210, 160, 180, 25);
kuang2.setEchoChar(‘*‘);//设置密码框出现的符号
a = new JButton("确定");
a.setForeground(Color.black);//所设置字体颜色为蓝色
a.addActionListener(this);//对这个按钮进行监听
b = new JButton("取消");
b.setMnemonic(KeyEvent.VK_N);
b.setForeground(Color.black);
b.addActionListener(this);
p = new JPanel();
tu = new ImageIcon("/img/22.jpg");//这是图片
t = new JLabel(tu);//将图片添加到JLabel当中
p.add(t);//将添加到JPanel当中
p.setBounds(0, 0, 560, 400);//图片560*400(宽*高)
b.setBounds(320, 270, 120, 35);
a.setBounds(120, 270, 120, 35);
u.setBounds(130, 90, 100, 50);
m.setBounds(130, 150, 100, 50);
add(se);
add(u);
add(m);
add(shfen);
add(kuang3);
add(kuang1);
add(kuang2);
add(a);
add(b);
add(p);
add(jlb1);
setTitle("帅哥靓女登录系统");
setSize(560,400);
setResizable(false);//不能改变界面大小
setLayout(null);//设置界面的布局管理器为空
setVisible(true);
setLocationRelativeTo(null);//让窗口位于屏幕中央
}
public static void main(String[] args) {
new shuaigexitong();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == a)
{
if (kuang1.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null,"请输入用户名!", "帅哥/靓女你还没注册",JOptionPane.OK_OPTION);
}
else {
if (new String(kuang2.getPassword()).equals("")) {
JOptionPane.showMessageDialog(null,"请输入密码!", "帅哥/靓女密码错误",JOptionPane.OK_OPTION);
}
else {
if (kuang1.getText().trim().equals("我是大帅哥")&& (new String(kuang2.getPassword()).equals("123"))) {
this.dispose();//使当前界面退出
JOptionPane.showMessageDialog(null,"欢迎您,回来,主人!");
}
else
JOptionPane.showMessageDialog(null,"您输入的用户名或密码错误!");
kuang2.setText(null);//设置密码框为空
} }
}
if (e.getSource() == b)
System.exit(0);//关闭当前界面
}
}
原文:http://www.cnblogs.com/wsxzr/p/5304071.html