📄 setautologinframe.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Properties;
public class SetAutoLoginFrame extends JFrame implements ActionListener{
static final long serialVersionUID=80;
JButton confirmbutton,cancelbutton;
JTextField addressfield,userfield,portfield;
JPasswordField passwordfield,confirmfield;
JLabel addresslabel,userNamelabel,passwordlabel,confirmlabel,portlabel;
Checkbox auto;
String fileName;
Container container;
Dimension screenSize;
Toolkit tool;
MainFrame frame;
SetAutoLoginFrame(String title,MainFrame frame){
super(title);
this.frame=frame;
setResizable(false);
setSize(240,250);
tool=getToolkit();
screenSize=tool.getScreenSize();
container=getContentPane();
container.setLayout(null);
container.setBackground(Color.ORANGE);
confirmbutton=new JButton("确定");
cancelbutton=new JButton("取消");
confirmbutton.addActionListener(this);
cancelbutton.addActionListener(this);
addressfield=new JTextField();
userfield=new JTextField();
portfield=new JTextField();
passwordfield=new JPasswordField();
confirmfield=new JPasswordField();
addresslabel=new JLabel("服务器");
portlabel=new JLabel("端口");
userNamelabel=new JLabel("用户名");
passwordlabel=new JLabel("密码");
confirmlabel=new JLabel("确认密码");
auto=new Checkbox("启动时自动登录");
addressfield.setText(frame.address);
userfield.setText(frame.user);
passwordfield.setText(frame.password);
confirmfield.setText(frame.password);
portfield.setText(frame.port);
fileName = "AutoLogin.properties";
File file=new File(fileName);
if(file.exists()){
auto.setState(true);
}
container.add(addresslabel);
addresslabel.setBounds(20,5,40,30);
container.add(addressfield);
addressfield.setBounds(60,9,160,22);
container.add(userNamelabel);
userNamelabel.setBounds(20,40,40,30);
container.add(userfield);
userfield.setBounds(60,44,160,22);
container.add(passwordlabel);
passwordlabel.setBounds(30,75,30,30);
container.add(passwordfield);
passwordfield.setBounds(60,79,160,22);
container.add(confirmlabel);
confirmlabel.setBounds(5,110,60,30);
container.add(confirmfield);
confirmfield.setBounds(60,114,160,22);
container.add(portlabel);
portlabel.setBounds(30,145,30,30);
container.add(portfield);
portfield.setBounds(60,149,55,22);
container.add(auto);
auto.setBounds(120,145,100,30);
container.add(confirmbutton);
confirmbutton.setBounds(30,185,70,30);
container.add(cancelbutton);
cancelbutton.setBounds(150,185,70,30);
Dimension frameSize=getSize();
setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) / 2);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==confirmbutton){
File file=new File(fileName);
if(auto.getState()){
String server=addressfield.getText();
String userstring=userfield.getText();
String passwordstring=new String(passwordfield.getPassword());
String confirmstring=new String(confirmfield.getPassword());
String portstring=portfield.getText();
if(server.length()!=0&&userstring.length()!=0&&passwordstring.length()!=0&&confirmstring.length()!=0&&portstring.length()!=0){
if(passwordstring.equals(confirmstring)){
if(file.exists()){
file.delete();
}
Properties props = new Properties();
props.setProperty("Address",server);
props.setProperty("Port",portstring);
props.setProperty("User",userstring);
props.setProperty("Password",passwordstring);
try{
PrintStream out=new PrintStream(new FileOutputStream(fileName));
props.list(out);
out.close();
frame.address=server;
frame.port=portstring;
frame.user=userstring;
frame.password=passwordstring;
dispose();
}
catch(IOException e2){
JOptionPane.showMessageDialog(this,e2);
}
}
else{
JOptionPane.showMessageDialog(this,"两次输入的密码要一致");
}
}
else{
JOptionPane.showMessageDialog(this,"文本框不能为空");
}
}
else{
if(file.exists()){
file.delete();
}
dispose();
}
}
else{
dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -