8 Replies - 6661 Views - Last Post: 04 November 2010 - 05:30 AM Rate Topic: -----

#1 nelsonyap48  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-March 10

ATM simulation with GUI

Posted 01 November 2010 - 02:57 AM

I am working on an ATM simulation program. I have no problem with the console/terminal coding(without GUI), and now I plan to write one with basic GUI features but i have no experience in writing GUI codes. After reading some tutorials, I started coding but i got stuck.

My idea is after the user entered all the fields and clicked on the "create account" button, the "first window" will be replaced with the "second" one.

First Window: 3 Jtextfields with Jlabels (name,id,balance), Jbutton("create account")

Second Window: 4 JButtons(deposit,withdraw,check balance,exit)


Problem 1: How do I switch/replace the window after the click? Is it using JPanel or something else?

Problem 2: How do I arrange the Jtextfields & JButton, because when I added them into the JFrame, they're arrange in a horizontal line and the arrangement changes as I resize the window. I want it to be something like this:
Name:[ ]
ID:[ ]
Balance:[ ]
(Create Account)

I know how to code the "windows" separately but do not know how to fuse it together.

Thanks in advance;)

Is This A Good Question/Topic? 0
  • +

Replies To: ATM simulation with GUI

#2 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: ATM simulation with GUI

Posted 01 November 2010 - 06:08 AM

Problem #1: myFrame.dispose()

Problem #2: look into layouts Layouts

what do you mean with this?

Quote

I know how to code the "windows" separately but do not know how to fuse it together.

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: ATM simulation with GUI

Posted 01 November 2010 - 06:13 AM

View PostHandler, on 01 November 2010 - 09:08 AM, said:

Problem #1: myFrame.dispose()

Expanding on this some, if you want it to occur on a button click, you will need to use an ActionListener for the specific JButton. For the first JFrame, you will need to alter visibility using the setVisible() method as well as the dispose() method to free up resources. Then set the other frame as visible.
Was This Post Helpful? 0
  • +
  • -

#4 nelsonyap48  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-March 10

Re: ATM simulation with GUI

Posted 01 November 2010 - 06:50 AM

View Postmacosxnerd101, on 01 November 2010 - 05:13 AM, said:

View PostHandler, on 01 November 2010 - 09:08 AM, said:

Problem #1: myFrame.dispose()

Expanding on this some, if you want it to occur on a button click, you will need to use an ActionListener for the specific JButton. For the first JFrame, you will need to alter visibility using the setVisible() method as well as the dispose() method to free up resources. Then set the other frame as visible.


I'm able to solve the ActionListener codes. My main idea is combining 2 JFrame into one instead of setting the visibility, is that possible?
Was This Post Helpful? 0
  • +
  • -

#5 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: ATM simulation with GUI

Posted 01 November 2010 - 07:06 AM

no it is not, what would need be done is use 2 panels.

so all you would do is:

pack your second panel
remove first panel
add second panel
Was This Post Helpful? 0
  • +
  • -

#6 nelsonyap48  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-March 10

Re: ATM simulation with GUI

Posted 01 November 2010 - 09:25 AM

View PostHandler, on 01 November 2010 - 06:06 AM, said:

no it is not, what would need be done is use 2 panels.

so all you would do is:

pack your second panel
remove first panel
add second panel


Alright, may i know how to do so? I mean remove the first panel and add the second one. And what you mean by "pack"?
Was This Post Helpful? 0
  • +
  • -

#7 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: ATM simulation with GUI

Posted 01 November 2010 - 10:48 AM

An example:
import javax.swing.*;
import java.awt.event.*;
public class SwitchPanels implements ActionListener
{
    JFrame frame = new JFrame ();
    JPanel panel1 = new JPanel ();
    JPanel panel2 = new JPanel ();
    JLabel lbl1 = new JLabel ("panel 1");
    JLabel lbl2 = new JLabel ("panel 2");
    JButton btn = new JButton ("Click me");
    int pane = 0;

    SwitchPanels ()
    {
        frame.setSize (100, 100);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        panel1.add (lbl1);
        panel1.add (btn);
        frame.getContentPane ().add (panel1);
        btn.addActionListener (this);
        frame.setVisible (true);
    }


    void packPanel2 ()
    {
        panel2.add (lbl2);
        panel2.add (btn);
    }


    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == btn)
        {
            if (pane == 0)
            {
                pane++;
                frame.getContentPane ().remove (panel1);
                packPanel2 ();
                frame.getContentPane ().add (panel2);
                frame.validate ();
            }
        }
    }


    public static void main (String[] args)
    {
        SwitchPanels con = new SwitchPanels ();
    }
}



Pack was the wrong word to use, add is the right one.
Now What i did here was:
1) Create my first panel and added lbl1 to it
2) When the btn is clicked it switches to the 2nd panel
3) Set panel1, as well as everything used on it to null

ps:
the validate is called to refresh the frame.
setting it all to null is just a 'lil' bit safer and uses a tiny bit less resources :P

Ask if you are wondering 'bout anything
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: ATM simulation with GUI

Posted 01 November 2010 - 10:56 AM

View Postnelsonyap48, on 01 November 2010 - 12:25 PM, said:

Alright, may i know how to do so? I mean remove the first panel and add the second one. And what you mean by "pack"?

He means to invoke the pack() method on your JPanel to consolidate the layout. It helps in scaling your GUI to a reasonable and necessary size.
Was This Post Helpful? 1
  • +
  • -

#9 nelsonyap48  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-March 10

Re: ATM simulation with GUI

Posted 04 November 2010 - 05:30 AM

Thanks guys! Wrote the program in an hour and it works like a charm;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1