/** TabbedPaneTest.java
* @2015-06-02*/
importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ButtonGroup;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JRadioButton;importjavax.swing.JTabbedPane;importjavax.swing.event.ChangeEvent;importjavax.swing.event.ChangeListener;public classTabbedPaneTest {public static voidmain(String[] args) {//TODO Auto-generated method stub
TabbedPaneFrame frame= newTabbedPaneFrame();
frame.setTitle("TabbedPaneFrame");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class TabbedPaneFrame extendsJFrame {privateJTabbedPane tabbedPane;private int count = 0;publicTabbedPaneFrame() {//添加选项卡
tabbedPane= newJTabbedPane();
tabbedPane.addTab("Mercury", null);
tabbedPane.addTab("Venus", null);
tabbedPane.addTab("Earth", null);
tabbedPane.addTab("Mars", null);
tabbedPane.addTab("Jupiter", null);
tabbedPane.addTab("Saturn", null);
tabbedPane.addTab("Uranus", null);
tabbedPane.addTab("Neptune", null);
tabbedPane.addTab("Pluto", null);//添加选项卡面板
add(tabbedPane,"Center");//添加监听器
tabbedPane.addChangeListener(newChangeListener() {
@Overridepublic voidstateChanged(ChangeEvent e) {//TODO Auto-generated method stub
int n =tabbedPane.getSelectedIndex();
loadTab(n);
}
});
loadTab(0);//添加单选按钮,用于调节选项卡的布局方式
JPanel buttonPanel= newJPanel();
ButtonGroup buttonGroup= newButtonGroup();
JRadioButton wrapButton= new JRadioButton("Wrap tabs");
wrapButton.setSelected(true);
wrapButton.addActionListener(newActionListener() {
@Overridepublic voidactionPerformed(ActionEvent arg0) {//TODO Auto-generated method stub
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
}
});
buttonGroup.add(wrapButton);
buttonPanel.add(wrapButton);
JRadioButton scroButton= new JRadioButton("Scroll tabs");
scroButton.addActionListener(newActionListener() {
@Overridepublic voidactionPerformed(ActionEvent arg0) {//TODO Auto-generated method stub
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
});
buttonGroup.add(scroButton);
buttonPanel.add(scroButton);
add(buttonPanel, BorderLayout.SOUTH);
}private void loadTab(intn) {
String title=tabbedPane.getTitleAt(n);
String countString= String.valueOf(count ++);
String msg= "this is " + title + ", load at " + countString + " times";
tabbedPane.setComponentAt(n,newJLabel(msg));
}
}