package Example;
/**
* @author:
* @function:用户登录界面实现,跳转到监测数据界面
*/
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class helloLabel extends JFrame{
private static int count=0;
private static JButton bt1;//登陆按钮
private static JButton bt2;//忘记密码按钮
private static JLabel jl_1;//登录的版面
private static JFrame jf_1;//登陆的框架
private static JTextField jtext1;//用户名
private static JPasswordField jtext2;//密码
private static JLabel jl_admin;
private static JLabel jl_password;
public helloLabel (){//初始化登陆界面
Font font =new Font("黑体", Font.PLAIN, 20);//设置字体
jf_1=new JFrame("登陆界面");
jf_1.setSize(450, 400);
//给登陆界面添加背景图片
ImageIcon bgim = new ImageIcon(helloLabel.class.getResource("baozou.PNG")) ;//背景图案
bgim.setImage(bgim.getImage().
getScaledInstance(bgim.getIconWidth(),
bgim.getIconHeight(),
Image.SCALE_DEFAULT));
jl_1=new JLabel();
jl_1.setIcon(bgim);
jl_admin=new JLabel("用户名");
jl_admin.setBounds(20, 50, 60, 50);
jl_admin.setFont(font);
jl_password=new JLabel("密码");
jl_password.setBounds(20, 120, 60, 50);
jl_password.setFont(font);
bt1=new JButton("登陆"); //更改成loginButton
bt1.setBounds(90, 250, 100, 50);
bt1.setFont(font);
bt2=new JButton("退出");
bt2.setBounds(250, 250, 100, 50);
bt2.setFont(font);
//加入文本框
jtext1=new JTextField("root");
jtext1.setBounds(150, 50, 250, 50);
jtext1.setFont(font);
jtext2=new JPasswordField("123456");//密码输入框
jtext2.setBounds(150, 120, 250, 50);
jtext2.setFont(font);
jl_1.add(jtext1);
jl_1.add(jtext2);
jl_1.add(jl_admin);
jl_1.add(jl_password);
jl_1.add(bt1);
jl_1.add(bt2);
jf_1.add(jl_1);
jf_1.setVisible(true);
jf_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf_1.setLocation(300,400);
}
public static void main(String[] args) {
//初始化登陆界面
helloLabel hl =new helloLabel();
/**
* 处理点击事件
* 1.登陆按钮点击事件,判断账号密码是否正确,若正确,弹出监测信息界面
* 否则,无响应(暂时无响应)
* :后可在登陆界面添加一个logLabel提示用户是否用户密码正确
* 2.退出按钮,直接退出程序
*/
//登陆点击事件
ActionListener bt1_ls=new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String admin=jtext1.getText();
char[] password=jtext2.getPassword();
String str=String.valueOf(password); //将char数组转化为string类型
if(admin.equals("root")&&str.equals("123456"))
{
System.out.println(admin);
System.out.println(str);
mainLayout ml=new mainLayout();//为跳转的界面
hl.jf_1.dispose();//销毁当前界面
}
else {
count++;
System.out.println("error");
if(count==3){
hl.jf_1.dispose();
}
}
}
};
bt1.addActionListener(bt1_ls);
//退出事件的处理
ActionListener bt2_ls=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);//终止当前程序
}
};
bt2.addActionListener(bt2_ls);
}
}
最近题主我在做一个垃圾项目,从java入门到放弃已经一年了。。。重新来写java代码,很糟心,需要写一个swing的界面,主要实现串口通信,这两三天可能会跟进项目更新博客,大家有事没事可以来看看,先贴代码
今天一晚上写的是一堆登陆界面的垃圾代码
上面的是登陆界面:helloJlabel.java
跳转的界面 是串口通讯数据显示的界面,明天贴出来,这个界面暂时不要看,看了也没用。。。。
代码写得烂,希望各路大神轻喷。。。。
package Example;
/*
* @author :
* @功能:主要监测界面,显示温度,湿度,光照强度,
* @代码逻辑主要为处理串口通信的代码
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class mainLayout extends JFrame{
private static JFrame jf;
private static JLabel jl;
private static JButton bt_open;
private static JButton bt_close;
private static JButton bt_sysInfo;
private static JButton bt_back;
public mainLayout(){
jf=new JFrame("i am the new JFrame");
jf.setVisible(true);
jf.setLocation(10, 10);
jf.setBounds(10, 10, 100, 100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
mainLayout ml=new mainLayout();
}
}