package com.JComB;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class JComboBoxEx extends JComboBox
{
public JComboBoxEx()
{
super();
}
public void firePopupMenuWillBecomeVisible()
{
int maxHight = 0;
BasicComboPopup comboPopup = (BasicComboPopup)getUI().getAccessibleChild(this, 0);
//此为 ComboPopup 接口的基本实现。 此类表示组合框的弹出部分的 UI
maxHight = comboPopup.getHeight();
System.out.println("11"+maxHight);
if (maxHight == 0)
//封装了一个构件的高度和宽度,这个类与一个构件的许多属性具有相关性,因此在Component类中定义多个与之有关的方法,
//LayoutManager接口也与一个Dimension对象有关联。Dimension类的高度和宽度值是一个整数,表明有多少个像素点
{ //getPreferredSize获得相应组件的大小
maxHight = comboPopup.getPreferredSize().height;
}
comboPopup.setLocation(getLocationOnScreen().x, getLocationOnScreen().y - maxHight);
super.firePopupMenuWillBecomeVisible();
}
}
package com.JComB;
/*
<applet code="MainFrame" width="300" height="100"></applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JApplet
{
JComboBoxEx box;
public MainFrame(){
}
public void init()
{
box = new JComboBoxEx();
Container c=getContentPane();
c.setLayout(null);
Rectangle rc = c.getBounds();
box.setBounds(new Rectangle(30,30,100, 30));
box.addItem("1");
box.addItem("2");
box.addItem("3");
box.addItem("4");
add(box);
}
}