我有以下简单的代码,我不知道如何修改它,以便有3个单独的面板切换到,每个按钮一个:
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();
}
}
谢谢
答案 0 :(得分:1)
听起来你应该考虑使用JTabbedPane。
答案 1 :(得分:1)
除了How to Use Tabbed Panes之外,您还可以查看CardLayout
,提及here和here。
答案 2 :(得分:0)
您应该创建一个主容器:
JPanel mainContainer = new JPanel();
//creation of each child
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
...所以每个按钮必须将这些面板添加到主容器中并调整新的面板大小,如下所示:
//for button1:
mainContainer.add(panel1);
panel1.setSize(mainContainer.getSize());
...对于button2操作,您必须遵循上述相同的方式。