当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

JComboBox的下拉框向上[Java编程]

赞助商链接



  本文“JComboBox的下拉框向上[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

近来学习SWING,改变JComboBox的的显示方法,这里是将JComboBox的下拉框向上显示,以下为具体代码:

package kissJava.JComboBox;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
public class PopupComboSample extends JFrame...{
   String labels[] = ...{ "Chardonnay", "Sauvignon", "Riesling", "Cabernet",
           "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc",
           "Syrah", "Gewurztraminer" };
   JComboBox jcb = null;
  public PopupComboSample()...{
   this.setTitle("下拉框向上");
    this.setLayout(null);
     jcb = new JComboBox(labels);
     //setUI.......
     jcb.setUI((ComboBoxUI) MyComboBoxUI.createUI(jcb));
     jcb.setBounds(50, 150, 200, 20);
     this.add(jcb, null);
     setSize(300, 300);
     setVisible(true);
  }

   public static void main(String args[]) ...{

      new PopupComboSample();

   }
   static class MyComboBoxUI extends BasicComboBoxUI ...{
    int newsBarTitleWidth = 0;

     public static ComponentUI createUI(JComponent c) ...{
       return new MyComboBoxUI();
     }
     //改变箭头的方向
     protected JButton createArrowButton() ...{
       JButton button = new BasicArrowButton(BasicArrowButton.NORTH);
       return button;
     }
     //改变comboBox的弹出方向
     protected ComboPopup createPopup() ...{
      BasicComboPopup popup = new BasicComboPopup(comboBox)...{
       public void show() ...{
          //大小按需求更改
           Dimension popupSize = new Dimension(200 - newsBarTitleWidth - 5, 17 * 5);
           scroller.setMaximumSize(popupSize);
           scroller.setPreferredSize(popupSize);
           scroller.setMinimumSize(popupSize);
           show(comboBox, newsBarTitleWidth, -17 * 5);//getPopupHeightForRowCount(10));
     }
   };
   popup.getAccessibleContext().setAccessibleParent(comboBox);
   return popup;
  }
   }
}

显示效果为:

不过看图的效果jcb的输入框没有线条显示,看来得再改改,学习中.


  以上是“JComboBox的下拉框向上[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • Java中操纵JCOM操作Office对象
  • JComboBox的下拉框向上
  • Java中操作JCOM实现仿Excel编程
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .