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;)
ATM simulation with GUIneed advice
Page 1 of 1
8 Replies - 6661 Views - Last Post: 04 November 2010 - 05:30 AM
Replies To: ATM simulation with GUI
#2
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?
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.
#3
Re: ATM simulation with GUI
Posted 01 November 2010 - 06:13 AM
Handler, 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.
#4
Re: ATM simulation with GUI
Posted 01 November 2010 - 06:50 AM
macosxnerd101, on 01 November 2010 - 05:13 AM, said:
Handler, 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?
#5
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
so all you would do is:
pack your second panel
remove first panel
add second panel
#6
Re: ATM simulation with GUI
Posted 01 November 2010 - 09:25 AM
Handler, 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
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"?
#7
Re: ATM simulation with GUI
Posted 01 November 2010 - 10:48 AM
An example:
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
Ask if you are wondering 'bout anything
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
Ask if you are wondering 'bout anything
#8
Re: ATM simulation with GUI
Posted 01 November 2010 - 10:56 AM
nelsonyap48, 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.
#9
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;)
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|