GUI登陆界面
程序员文章站
2022-04-18 17:10:32
...
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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 Test extends JFrame {
JTextField jt;
JPasswordField jp;
Font f1 = new Font("", Font.BOLD, 20);
public Test() {
setTitle("主窗口");
setBounds(100, 100, 607, 469);
setLayout(null);
setDefaultCloseOperation(2);
setIconImage(new ImageIcon("image\\icon.png").getImage());
Icon image1 = new ImageIcon("image\\top.png");
JLabel jl1 = new JLabel(image1);
jl1.setBounds(0, 0, 607, 150);
add(jl1);
JLabel jl2 = new JLabel("用 户 名:");
jl2.setBounds(140, 160, 100, 35);
jl2.setFont(f1);
add(jl2);
jt = new JTextField();
jt.setBounds(230, 160, 200, 35);
add(jt);
JLabel jl3 = new JLabel("密 码:");
jl3.setFont(f1);
jl3.setBounds(140, 220, 100, 35);
add(jl3);
jp = new JPasswordField();
jp.setBounds(230, 220, 200, 35);
add(jp);
JButton jb = new JButton("登录");
jb.setBounds(180, 310, 80, 30);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Boolean g=false;
String str = jt.getText();
String str1 = jp.getText();
String str2 = str + "," + str1;
File f = new File("file/gy.txt");
try {
if (!f.exists()) {
f.createNewFile();
}
FileReader fr = new FileReader(f);
BufferedReader bd = new BufferedReader(fr);
String len = null;
while ((len=bd.readLine()) != null) {
if (len.equals(str2)) {
new denglu(str).setVisible(true);
g=true;
}
}
if(!g){
JOptionPane.showMessageDialog(null, "该用户不存在,请注册");
jt.setText("");
jp.setText("");
}
} catch (Exception e) {
e.printStackTrace();
}
//dispose();
}
});
add(jb);
JButton jb1 = new JButton("注册");
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
new zhuce().setVisible(true);
dispose();
}
});
jb1.setBounds(300, 310, 80, 30);
add(jb1);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}