Here is something you can do, for this situation, where you have multiple Forms or Windows
what you can do is to use a JPanel
which can have this CardLayout set as it's LayoutManager
and then you can add the two JPanel
s to it and access them with the methods provided by the same.
Don't use setBounds()
when using Absolute Positioning
this is really not the right way of putting components to the parent container. Instead use setLocation(...) and setSize(...) methods. Consider not to use Absolute Positioning as much as possible for you. Certain lines in favour of the before said line taken from Java Docs are as follows :
Although it is possible to do without a layout manager, you should use a
layout manager if at all possible. A layout manager makes it easier to
adjust to look-and-feel-dependent component appearances, to different
font sizes, to a container's changing size, and to different locales.
Layout managers also can be reused easily by other containers, as well as
other programs.
Since the output of your program is really not a soothing experience in any sense. Atleast LayoutManager, can make that work a lot more easier for you, since you need not have to specify position and size for each and every component. Try walking through the Layout Mangers Tutorials, and get accustomed to them as soon as possible. They are the real life savers :-)
Here is a modified code taken from your SOURCE CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
private JPanel contentPane;
public MyPanel(JPanel panel) {
contentPane = panel;
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setLocation(0, 0);
jcomp4.setSize(315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}