Java-从GUI中的文件设置密码
我最近学会了如何使用NetBeans GUI编辑器,我非常喜欢它,但我遇到了一个问题。我正在制作一个个人使用的程序,需要登录。登录的密码是从文件中检索的,可以更改。到目前为止,制作文本文件、放置内容和更改内容都不是问题所在。我的问题是将字符串密码设置为等于文本文件中的任何内容。请帮帮我。这是代码。(jPasswordField1是密码框,jButton1是打开菜单更改密码的按钮。)Java-从GUI中的文件设置密码,java,swing,file-io,passwords,jframe,Java,Swing,File Io,Passwords,Jframe,我最近学会了如何使用NetBeans GUI编辑器,我非常喜欢它,但我遇到了一个问题。我正在制作一个个人使用的程序,需要登录。登录的密码是从文件中检索的,可以更改。到目前为止,制作文本文件、放置内容和更改内容都不是问题所在。我的问题是将字符串密码设置为等于文本文件中的任何内容。请帮帮我。这是代码。(jPasswordField1是密码框,jButton1是打开菜单更改密码的按钮。) 包my.shortcutApp; 导入java.io.BufferedWriter; 导入java.io.File
包my.shortcutApp;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JOptionPane;
公共类shotcutApp扩展了javax.swing.JFrame
{
文件密码文件=新文件(“C:/ProgramFiles(x86)/Steam/userdata/193530500/7/remote/Game Data.txt”);
字符串密码;//我需要它等于^^
字符串重置=”;
公共shotcutApp()
{
初始化组件();
}
@抑制警告(“未选中”)
//
私有组件(){
loginPanel=newjavax.swing.JPanel();
jPasswordField1=newjavax.swing.JPasswordField();
jLabel1=newjavax.swing.JLabel();
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
loginPanel.setName(“组织者”);//NOI18N
jPasswordField1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
JPasswordField1执行的操作(evt);
}
});
jLabel1.setText(“登录”);
jButton1.setText(“更改”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout loginPanelLayout=新建javax.swing.GroupLayout(loginPanel);
loginPanel.setLayout(loginPanelLayout);
LoginPannelLayout.setHorizontalGroup(
loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(loginPannellayout.createSequentialGroup()
.addGap(128、128、128)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
.addGroup(loginPannellayout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,98,javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPasswordField1,javax.swing.GroupLayout.PREFERRED_SIZE,171,javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(128,简称最大值))
);
LoginPannelLayout.setVerticalGroup(
loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,loginPanelLayout.createSequentialGroup()
.addContainerGap(259,简称最大值)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,22,javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPasswordField1)
.addContainerGap())
);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loginPanel,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loginPanel,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
);
包装();
setLocationRelativeTo(空);
}//
私有void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt){
字符串输入=evt.getActionCommand();
if(输入.等于(密码))
{
loginPanel.setVisible(false);
}
如果(!input.equals(密码))为else
{
showMessageDialog(这是“错误的密码”);
}
jPasswordField1.setText(“”);
}
@SuppressWarnings(“ConvertToTryWithResources”)
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
如果(!passwordFile.exists())
{
reset=JOptionPane.showInputDialog(“请输入新密码”);
请尝试{passwordFile.createNewFile();}catch(IOException ex){Logger.getLogger(shotcutApp.class.getName()).log(Level.SEVERE,null,ex);}
试一试{
BufferedWriter输出=new BufferedWriter(new FileWriter(passwordFile));
输出、写入(复位);
output.close();
}捕获(IOEX异常){
Logger.getLogger(shotcutApp.class.getName()).log(Level.SEVERE,null,ex);
}
}
package my.shortcutApp;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class shotcutApp extends javax.swing.JFrame
{
File passwordFile = new File("C:/Program Files (x86)/Steam/userdata/193530500/7/remote/Game Data.txt");
String password; // I need this to equal ^^
String reset = "";
public shotcutApp()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
loginPanel = new javax.swing.JPanel();
jPasswordField1 = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
loginPanel.setName("Organizer"); // NOI18N
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
jLabel1.setText("Login");
jButton1.setText("change");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
loginPanel.setLayout(loginPanelLayout);
loginPanelLayout.setHorizontalGroup(
loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(loginPanelLayout.createSequentialGroup()
.addGap(128, 128, 128)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(loginPanelLayout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(128, Short.MAX_VALUE))
);
loginPanelLayout.setVerticalGroup(
loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
.addContainerGap(259, Short.MAX_VALUE)
.addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPasswordField1)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
String input = evt.getActionCommand();
if (input.equals(password))
{
loginPanel.setVisible(false);
}
else if (!input.equals(password))
{
JOptionPane.showMessageDialog(this, "Wrong password.");
}
jPasswordField1.setText("");
}
@SuppressWarnings("ConvertToTryWithResources")
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (!passwordFile.exists())
{
reset = JOptionPane.showInputDialog("Please enter new password.");
try {passwordFile.createNewFile();} catch (IOException ex) {Logger.getLogger(shotcutApp.class.getName()).log(Level.SEVERE, null, ex);}
try {
BufferedWriter output = new BufferedWriter(new FileWriter(passwordFile));
output.write(reset);
output.close();
} catch (IOException ex) {
Logger.getLogger(shotcutApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (passwordFile.exists())
{
reset = JOptionPane.showInputDialog("Please enter old password.");
if (reset.equals(password))
{
try {PrintWriter pw = new PrintWriter(passwordFile);pw.close();} catch (FileNotFoundException ex) {Logger.getLogger(shotcutApp.class.getName()).log(Level.SEVERE, null, ex);}
password = JOptionPane.showInputDialog("Please enter new password.");
try {
BufferedWriter output = new BufferedWriter(new FileWriter(passwordFile));
output.write(password);
output.close();
} catch (IOException ex) {
Logger.getLogger(shotcutApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (!reset.equals(password))
{
JOptionPane.showMessageDialog(this, "Wrong password.");
}
}
}
public static void main(String args[])
{
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(shotcutApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(shotcutApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(shotcutApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(shotcutApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new shotcutApp().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JPanel loginPanel;
// End of variables declaration
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Configs {
public static Properties prop = new Properties();
public void SaveProp(String title, String value) {
try {
prop.setProperty(title, value);
prop.store(new FileOutputStream("configuration.con"), null);
} catch (IOException e) {
}
}
public String GetProp(String title) {
String value = "";
try {
prop.load(new FileInputStream("configuration.con"));
value = prop.getProperty(title);
} catch (IOException e) {
}
return value;
}
}
Configs con = new Configs();
String UserName = "UserName";
String Password = "Password";
String newpassword;
String newuser;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
con.SaveProp(UserName, jTextField1.getText());
con.SaveProp(Password, jTextField2.getText());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
newuser = con.GetProp("UserName");
newpassword = con.GetProp("Password");
jTextField3.setText(newuser);
jTextField4.setText(newpassword);
}