Reputation: 23
My program is about a supermarket. When I compile the program, both the JFrame windows 'f1' and 'f2' appear on the screen. However I want JFrame window 'f1' to appear first and then after clicking on the JButton 'b1' of 'f1' window, I want the JFrame window 'f2' to come. Below is the delivery() method of my program:
public static void delivery()
{
final JFrame f1 = new JFrame("Name");
GridLayout grid = new GridLayout(20, 40, 10, 8);
f1.setLayout(grid);
f1.setVisible(true);
f1.setSize(600,200);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setLocation(700,450);
JPanel p1 = new JPanel();
final JLabel l1 = new JLabel("Enter your name: ");
final JTextField jt1 = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input1 = jt1.getText();
f1.dispose();
}
});
p1.add(b1);
p1.add(l1);
p1.add(jt1);
f1.add(p1);
final JFrame f2 = new JFrame("Address");
f2.setVisible(true);
f2.setSize(600,200);
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setLocation(700,450);
JPanel p2 = new JPanel();
final JLabel l2 = new JLabel("Enter your address: ");
final JTextField jt2 = new JTextField(20);
JButton b2 = new JButton("Ok");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input2 = jt2.getText();
f2.dispose();
}
});
p2.add(b1);
p2.add(l2);
p2.add(jt2);
f2.add(p2);
JOptionPane.showMessageDialog(null, "The ordered stuff will be delivered to " +input1+ " who lives in: " +input2 , "Delivery" , JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Thank you for shopping at Paradise 24/7. Hope to see you again." , "Shopping Done!" , JOptionPane.PLAIN_MESSAGE);
}
Upvotes: 0
Views: 2222
Reputation: 921
Then you must read JavaDocs first and read some good ebooks for Java Beginner like Java 2 Complete Reference, O'Really - Java Swing will be helpful for you
Upvotes: 0
Reputation: 921
Simply add the code f2.setVisible(true);
in actionPerformed()
of the Button.
For e.g.
f1.setBounds(whatever);
f2.setBounds(whatever);
//add button in JFrame and actionListener
f1.setVisible(true);
f2.setVisible(false);
actionPerformed(ActionEvent e)
{
f2.setVisible(true);
}
Upvotes: 0
Reputation: 10959
The line of code that makes a frame appear is this
f1.setVisible(true);
You have this for both frames within your delivery method.
To make one appear after the other change this so that one gets set to visible and the other gets unset within the code for the button ie (you will obviously have to declare f2 before this though)
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input1 = jt1.getText();
f1.dispose();
//f1.setVisible(false); // or dispose if you no longer need it
f2.setVisible(true);
}
});
Just a suggestion: A better approach maybe to use a JDialog. This would allow you to get the input form the user wait for response, and then prompt for the next input. Click here for tutorial on Dialogs
You may also want to look at some layouts when adding components your frames/panels. GridLayout, BorderLayout, FlowLayout
Upvotes: 1