📄 loginframe.java
字号:
package com.ui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class LoginFrame extends JFrame implements ActionListener{
private JLabel jl;
private JTextField jf;
private JButton jb1;
private JButton jb2;
public LoginFrame()
{
super();
JPanel top=new JPanel();
jl=new JLabel("用户名:");
jf=new JTextField(20);
top.add(jl);
top.add(jf);
JPanel bottom=new JPanel();
jb1=new JButton("登录");
jb1.addActionListener(this);
bottom.add(jb1);
jb2=new JButton("退出");
bottom.add(jb2);
jb2.addActionListener(this);
// this.getContentPane().add(jl);
// this.getContentPane().add(jf);
// this.getContentPane().add(jb1);
// this.getContentPane().add(jb2);
this.getContentPane().add(top,"North");
this.getContentPane().add(bottom,"South");
// this.setSize(350,150);
this.setBounds(400, 300, 350, 150);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1)
{
File file=new File("txt\\user.txt");
boolean isLoginSuccess=false;
try {
BufferedReader bf=new BufferedReader(new FileReader(file));
String str="";
while((str=bf.readLine())!=null)
{
if(str.equals(jf.getText().trim()))
{
isLoginSuccess=true;
}
}
if(!isLoginSuccess)
{
jf.setText(null);
jf.grabFocus();
}
else
{
String user=jf.getText().trim();
dispose();
new MyJFrame(user);
}
} catch ( Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
// TODO 自动生成方法存根
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -