开发者

(Java Newbie - Panel Transitions) How do I switch between panels in a frame

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 Locat开发者_开发百科ions");
    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


Sounds like you should consider using JTabbedPane.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜