Java Swing 可关闭的TabbedPane

Java Swing 可关闭的TabbedPane
可关闭的TabbedPane结构:


测试代码:

package  cn.demo.test;

import  java.awt.Component;

import  javax.swing.JFrame;
import  javax.swing.JLabel;
import  javax.swing.UIManager;

/**
 * Test
 * 
@author  Tom
 *
 
*/
public   class  TestDemo {

    
public   static   void  main(String[] args) {
        
try  {
            String feel 
=  UIManager.getSystemLookAndFeelClassName();
            UIManager.setLookAndFeel(feel);
        } 
catch  (Exception e) {
            e.printStackTrace();
        } 
        
        JFrame frame 
=   new  JFrame();
        frame.setTitle(
" 可关闭Tab测试 " );
        frame.setSize(
300 400 );
        frame.setLocationRelativeTo(
null );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        TabbedPane tabbedPane 
=   new  TabbedPane();
        tabbedPane.setCloseButtonEnabled(
true );
        tabbedPane.addTab(
" 测试一 " null new  JLabel( " 测试一 " ));
        tabbedPane.addTab(
" 测试二 " null new  JLabel( " 测试二 " ));
        tabbedPane.addTab(
" 测试三 " null new  JLabel( " 测试三 " ));
        tabbedPane.addTab(
" 测试四 " null new  JLabel( " 测试四 " ));
        tabbedPane.addTabbedPaneListener(
new  TabbedPaneListener(){
            @Override
            
public   void  allTabsRemoved() {
                
//  TODO Auto-generated method stub
                
            }
            @Override
            
public   boolean  canTabClose(Tab tab, Component component) {
                
//  TODO Auto-generated method stub
                 return   false ;
            }
            @Override
            
public   void  tabAdded(Tab tab, Component component,  int  index) {
                
//  TODO Auto-generated method stub
                
            }
            @Override
            
public   void  tabRemoved(Tab tab, Component component,  int  index) {
                
//  TODO Auto-generated method stub
                System.out.println( " close " );
            }
            @Override
            
public   void  tabSelected(Tab tab, Component component,  int  index) {
                
//  TODO Auto-generated method stub
                
            }
        });
        
        frame.add(tabbedPane);
        frame.setVisible(
true );
    }
    
}

 测试效果:

     

源码下载:TabbedPane.rar

发表人: Tom


你可能感兴趣的:(Java Swing 可关闭的TabbedPane)