为什么ComboBox显示不出来

时间:2021-06-02 08:43:15

/*
 *
 * 
 */

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却显示不出来,求解决方法!

6 个解决方案

#1


p.setLayout(null);//空布局  任意摆放
这句去掉

#2


f.setContentPane(p);//不可直接用f.add(p);
现在的api已经可以直接用f.add(p);了,你可以试一试

#3


p.setLayout(null);//空布局的话需要p.setBounts(x,y,w,h);来定位显示的位置及大小。

#4


JPanle默认是流式布局 FlowLayout

#5


引用 1 楼  的回复:
p.setLayout(null);//空布局  任意摆放
这句去掉

去掉之后确实是可以了,可是我是想让那个JComboBox控件放在中间,去掉之后,那个控件就被放在最上部了;该怎么办呢?

#6


引用 3 楼  的回复:
p.setLayout(null);//空布局的话需要p.setBounts(x,y,w,h);来定位显示的位置及大小。

按照你说的方法设置位置没什么效果啊

#1


p.setLayout(null);//空布局  任意摆放
这句去掉

#2


f.setContentPane(p);//不可直接用f.add(p);
现在的api已经可以直接用f.add(p);了,你可以试一试

#3


p.setLayout(null);//空布局的话需要p.setBounts(x,y,w,h);来定位显示的位置及大小。

#4


JPanle默认是流式布局 FlowLayout

#5


引用 1 楼  的回复:
p.setLayout(null);//空布局  任意摆放
这句去掉

去掉之后确实是可以了,可是我是想让那个JComboBox控件放在中间,去掉之后,那个控件就被放在最上部了;该怎么办呢?

#6


引用 3 楼  的回复:
p.setLayout(null);//空布局的话需要p.setBounts(x,y,w,h);来定位显示的位置及大小。

按照你说的方法设置位置没什么效果啊