利用JTabbedPane类中的addTab方法即可实现你的要求!! 源码/** * Adds a <code>component</code> and <code>tip</code> * represented by a <code>title</code> and/or <code>icon</code>, * either of which can be <code>null</code>. * Cover method for <code>insertTab</code>. * * @param title the title to be displayed in this tab * @param icon the icon to be displayed in this tab * @param component the component to be displayed when this tab is clicked * @param tip the tooltip to be displayed for this tab * * @see #insertTab * @see #removeTabAt */ public void addTab(String title, Icon icon, Component component, String tip) { insertTab(title, icon, component, tip, pages.size()); }
import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.JPanel; import java.awt.FlowLayout; import javax.swing.JButton; import java.awt.Container; import java.awt.Color; import java.awt.Font; import java.awt.CardLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JLabel;public class FrameTest extends JFrame { static final int SIZE = 6; private JButton[] buttons = new JButton[SIZE]; private JPanel upPanel; //上部窗体 private JPanel centerPanel; //中部窗体 private JPanel[] insidePanles = new JPanel[SIZE]; //内部窗体 private CardLayout cardLayout = new CardLayout(); //布局方式 private final String[] name={"按钮1","按钮2","按钮3","按钮4","按钮5","按钮6"}; private final String names = "内部窗口";
ActionListener listener = new ActionListener(){ public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); cardLayout.show(centerPanel,button.getLabel()); } };
public FrameTest(){ super("Frame");
//设置布局方式为BorderLayout this.setLayout(new BorderLayout()); //创建upPanel upPanel = new JPanel(); upPanel.setLayout(new FlowLayout());//设置布局方式为FlowLayout //创建中部面板 centerPanel = new JPanel(); centerPanel.setLayout(cardLayout);
for(int i = 0;i<SIZE;i++){ //实例化buttons,并设置属性 buttons[i] = new JButton("按钮"+(i+1)); buttons[i].setFont(new Font("宋体", 4, 15)); buttons[i].setBackground(Color.yellow); buttons[i].addActionListener(listener); upPanel.add(buttons[i]);
insidePanles[i] = new JPanel(); insidePanles[i].add(new JLabel("内部窗口"+(i+1))); insidePanles[i].setBackground(Color.yellow); centerPanel.add(insidePanles[i],name[i]); }
this.add(upPanel,BorderLayout.NORTH); this.add(centerPanel,BorderLayout.CENTER); this.setVisible(true); this.setSize(450,300); } public static void main(String[] args){ FrameTest ft = new FrameTest(); } }
源码/**
* Adds a <code>component</code> and <code>tip</code>
* represented by a <code>title</code> and/or <code>icon</code>,
* either of which can be <code>null</code>.
* Cover method for <code>insertTab</code>.
*
* @param title the title to be displayed in this tab
* @param icon the icon to be displayed in this tab
* @param component the component to be displayed when this tab is clicked
* @param tip the tooltip to be displayed for this tab
*
* @see #insertTab
* @see #removeTabAt
*/
public void addTab(String title, Icon icon, Component component, String tip) {
insertTab(title, icon, component, tip, pages.size());
}
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;public class FrameTest extends JFrame
{
static final int SIZE = 6;
private JButton[] buttons = new JButton[SIZE];
private JPanel upPanel; //上部窗体
private JPanel centerPanel; //中部窗体
private JPanel[] insidePanles = new JPanel[SIZE]; //内部窗体
private CardLayout cardLayout = new CardLayout(); //布局方式
private final String[] name={"按钮1","按钮2","按钮3","按钮4","按钮5","按钮6"};
private final String names = "内部窗口";
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
cardLayout.show(centerPanel,button.getLabel());
}
};
public FrameTest(){
super("Frame");
//设置布局方式为BorderLayout
this.setLayout(new BorderLayout());
//创建upPanel
upPanel = new JPanel();
upPanel.setLayout(new FlowLayout());//设置布局方式为FlowLayout
//创建中部面板
centerPanel = new JPanel();
centerPanel.setLayout(cardLayout);
for(int i = 0;i<SIZE;i++){
//实例化buttons,并设置属性
buttons[i] = new JButton("按钮"+(i+1));
buttons[i].setFont(new Font("宋体", 4, 15));
buttons[i].setBackground(Color.yellow);
buttons[i].addActionListener(listener);
upPanel.add(buttons[i]);
insidePanles[i] = new JPanel();
insidePanles[i].add(new JLabel("内部窗口"+(i+1)));
insidePanles[i].setBackground(Color.yellow);
centerPanel.add(insidePanles[i],name[i]);
}
this.add(upPanel,BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(450,300);
} public static void main(String[] args){
FrameTest ft = new FrameTest();
}
}