0

I have the following simple code and I don't know how to modify it so as to have 3 separate panels to switch to, one for each button:

package TouristLocations;

import javax.swing.*;

import java.awt.*;

public class buildApp extends JFrame {
  /**
     * 
     */
    private static final long serialVersionUID = 1L;

public static void main(String[] args){
    JFrame frame = new JFrame("Test");
    frame.setSize(400,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("Locations");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    frame.add(title, c);

    JButton b1 = new JButton("View Locations");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    frame.add(b1, c);

    JButton b2 = new JButton("Insert Locations");
    c.gridx = 1;
    c.gridy = 1;
    frame.add(b2, c);

    JButton b3 = new JButton("Help");
    c.gridx = 2;
    c.gridy = 1;
    frame.add(b3, c);

    TextArea text1 = new TextArea(15,40);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 3;
    frame.add(text1, c);

    frame.pack();


  }
}

thank you

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user573382
  • 343
  • 3
  • 10
  • 22
  • @ user573382 maybe similair topic http://stackoverflow.com/questions/6010915/change-contentpane-of-frame-after-button-clicked – mKorbel May 15 '11 at 20:45

3 Answers3

1

Sounds like you should consider using JTabbedPane.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

In addition to How to Use Tabbed Panes, you may want to look at CardLayout, mentioned here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

You should create a main container:

JPanel mainContainer = new JPanel();

//creation of each child
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

... so each button must add those panels into the main container and resize new panel size, something like this:

//for button1:
mainContainer.add(panel1);
panel1.setSize(mainContainer.getSize());

... for button2 action, you must follow the same way above.

Alex
  • 1