自定义组合框

import  javax.swing. * ;
import  java.awt. * ;

public   class  JComboBoxCustomTest{
    JFrame mainFrame;
    JPanel mainPanel;
    JComboBox customComboBox;
    CustomComboBoxRenderer customComboBoxRenderer;
    
    
public  JComboBoxCustomTest(){
        mainFrame 
=   new  JFrame( " JComboBoxCustomTest " );
        mainPanel 
=   new  JPanel();
        customComboBoxRenderer 
=   new  CustomComboBoxRenderer();
        customComboBox 
=   new  JComboBox();
        
        customComboBoxRenderer.setPreferredSize( 
new  Dimension( 200 , 50 ));
        customComboBox.setRenderer(customComboBoxRenderer);
        customComboBox.addItem(
" dd " ); // 随便加上两个item,"dd"在这里没有任何用处
        customComboBox.addItem( " dd " );
        
        mainPanel.add(customComboBox);
        mainFrame.getContentPane().add(mainPanel,BorderLayout.PAGE_START);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setVisible(
true );
    }
    
public   static   void  main(String[] args){
        SwingUtilities.invokeLater(
new  Runnable(){
            
public   void  run(){
                
new  JComboBoxCustomTest();
            }
        });
    }
}
class  CustomComboBoxRenderer  implements  ListCellRenderer{
    
public  CustomComboBoxRenderer(){
        setOpaque(
true );
    }
    
public  Component getListCellRendererComponent(
        JList list,
        Object value,
        
int  index,
        
boolean  isSelected,
        
boolean  cellHasFocus)
    {
        
return   new  JCheckBox( " chekcbox " );
    }
}

你可能感兴趣的:(职场,休闲,自定义组合框)