Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Class File Help

$
0
0
Hello, I am in grade 12 and i am trying to create a class and its not working, when i compile it , it works but when i run it and click the button to initialize i get so many errors, is there any suggestions on how to fix it. I tried fixing it but it won't work.




import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.io.*;

public class Bank extends JFrame implements ActionListener
  
{

private JTextField username;  
private JPasswordField pass; 
private JButton btnOk;
private JButton btnExit;
private JLabel enterUsername;
private JLabel enterPass;
private JLabel newAccountBalance;
private JLabel accountBalance;
private JLabel accountBalance2;
private JFrame framer;
private JButton resetPassword;
private JRadioButton deposit, withdraw;
private JTextField amount;
private JLabel enterAmount;
private JButton initiateTransaction;
private JLabel newBalance;
private JButton save;
 static PrintWriter output;
 private Zaib z;

private String filename;
private JButton logout;

int balance = 2000;
int  balance1 = 2000;
int noBalance = 0;
    
 public static void main(String [] args) throws IOException
{
new Bank();
}
 
 public Bank ()throws IOException
   
 {
   z = new Zaib();
  filename = "sample.txt";

 output = new PrintWriter(new FileWriter(filename)); 
    
  enterUsername = new JLabel("Username (Card Number): "); 
  enterUsername.setBounds(50, 50, 175,50);
 
  btnOk = new JButton ("OK");
  btnOk.setBounds(25, 200, 75,50);
  btnOk.addActionListener(this);
  
  btnExit = new JButton ("Exit");
  btnExit.setBounds(125, 200, 75,50);
  btnExit.addActionListener(this);
  
  username = new JTextField(15);
  username.setBounds(50, 85, 150, 25);
  username.addActionListener(this);

  enterPass = new JLabel("Passcode "); 
  enterPass.setBounds(50, 100, 175,50);
  
  pass = new JPasswordField(15);
  pass.setBounds(50, 135, 150, 25);
  pass.addActionListener(this);  
  
  resetPassword = new JButton ("Reset Password");
  resetPassword.setBounds(35,260,150,25);
  resetPassword.addActionListener(this);  
  
JPanel panel1 = new JPanel();
panel1.setLayout(null);
panel1.add(username);
panel1.add(enterUsername);
panel1.add(btnOk);
panel1.add(enterPass);
panel1.add(pass);
panel1.add(btnExit);
panel1.add(resetPassword);

setContentPane(panel1);
setLocationRelativeTo(null);
setTitle("Login");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
 }
 
  public void actionPerformed(ActionEvent e) 
   {
 String name = username.getText();
 String name1 = pass.getText();
 

if(e.getSource() == resetPassword)
   
 {
    String email = JOptionPane.showInputDialog(null, "Enter your email for a password reset link", "Input Dialog", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null,"A password reset link will be sent to: " + email, "Reset Password", JOptionPane.INFORMATION_MESSAGE);
 }
 

if (e.getSource() == btnOk)
   {
    if(name.equals("") && name1.equals(""))
     {
          
     framer = new JFrame(); 
       
     JOptionPane.showMessageDialog(null, "Login Sucessful", "Sucess", JOptionPane.INFORMATION_MESSAGE);
     accountBalance = new JLabel("Current Account Balance: " + balance); 
     accountBalance.setBounds(50, 50, 250,50);
     accountBalance.setVisible(true);
     
     accountBalance2 = new JLabel("Current Account Balance: " + balance1); 
     accountBalance2.setBounds(50, 50, 250,50);
     accountBalance2.setVisible(false);
     
     newAccountBalance = new JLabel("New Account Balance: " ); 
     newAccountBalance.setBounds(50, 400, 250, 50);
      
     newBalance = new JLabel ();
     newBalance.setBounds (200, 400,250,50);
     
     deposit = new JRadioButton("Deposit");
     deposit.setBounds (50,150, 125,25);
     deposit.addActionListener(this);
      
     withdraw = new JRadioButton("Withdraw");
     withdraw.setBounds (50,200, 125,25);
     withdraw.addActionListener(this);
       
     enterAmount = new JLabel ("Enter Amount:");
     enterAmount.setBounds(50,250, 125,25);
     
     amount = new JTextField (15);
     amount.setBounds(50, 300,125,25);
       
     initiateTransaction = new JButton ("Initiate Transaction");
     initiateTransaction .setBounds(50, 350, 150,25);
     initiateTransaction .addActionListener(this);
  
     save = new JButton("Save To File");
     save.setBounds (300,350,150,25);
     save.addActionListener(this);
     
     logout = new JButton ("Log Out");
     logout.setBounds (300, 400, 150,25);
     logout.addActionListener(this);
     

 // Create button group for soup options
     ButtonGroup option = new ButtonGroup();
     option.add(deposit);
     option.add(withdraw); 
      
     JPanel accountPanel = new JPanel();
     accountPanel.setLayout(null);
       accountPanel.add(accountBalance);
       accountPanel.add(newAccountBalance);
       accountPanel.add(deposit);
       accountPanel.add(withdraw);
       accountPanel.add(enterAmount);
       accountPanel.add(amount);
       accountPanel.add(initiateTransaction);
       accountPanel.add(newBalance);
       accountPanel.add(accountBalance2);
       accountPanel.add(save);
       accountPanel.add(logout);
     
      framer.setContentPane(accountPanel);
      framer.setLocationRelativeTo(null); 
      framer.setTitle("Account Information");
      framer.setSize(500, 500);
      framer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      framer.setVisible(true);
      framer.setLayout(null);
     }
     
     else 
     {
      JOptionPane.showMessageDialog(null, "Login Failed, Please Try Again", "Fail", JOptionPane.INFORMATION_MESSAGE);
     }
  }
     if (e.getSource() == btnExit)
     {
      JOptionPane.showMessageDialog(null, "Thank You For Choosing Bank of Baquial", "Bye!", JOptionPane.INFORMATION_MESSAGE);
       System.exit(0);    
     }
     try
     {
     if (e.getSource() == initiateTransaction && deposit.isSelected())
     {
       z.Deposit();
     }
     }
     catch (NumberFormatException x)
     {
       JOptionPane.showMessageDialog(null, "Please enter a number.", "Error", JOptionPane.ERROR_MESSAGE);
       amount.setText(" ");
     }
       try
       {
      if (e.getSource() == initiateTransaction && withdraw.isSelected())
      {
        z.Withdraw();
      }
       }
       catch (NumberFormatException x)
     {
       JOptionPane.showMessageDialog(null, "Please enter a number.", "Error", JOptionPane.ERROR_MESSAGE);
       amount.setText(" ");
     }
                
     if (e.getSource() == save)
      {
   
        output.println("" + balance );
        output.close();
      }
           
     if (e.getSource() == logout)
      {
        JOptionPane.showMessageDialog(null, "Thank You For Choosing Bank of Baquial", "Bye!", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);  
      }
     if (balance < 0)
     {
       newBalance.setText(" " + noBalance);
       accountBalance.setText ("Current Account Balance: " + noBalance);
       JOptionPane.showMessageDialog(null, "You are not eligible for a loan right now, please fill out an application with our branch manager: Brian Baquial", "Error", JOptionPane.INFORMATION_MESSAGE);
     }
     if (balance == 0)
     {
       JOptionPane.showMessageDialog(null, "Your Account Balance is: $0 ", "Warning", JOptionPane.INFORMATION_MESSAGE);
       JOptionPane.showMessageDialog(null, "The Bank of Baqial is known for its low interst rates on loan; apply now!", "Warning", JOptionPane.INFORMATION_MESSAGE);
     }
   }

 
//public  void Deposit()
//{
//  int enteredAmount;
//       enteredAmount = Integer.parseInt(amount.getText());
//       balance = balance + enteredAmount;
//       newBalance.setText("" + balance);
//       accountBalance.setText ("Current Account Balance: " + balance);
//  
//}
//public void Withdraw()
//{
//  int enteredAmount;
//        enteredAmount = Integer.parseInt(amount.getText());
//        balance = balance - enteredAmount;
//        newBalance.setText(" " + balance);
//        accountBalance.setText ("Current Account Balance: " + balance);
//}
}

 
  


 
 
   

   
  



This is the class file i tried to make

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.io.*;

public class Zaib
{
  
  public JTextField username;  
public JPasswordField pass; 
public JButton btnOk;
public JButton btnExit;
public JLabel enterUsername;
public JLabel enterPass;
public JLabel newAccountBalance;
public JLabel accountBalance;
public JLabel accountBalance2;
public JFrame framer;
public JButton resetPassword;
public JRadioButton deposit, withdraw;
public JTextField amount;
public JLabel enterAmount;
public JButton initiateTransaction;
public JLabel newBalance;
public JButton save;
 static PrintWriter output;

private String filename;
private JButton logout;


  
  
  
int balance = 2000;
int  balance1 = 2000;
int noBalance = 0;
  
 
  
  public  void Deposit()
{
  int enteredAmount;
       enteredAmount = Integer.parseInt(amount.getText());
       balance = balance + enteredAmount;
       newBalance.setText("" + balance);
       accountBalance.setText ("Current Account Balance: " + balance);
  
}
public void Withdraw()
{
  int enteredAmount;
        enteredAmount = Integer.parseInt(amount.getText());
        balance = balance - enteredAmount;
        newBalance.setText(" " + balance);
        accountBalance.setText ("Current Account Balance: " + balance);
}
}
  
  
  
  
  
  
  
  
  
  
  
  
  
  


sorry for not commenting

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>