Results 1 to 18 of 18
Thread: Help needed in Java Project
- 10-31-2010, 07:12 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Help needed in Java Project
HELP NEEDED in JAVA
Hello. I am studying JAVA and need a help in Java.
I was creating a GUI for my Java project named "THE ATM". everything was OK, till the part came where I had put the logic in the code.
THE PROBLEM :-
I wanted to implement a functionality in which I wanted the user to create a new account using the terminal window , when the user presses the "new account" button .
But instead, when the user presses the "new user" button, the terminal window opens, and I can't input anything in it. When I come back to the GUI, it gets all forzen up and my program crashes.
Please help by refining my code.
THE Code :-
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Atm.java * * Created on Oct 30, 2010, 1:45:17 PM */ package atm; /** * * @author Suresh */ import java.io.*; public class Atm extends javax.swing.JFrame { String username[] = new String[1000]; int password[] = new int[1000]; double balance[]= new double[1000]; int a1; int posi; boolean flag; /** Creates new form Atm */ public Atm() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jPasswordField1 = new javax.swing.JPasswordField(); jButton1 = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); jTextField3 = new javax.swing.JTextField(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); status = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); jLabel1.setText(" ATM by ABHILASH"); jLabel2.setText("Username :"); jLabel3.setText("Password :"); jButton1.setText("Log In"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel4.setText("Balance :"); jTextField2.setText("0"); jLabel5.setText("Enter Amount :"); jTextField3.setText("0"); jButton2.setText("Deposit"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Withdraw"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("New Account"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); status.setText("Not Logged in"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPasswordField1) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE) .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(59, 59, 59) .addComponent(jButton1))) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE) .addGap(13, 13, 13) .addComponent(jButton2) .addGap(48, 48, 48) .addComponent(jButton3) .addGap(85, 85, 85)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(154, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(116, 116, 116)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(150, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(74, 74, 74)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(350, Short.MAX_VALUE) .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(22, 22, 22) .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(14, 14, 14) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3) .addComponent(jButton2) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(40, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("**************** CREATE A NEW ACCOUNT ****************"); System.out.println("\n\n\n\n"); System.out.println("Enter new username :"); username[a1] = in.readLine(); System.out.println("Enter new password :"); password[a1] = Integer.parseInt(in.readLine()); System.out.println("Enter the amount to start with : "); balance[a1] = Double.parseDouble(in.readLine()); a1++; } catch(Exception e) { System.out.println("Error Occured!("+e+" : Restart the program :"); String a[]={"","","",""}; main(a); } } public void new_user() { String abutton = jTextField1.getText(); int bbutton = Integer.parseInt(jPasswordField1.getText()); for(int i=0;i<1000;i++) { if(abutton.equalsIgnoreCase(username[i])) { if(bbutton==password[i]) { status.setText("Logged in!"); posi=i; flag = true; break; } } } if(flag!=true) { status.setText("User not valid"); System.exit(0); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { new_user (); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { double input = Double.parseDouble(jTextField3.getText()); balance[a1]+=input; String ab = ""+balance[a1]; jTextField2.setText(ab); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { double input = Double.parseDouble(jTextField3.getText()); balance[a1]-=input; String ab = ""+balance[a1]; jTextField2.setText(ab); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Atm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPasswordField jPasswordField1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField status; // End of variables declaration }
Thank you... Looking forward for your help!
- 10-31-2010, 07:22 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems you are working on NetBeans. If so why don't you try to debug the code, it's easy with NetBeans. Did you try that?
-
Don't mix console and Swing coding, but rather do it all in the GUI. You'll not regret this. As for your specific error, it will be hard to tell without an SSCCE. Check out the link for details.
Good luck!
- 10-31-2010, 07:25 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
- 10-31-2010, 07:38 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Then lol, tell us what happen when you do that. Reason to say that is, most of us are lazy to run your code and find the errors for you. If you could provide more you've then yes, you can find lots of comments on your question.
- 10-31-2010, 07:44 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Few things to point on you,
1. As Fubarable says, don't messup the console and GUI. Your new account works on the console partially and then move to the GUI. It's not good, really dangerous when cross threading in UI.
2. Don't use arrays with large number of elements. I can see that you've initialize some arrays with 1000 elements. Think about what happen you've only 1 element, or may be you've 5000 elements. Collections class in Java has a nice interface to handle those scenarios, dynamic growth of the size.
- 10-31-2010, 07:47 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
ok.
I appreciate your "laziness"
here's my problem:
1) as soon as I execute the program the GUI comes up. ( no problem with that)

2)As soon as I press the "New Account" button, the console opens up.
3) as you can see, the console doesn't take any input.

4) The program now freezes and I have to go to the Task manager to end the process
- 10-31-2010, 07:51 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually I can do that. Seems something wrong with your PC, or may be with the BlueJ.
As I said this cross threading from GUI to console is not good. That may cause lots of issues on BlueJ (IDE) as well.
I can suggest you one thing. Run the project in console not from the BlueJ. Do you know how to do that?
- 10-31-2010, 07:51 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
--------------------------------------------------
So now, if I want to migrate completely to the GUI form, then how can I create a new JFrame without creating a new class.
If that is not possible, then how do I take global elements from the ATM class and use it for the newly created class?
- 10-31-2010, 07:54 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
- 10-31-2010, 08:00 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-31-2010, 08:03 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
- 10-31-2010, 08:03 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Run in using the command prompt.
Open the command prompt and change the working folder to location where the Java class "Atm.java" file is located.
Compile the Atm class using javac command and then run it using java command.
Check still your application freeze. I don't think it'll.
- 10-31-2010, 08:07 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
There are many ways to do that. As I said it depends on your design. Why you really need data from one class to another? I don't know you've notice that, but you stuck with that because there is no central location to get data in your application. From the main class you've initialize few arrays and try to store data there.
Rather doing that try to preserve data in a permanent location, in a text file or in a database and so on. Think about that once your application is close all data is loss.
- 10-31-2010, 08:13 AM #15
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
I agree with your point. i will implement in in my project in the future. Can you now please enlighten the path in which you can teach me how to bring and change global variables in another class?
- 11-02-2010, 03:42 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you know about access modifiers, public, private and so on?
- 11-02-2010, 04:17 AM #17
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
forget it
Its Ok. I know that. Forget it. I just finished my program. Here it is, if you want it. I has got no problem. Hurrah!
Java Code:/** * * @author Abhilash * Copyrighted * DO NOT COPY * PROPERTY OF ABHILASH */ import java.io.*; import javax.swing.*; public class Atm extends javax.swing.JFrame { String username[] = new String[100]; int password[] = new int[100]; double balance[]= new double[100]; int a1; int posi; boolean flag; /** Creates new form Atm */ public Atm() { initComponents(); } @SuppressWarnings("unchecked") private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); usernameField = new javax.swing.JTextField(); passwordField = new javax.swing.JPasswordField(); loginButton = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); balanceField = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); amountField = new javax.swing.JTextField(); depositButton = new javax.swing.JButton(); withdrawButton = new javax.swing.JButton(); newaccountButton = new javax.swing.JButton(); status = new javax.swing.JTextField(); logoutButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("The ATM by Abhilash"); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); jLabel1.setText(" ATM by ABHILASH"); jLabel2.setText("Username :"); jLabel3.setText("Password :"); loginButton.setText("Log In"); loginButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loginButtonActionPerformed(evt); } }); jLabel4.setText("Balance :"); balanceField.setEditable(false); balanceField.setText("0.0"); jLabel5.setText("Enter Amount :"); amountField.setText("0.0"); depositButton.setText("Deposit"); depositButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { depositButtonActionPerformed(evt); } }); withdrawButton.setText("Withdraw"); withdrawButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { withdrawButtonActionPerformed(evt); } }); newaccountButton.setText("New Account"); newaccountButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { newaccountButtonActionPerformed(evt); } }); status.setEditable(false); status.setText("Not Logged in"); logoutButton.setText("Log Out"); logoutButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { logoutButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(newaccountButton, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE) .addGap(13, 13, 13) .addComponent(depositButton) .addGap(48, 48, 48) .addComponent(withdrawButton) .addGap(85, 85, 85)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(154, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(116, 116, 116)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(150, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(74, 74, 74)) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(passwordField) .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGap(59, 59, 59) .addComponent(loginButton))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(logoutButton) .addGap(18, 18, 18) .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(loginButton) .addComponent(logoutButton))) .addGroup(layout.createSequentialGroup() .addGap(22, 22, 22) .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(14, 14, 14) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(withdrawButton) .addComponent(depositButton) .addComponent(newaccountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(40, Short.MAX_VALUE)) ); pack(); } private void newaccountButtonActionPerformed(java.awt.event.ActionEvent evt) { try{ new_user(); status.setText("New account created"); } catch(Exception e) { JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE); dispose(); String a[] = {"","","",""}; main(a); } } public void new_user() { try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("**************** CREATE A NEW ACCOUNT ****************"); System.out.println("\n\n\n"); System.out.println("Enter new username :"); username[a1] = in.readLine(); System.out.println("Enter new password :"); password[a1] = Integer.parseInt(in.readLine()); System.out.println("Enter the amount to start with : "); balance[a1] = Double.parseDouble(in.readLine()); System.out.println("Account successfully created !"); a1++; } catch(Exception e) { System.out.println("Error Occured!("+e+" : Restart the program :"); dispose(); String a[]={"","","",""}; main(a); } } private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) { try{ String abutton = usernameField.getText(); int bbutton = Integer.parseInt(passwordField.getText()); for(int i=0;i<1000;i++) { if(abutton.equalsIgnoreCase(username[i])) { if(bbutton==password[i]) { status.setText("Logged in!"); posi=i; flag = true; break; } } } if(flag!=true) { status.setText("User not valid"); System.exit(0); } if(flag){ String a=""+balance[posi]; balanceField.setText(a); usernameField.setEditable(false); passwordField.setEditable(false); amountField.setEditable(true); } } catch(Exception e) { JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE); dispose(); String a[] = {"","","",""}; main(a); } } private void depositButtonActionPerformed(java.awt.event.ActionEvent evt) { try{ if(flag) { double input = Double.parseDouble(amountField.getText()); balance[posi]=balance[posi]+input; String ab = ""+balance[posi]; balanceField.setText(ab); } else{ status.setText("User not logged in!"); } } catch(Exception e) { JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE); dispose(); String a[] = {"","","",""}; main(a); } } private void withdrawButtonActionPerformed(java.awt.event.ActionEvent evt) { try { if(flag) { double input = Double.parseDouble(amountField.getText()); balance[posi]=balance[posi]-input; String ab = ""+balance[posi]; balanceField.setText(ab);} else{ status.setText("User not logged in!"); } } catch(Exception e) { JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE); dispose(); String a[] = {"","","",""}; main(a); } } private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) { try{ posi = 0; flag = false; status.setText("Logged out"); balanceField.setText("0.0"); amountField.setText("0.0"); amountField.setEditable(false); usernameField.setEditable(true); passwordField.setEditable(true); } catch(Exception e) { JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE); dispose(); String a[] = {"","","",""}; main(a); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Atm().setVisible(true); } }); } // Variables declaration private javax.swing.JTextField amountField; private javax.swing.JTextField balanceField; private javax.swing.JButton depositButton; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JButton loginButton; private javax.swing.JButton logoutButton; private javax.swing.JButton newaccountButton; private javax.swing.JPasswordField passwordField; private javax.swing.JTextField status; private javax.swing.JTextField usernameField; private javax.swing.JButton withdrawButton; // End of variables declaration }
- 11-02-2010, 05:06 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's much better. So now you can mark the thread solved from tools menu.
Similar Threads
-
Programmer needed for J2ME project
By Sber in forum Reviews / AdvertisingReplies: 0Last Post: 11-04-2009, 02:51 PM -
help needed in runnin java project
By anurag.25 in forum Advanced JavaReplies: 14Last Post: 02-15-2009, 07:00 PM -
help needed with project
By Ice_Angel in forum JDBCReplies: 1Last Post: 06-22-2008, 08:00 PM -
Java experts needed- 30 minute online Java projects
By michelle in forum Jobs OfferedReplies: 0Last Post: 03-05-2008, 11:47 PM -
Uni Project - GUI - Help Needed
By deeadeed in forum New To JavaReplies: 0Last Post: 12-03-2007, 11:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks