import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
public class TabbedPaneDemo extends JFrame implements ActionListener{
private JTabbedPane m_tabbedPane;
private JButton m_topButton,m_bottomButton,m_leftButton,m_rightButton,m_addButton,m_removeButton;
public TabbedPaneDemo(){
super("TabbedPane test");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,6));
m_topButton = new JButton("top");
m_bottomButton = new JButton("bottom");
m_leftButton = new JButton("left");
m_rightButton = new JButton("right");
m_addButton = new JButton("add");
m_removeButton = new JButton("remove");
m_topButton.addActionListener(this);
m_bottomButton.addActionListener(this);
m_leftButton.addActionListener(this);
m_rightButton.addActionListener(this);
m_addButton.addActionListener(this);
m_removeButton.addActionListener(this);
buttonPanel.add(m_topButton);
buttonPanel.add(m_bottomButton);
buttonPanel.add(m_leftButton);
buttonPanel.add(m_rightButton);
buttonPanel.add(m_addButton);
buttonPanel.add(m_removeButton);
m_tabbedPane = new JTabbedPane(SwingConstants.TOP);
getContentPane().add("South",buttonPanel);
getContentPane().add("Center",m_tabbedPane);
creatTab();
creatTab();
creatTab();
m_tabbedPane.setSelectedIndex(0);
}
private void creatTab() {
JLabel label = null;
switch(m_tabbedPane.getTabCount() %3){
case 0:
label = new JLabel("Tab #" + m_tabbedPane.getTabCount() ,SwingConstants.CENTER);
break;
case 1:
label = new JLabel("Tab #" + m_tabbedPane.getTabCount() ,SwingConstants.CENTER);
break;
case 2:
label = new JLabel("Tab #" + m_tabbedPane.getTabCount() ,SwingConstants.CENTER);
break;
default:
break;
}
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setOpaque(true);
label.setBackground(Color.white);
m_tabbedPane.addTab("Tab #" + m_tabbedPane.getTabCount() , label);
}
public void deleteTab(){
if(m_tabbedPane.getTabCount()>0){
m_tabbedPane.removeTabAt(m_tabbedPane.getTabCount() - 1);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == m_topButton){
m_tabbedPane.setTabPlacement(SwingConstants.TOP);
}else if(e.getSource() == m_bottomButton){
m_tabbedPane.setTabPlacement(SwingConstants.BOTTOM);
}else if(e.getSource() == m_leftButton){
m_tabbedPane.setTabPlacement(SwingConstants.LEFT);
}else if(e.getSource() == m_rightButton){
m_tabbedPane.setTabPlacement(SwingConstants.RIGHT);
}else if(e.getSource() == m_addButton){
creatTab();
}else if(e.getSource() == m_removeButton){
deleteTab();
}
m_tabbedPane.revalidate();
m_tabbedPane.repaint();
}
public static void main(String args[]){
TabbedPaneDemo f = new TabbedPaneDemo();
f.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.pack();
f.show();
}
}
JTabbedPane
最新推荐文章于 2021-02-20 18:08:49 发布