java图片显示不了package cn.game.ui;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class LoginJFrame extends JFrame {
static Random rand = new Random();
//准备账号的储存
static ArrayList<database> list = new ArrayList<database>();
static{
list.add(new database("zhangsan","1234","123456789112345678","12345678912"));
}
//验证码
static String CAPTCHA = getcaptcha();
//获取输入的信息
String USERNAME;
String PASSWORD;
String IDCARDNUMBER;
//游戏登陆界面
public LoginJFrame() {
this.setSize(603,630);
//设置界面的标题
this.setTitle("登陆");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
this.setVisible(true);
//设置关闭模式,即关闭游戏程序结束
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//取消默认居中放置,取消了才可以按XY轴方式放置组件
this.setLayout(null);
//加载基本信息,用户名密码验证码
JLabel username = new JLabel("用户名");
JLabel password = new JLabel("密码");
JLabel captcha= new JLabel("验证码");
username.setBounds(160,250,100,30);
username.setFont(new Font("用户名", Font.PLAIN, 24)); // 调整字体大小
this.getContentPane().add(username);
password.setBounds(160,300,100,30);
password.setFont(new Font("<UNK>", Font.PLAIN, 24));
this.getContentPane().add(password);
captcha.setBounds(160,350,100,30);
captcha.setFont(new Font("<UNK>", Font.PLAIN, 24));
this.getContentPane().add(captcha);
//测试数据
// database data = new database();
// data.setUsername("123456789");
// data.setPassword("12342");
//
// JLabel testlabel = new JLabel(CAPTCHA);
// testlabel.setBounds(160,200,100,30);
// this.getContentPane().add(testlabel);
//加载白框为了输入
JTextField usernametext = new JTextField();
usernametext.setBounds(250,252,180,30);
this.getContentPane().add(usernametext);
JPasswordField passwordtext = new JPasswordField();
passwordtext.setBounds(250,302,180,30);
this.getContentPane().add(passwordtext);
JTextField captchatext = new JTextField();
captchatext.setBounds(250,352,130,30);
this.getContentPane().add(captchatext);
//生成验证码
JLabel captchalabel = new JLabel(getcaptcha());
captchalabel.setBounds(390,350,180,30);
captchalabel.setFont(new Font(getcaptcha(), Font.PLAIN, 24));
this.getContentPane().add(captchalabel);
//添加登陆和注册按钮
// JButton login = new JButton();
// login.setIcon(new ImageIcon("D:\\java\\IDEA\\code\\game\\login\\login.png"));
// login.setBorderPainted(false);
// login.setContentAreaFilled(false);
// login.setBounds(250,400,100,30);
// this.getContentPane().add(login);
//加载背景图
JLabel label = new JLabel(new ImageIcon("D:\\java\\IDEA\\code\\game\\animal1\\background.png"));
label.setBounds(40,10,503,560);
this.getContentPane().add(label);
//添加登陆和注册按钮
JButton login = new JButton();
login.setIcon(new ImageIcon("D:\\java\\IDEA\\code\\game\\animal1\\login.png"));
login.setBorderPainted(false);
login.setContentAreaFilled(false);
login.setBounds(250,400,130,50);
this.getContentPane().add(login);
//获取白框的输入值
// String USERNAME = usernametext.getText();
// String PASSWORD = passwordtext.getText();
// String CAPTCHA = captchalabel.getText();
}
//生产验证码
public static String getcaptcha()
{
String captcha = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
String number= "0123456789";
StringBuilder sb = new StringBuilder(5);
for(int i = 0;i < 4;i++)
{
int num = rand.nextInt(captcha.length());
sb.append(captcha.charAt(num));
}
int num = rand.nextInt(number.length());
int index = rand.nextInt(4);
sb.insert(index,number.charAt(num));
return sb.toString();
}
//判断账号是否可用
public static boolean isusername(String username)
{
if(username.length()<3 || username.length() > 17)
{
return false;
}
for(int i = 0;i < username.length();i++)
{
if(!Character.isDigit(username.charAt(i)))
{
return false;
}
}
return true;
}
//判断身份证是否可用
public static boolean isidcardnumber(String idcardnumber)
{
if(idcardnumber.length()!= 18)
{
return false;
}
for(int i = 0;i < idcardnumber.length();i++)
{
if(!Character.isDigit(idcardnumber.charAt(i)))
{
return false;
}
}
return Character.isDigit(idcardnumber.charAt(17)) && idcardnumber.charAt(17) !='x' && idcardnumber.charAt(17) != 'X';
}
//判断手机号是否可用
public static boolean isphonenumber(String phonenumber)
{
if(phonenumber.length()!= 11)
{
return false;
}
for(int i = 0;i < phonenumber.length();i++)
{
if(!Character.isDigit(phonenumber.charAt(i)))
{
return false;
}
}
return true;
}
//判断账号是否正确
public static boolean istrueusername(String username)
{
for(int i = 0;i < list.size();i++)
{
if(username.equals(list.get(i).getUsername()))
{
return true;
}
}
return false;
}
//判断密码是否正确
public static boolean istruepassword(String password)
{
for(int i = 0 ;i < list.size();i++)
{
if(password.equals(list.get(i).getPassword()))
{
return true;
}
}
return false;
}
//判断身份证是否正确
public static boolean istrueidcardnumber(String idcardnumber)
{
for(int i = 0;i < list.size();i++)
{
if(idcardnumber.equals(list.get(i).getIdcardnumber()))
{
return true;
}
}
return false;
}
//判断验证码是否正确
public static boolean istruecaptcha(String captcha)
{
if(captcha.equals(CAPTCHA))
{
return true;
}
return false;
}
//判断手机号是否正确
public static boolean istruephonenum(String phonenumber)
{
for(int i = 0;i < list.size();i++)
{
if(phonenumber.equals(list.get(i).getPhonenumber()))
{
return true;
}
}
return false;
}
//判断是可以登陆
public static boolean islogin(String username, String password, String captcha)
{
if(istrueusername(username) && istruepassword(password) && istruecaptcha(captcha))
{
return true;
}
return false;
}
//展示弹框
public static void showJDialog(String content)
{
JDialog jdialog = new JDialog();
jdialog.setSize(200,200);
jdialog.setAlwaysOnTop(true);
jdialog.setLocationRelativeTo(null);
jdialog.setModal(true);
JLabel label = new JLabel();
label.setBounds(0,0,200,150);
jdialog.getContentPane().add(label);
jdialog.setVisible(true);
}
}
为啥我的背景图和按钮不会显示出来
最新发布