日期:2014-05-20  浏览次数:20772 次

为什么ComboBox显示不出来
Java code

/*
 *
 * 
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import javax.swing.*;

public class Splash_Window extends   JWindow {
    
    ImageIcon imageIcon;
    public   Splash_Window(final JFrame f,int waitTime) 
    { 
        
        super(f); 
        imageIcon=new   ImageIcon(getClass().getResource("/").getPath().replace("%20", " ")+"image/xinlangweibo.jpg");
        f.setSize(this.imageIcon.getIconWidth(), this.imageIcon.getIconHeight());
        f.setTitle("请选择");
        
        f.setDefaultCloseOperation(3);
        f.setUndecorated(true);
        
        if(imageIcon==null)
            System.out.println("imageIcon null ");
        JPanel p=new JPanel(){
            public void paintComponent(Graphics g){//重写paintComponent
                super.paintComponent(g);//继承的绘制组件方法
                
                Graphics2D gg=(Graphics2D)g;
                if(imageIcon !=null){//绘制背景
                    gg.drawImage(imageIcon.getImage(),0,0,f.getWidth(),f.getHeight(),f);
                    System.out.println("draw ");
                }
                
            }
        };
        p.setLayout(null);//空布局  任意摆放
        String [] selectItem=new String[]{"请选择关联文件","关联数据库","关联已存文件","默认"};
        JComboBox   ComboBox_Select=new JComboBox(selectItem);
        ComboBox_Select.setVisible(true);
        p.add(ComboBox_Select);
        ComboBox_Select.setLocation(f.getWidth()/2, f.getHeight()/2);
        Dimension   screenSize   = Toolkit.getDefaultToolkit().getScreenSize(); 
        f.setBounds((screenSize.width-this.imageIcon.getIconWidth())/2, (screenSize.height-this.imageIcon.getIconHeight())/2, this.imageIcon.getIconWidth(), this.imageIcon.getIconHeight());
        System.out.println((screenSize.width-this.imageIcon.getIconWidth())/2+" "+(screenSize.height-this.imageIcon.getIconHeight())/2);
        System.out.println(this.imageIcon.getIconWidth()+" "+this.imageIcon.getIconHeight());
        f.setContentPane(p);//不可直接用f.add(p);
    
//        f.add(ComboBox_Select);
        f.setVisible(true);

        pack(); 

        f.addMouseListener(new   MouseAdapter() 
        { 
            public   void   mousePressed(MouseEvent   e) { 
                setVisible(false); 
                System.exit(0);
                dispose(); 
            } 
        }); 
        
    }

    public static void main(String [] args){
        final JFrame f=new JFrame();
        Splash_Window sWindow=new Splash_Window(f,10000);
    }
    
    
}



运行之后只有一个自己做的窗体,ComboBox却显示不出来,求解决方法!

------解决方案--------------------
p.setLayout(null);//空布局 任意摆放
这句去掉
------解决方案--------------------
JPanle默认是流式布局 FlowLayout