Question
Having issues with my Java swing program. I can create users perfectly fine but when I try and log them in the user accounts in
Having issues with my Java swing program. I can create users perfectly fine but when I try and log them in the user accounts in the login.txt are deleted.
Source Code:
Home.java:
import java.awt.EventQueue;
import javax.swing.JFrame; import java.awt.Color; import javax.swing.JButton; import java.awt.Font; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent;
public class Home {
private JFrame frmWeatherDataProgram;
/** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Home window = new Home(); window.frmWeatherDataProgram.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
/** * Create the application. */ public Home() { initialize(); }
/** * Initialize the contents of the frame. */ private void initialize() { frmWeatherDataProgram = new JFrame(); frmWeatherDataProgram.setResizable(false); frmWeatherDataProgram.setForeground(Color.BLACK); frmWeatherDataProgram.setTitle("Weather Data Program"); frmWeatherDataProgram.getContentPane().setBackground(Color.BLACK); frmWeatherDataProgram.getContentPane().setLayout(null); JButton Loginbtn = new JButton("Login"); Loginbtn.setFont(new Font("Tahoma", Font.BOLD, 12)); Loginbtn.setForeground(new Color(255, 140, 0)); Loginbtn.setBackground(Color.BLACK); Loginbtn.setBounds(29, 138, 107, 23); frmWeatherDataProgram.getContentPane().add(Loginbtn); JButton CreateAccountbtn = new JButton("Create Account"); CreateAccountbtn.setFont(new Font("Tahoma", Font.BOLD, 12)); CreateAccountbtn.setForeground(new Color(255, 140, 0)); CreateAccountbtn.setBackground(Color.BLACK); CreateAccountbtn.setBounds(192, 138, 131, 23); frmWeatherDataProgram.getContentPane().add(CreateAccountbtn); JButton Exitbtn = new JButton("Exit "); Exitbtn.setFont(new Font("Tahoma", Font.BOLD, 12)); Exitbtn.setForeground(new Color(255, 140, 0)); Exitbtn.setBackground(Color.BLACK); Exitbtn.setBounds(368, 138, 107, 23); frmWeatherDataProgram.getContentPane().add(Exitbtn); JLabel lblWeatherProgram = new JLabel("Weather Data Program"); lblWeatherProgram.setFont(new Font("Yu Gothic Medium", Font.BOLD, 12)); lblWeatherProgram.setBackground(Color.BLACK); lblWeatherProgram.setForeground(Color.WHITE); lblWeatherProgram.setBounds(358, 11, 131, 26); frmWeatherDataProgram.getContentPane().add(lblWeatherProgram); JLabel jl = new JLabel(); jl.setBounds(0, 0, 524, 49); frmWeatherDataProgram.getContentPane().add(jl); frmWeatherDataProgram.setBackground(Color.BLACK); frmWeatherDataProgram.setBounds(100, 100, 530, 244); frmWeatherDataProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jl.setIcon(new ImageIcon("E:\\CSE 226 Project\\Weather.jpg")); JPanel panel = new JPanel(); panel.setBackground(new Color(255, 140, 0)); panel.setBounds(0, 48, 524, 6); frmWeatherDataProgram.getContentPane().add(panel); Exitbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); CreateAccountbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AccountCreation create = new AccountCreation(); create.setVisible(true); } }); Loginbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AccountLogin sign = new AccountLogin(); sign.setVisible(true); } }); } }
AccountCreation.java:
10 import java.awt. EventQueue; 3 import javax.swing.JFrame; 4 import javax.swing.JPanel; 5 import javax.swing.border. EmptyBorder; import java.awt.Color; import javax.swing. JTextField; import javax.swing Passwordfield import javax.swing. JLabel; 14 11 import java.awt. Font; 12 import javax.swing.JButton; 13 import java.awt.event.ActionListener; import java.io.File; 15 import java.io.FileNotFoundException; 16 import java.io.FileReader; 17 import java.io.FileWriter; 18 import java.io.IOException; 19 import java.io. RandomAccessFile; 20 import java.awt.event.ActionEvent; 21 22 public class AccountCreation extends JFrame { 23 240 /** 26 private static final long serialversionUID = 1; private JPanel contentPane; private JTextField tfusr; private JPanel panel; private JPanel panel_1; File f = new File("E:\\CSE 226 Project"); 34 int ln; String Username, Password; private JTextField tfpswd; /** * Launch the application. 380 39 40 410 420 430 public static void AccountScreen() { EventQueue.invokelater(new Runnable() { public void run() { try { AccountCreation frame = new AccountCreation frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); void createFolder() { if(!f.exists()) { f.mkdirs(); 56 600 void readFile() { try { FileReader fr = new FileReader(f+"\\logins.txt"); System.out.println("file exists!"); } catch (FileNotFoundException e) { try { FileWriter fw = new FileWriter(f+"\\logins.txt"); System.out.println("File created"); } catch (IOException el) { el.printStackTrace(); 740 75 26 void addData(String usr, String pswd) { try { RandomAccessFile caf = new RandomAccessFile(f+"\\logins.txt", "rw"); for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started