成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Swing不丑系列:JComboBox

開發 后端
話說JComboBox 默認外觀確實不是太好看..在由于本人有時實在太賤.就是想要它好看,所以這不又折騰了,其實這個ComboBox 剛開始想改變外觀 只考慮到renderer 畢竟swing上所有的界面 顯示全靠renderer來控制.

一直都是一只菜鳥..不管別人怎么說.

[[20608]]

 

一直認為Swing不丑..所以打算這段時間寫個Swing不丑系列(獻丑了)

[[20609]]

 

 

-----------------------------

 

話說JComboBox 默認外觀確實不是太好看..在由于本人有時實在太賤.就是想要它好看...所以..這不..又折騰了..

其實這個ComboBox 剛開始想改變外觀 只考慮到renderer 畢竟swing上所有的界面 顯示全靠renderer來控制..

所以 著手寫ComboBoxRenderer. 總是覺的 JComboBox 似乎不太好搞定 因為它不但有顯示框 還有小鍵頭.

還有彈出的List 還有ScrollBar..等等.. 似乎不那么好搞...不過Swing是強大的 ..只要你能想到..就可以做到.

那么我們要做幾件事.

1: 重載JComboBox 并且設置面板透明

2: 新建renderer 實現ListCellRenderer接口

3: 重載BasicComboBoxUI

1.重載JComboBox 并且設置面板透明 設置renderer 以及設置 ui

 

  1. public class IComboBox extends JComboBox{  
  2.    
  3.  public IComboBox(){  
  4.   super();  
  5.   init();  
  6.  }  
  7.  public IComboBox(ComboBoxModel model){  
  8.   super(model);  
  9.   init();  
  10.  }  
  11.  public IComboBox(Object[] items){  
  12.   super(items);  
  13.   init();  
  14.  }  
  15.  public IComboBox(Vector items){  
  16.   super(items);  
  17.   init();  
  18.  }  
  19.  private void init(){  
  20.   setOpaque(false);  
  21.   setUI(new IComboBoxUI());  
  22.   setRenderer(new IComboBoxRenderer());  
  23.   setBackground(XUtil.defaultComboBoxColor);  
  24.  }  
  25.  public Dimension getPreferredSize(){  
  26.   return super.getPreferredSize();  
  27.  }  

 

 

2.新建renderer 實現ListCellRenderer接口.注意.這里的ComboBoxRenderer它是控制combobox彈出的List 并非控制JComboBox的 注意 他實現的是ListCellRenderer

 

  1. public class IComboBoxRenderer implements ListCellRenderer {  
  2.    
  3.  private DefaultListCellRenderer defaultCellRenderer = new DefaultListCellRenderer();  
  4.  
  5.  public IComboBoxRenderer() {  
  6.   super();  
  7.  }  
  8.  
  9.  public Component getListCellRendererComponent(JList list, Object value,  
  10.    int index, boolean isSelected, boolean cellHasFocus) {  
  11.  
  12.   JLabel renderer = (JLabel)defaultCellRenderer.getListCellRendererComponent(  
  13.     list, value, index, isSelected, cellHasFocus);  
  14.   if(isSelected){  
  15.    renderer.setBackground(XUtil.defaultComboBoxBoundsColor);  
  16.    renderer.setForeground(Color.WHITE);  
  17.   }else{  
  18.    renderer.setBackground(Color.WHITE);  
  19.   }  
  20.   list.setSelectionBackground(XUtil.defaultComboBoxColor);  
  21.   list.setBorder(null);  
  22.   renderer.setFont(XUtil.defaultComboBoxFont);  
  23.   renderer.setHorizontalAlignment(JLabel.CENTER);  
  24.   return renderer;  
  25.  }  

 

 

3重載BasicComboBoxUI .sure 這里當然要注意.因為他是JComboBox的繪制機制

這里包括ComboBox右邊的那個箭頭的Button.(我們已經通過重寫 createArrowButton 來改變這個Button);

至于彈出的List ,it in here, look it ..createPoput(); it create ComboPopup.(不好意思 最近在學英文 總是那么順口來那么幾句.)

這里存在一個ScrollPane 它包含了List.并且我們重寫ScrollPane的paintBorder方法 來讓我們畫出List的Border

 

  1. public class IComboBoxUI extends BasicComboBoxUI {  
  2.  
  3.  private JButton arrow;  
  4.  private boolean boundsLight = false;  
  5.  private static final int ARCWIDTH = 15;  
  6.  private static final int ARCHEIGHT = 15;  
  7.  
  8.  public IComboBoxUI() {  
  9.   super();  
  10.  }  
  11.  
  12.  protected JButton createArrowButton() {  
  13.   arrow = new JButton();  
  14.   arrow.setIcon(XUtil.defaultComboBoxArrowIcon);  
  15.   arrow.setRolloverEnabled(true);  
  16.   arrow.setRolloverIcon(XUtil.defaultComboBoxArrowIcon_Into);  
  17.   arrow.setBorder(null);  
  18.   arrow.setBackground(XUtil.defaultComboBoxColor);  
  19.   arrow.setOpaque(false);  
  20.   arrow.setContentAreaFilled(false);  
  21.   return arrow;  
  22.  }  
  23.  
  24.  public void paint(Graphics g, JComponent c) {  
  25.   hasFocus = comboBox.hasFocus();  
  26.   Graphics2D g2 = (Graphics2D)g;  
  27.   if (!comboBox.isEditable()) {  
  28.    Rectangle r = rectangleForCurrentValue();  
  29.    //重點:JComboBox的textfield 的繪制 并不是靠Renderer來控制 這點讓我很吃驚.  
  30.    //它會通過paintCurrentValueBackground來繪制背景  
  31.    //然后通過paintCurrentValue();去繪制JComboBox里顯示的值  
  32.    paintCurrentValueBackground(g2, r, hasFocus);  
  33.    paintCurrentValue(g2, r, hasFocus);  
  34.   }  
  35.     
  36.   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  37.     RenderingHints.VALUE_ANTIALIAS_ON);  
  38.   int width = (intthis.getPreferredSize(c).getWidth()  
  39.     + arrow.getWidth() - 2;  
  40.   int height = 0;  
  41.   int heightOffset = 0;  
  42.   if (comboBox.isPopupVisible()) {  
  43.    heightOffset = 5;  
  44.    height = (intthis.getPreferredSize(c).getHeight();  
  45.    arrow.setIcon(XUtil.defaultComboBoxArrowIcon_Into);  
  46.   } else {  
  47.    heightOffset = 0;  
  48.    height = (intthis.getPreferredSize(c).getHeight() - 1;  
  49.    arrow.setIcon(XUtil.defaultComboBoxArrowIcon);  
  50.   }  
  51.   if (comboBox.isFocusable()) {  
  52.    g2.setColor(new Color(150207254));  
  53.   }  
  54.   g2.drawRoundRect(00, width, height + heightOffset,ARCWIDTH,ARCHEIGHT);  
  55.  }  
  56.  
  57.  public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) {  
  58.   Font oldFont = comboBox.getFont();  
  59.   comboBox.setFont(XUtil.defaultComboBoxFont);  
  60.     
  61.   super.paintCurrentValue(g, bounds, hasFocus);  
  62.   comboBox.setFont(oldFont);  
  63.  }  
  64.  
  65.  public Dimension getPreferredSize(JComponent c) {  
  66.   return super.getPreferredSize(c);  
  67.  }  
  68.  
  69.  public boolean isBoundsLight() {  
  70.   return boundsLight;  
  71.  }  
  72.  
  73.  public void setBoundsLight(boolean boundsLight) {  
  74.   this.boundsLight = boundsLight;  
  75.  }  
  76.  
  77.  protected ComboPopup createPopup() {  
  78.   ComboPopup popup = new BasicComboPopup(comboBox) {  
  79.    protected JScrollPane createScroller() {  
  80.     IScrollPane sp = new IScrollPane(list,  
  81.       ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,  
  82.       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  
  83.     sp.setHorizontalScrollBar(null);  
  84.     return sp;  
  85.    }  
  86.    //重載paintBorder方法 來畫出我們想要的邊框..  
  87.    public void paintBorder(Graphics g){  
  88.     Graphics2D g2 = (Graphics2D) g;  
  89.     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  90.       RenderingHints.VALUE_ANTIALIAS_ON);  
  91.     g2.setColor(new Color(150207254));  
  92.     g2.drawRoundRect(0,-arrow.getHeight(),getWidth()-1,getHeight()+arrow.getHeight()-1,0,0);  
  93.    }  
  94.   };  
  95.   return popup;  
  96.  }  

 

 

ok. 那么到這里 ComboBox這塊已經end 但是似乎還有個問題存在 那就是createPopup 方法里的ScrollPane的滾動條還是有點丑.

so。.next 我們搞定 it.

1:繼承 ScrollBar 并且 setUI();

2:繼承 BasicScrollBarUI 我們來G出我們的效果.

paintThumb 繪制scrollbar里拖動的小box 我們先畫個邊框 and draw two Orange line.

paintTrack 繪制scrollbar里小box的軌跡.也就是那個啥(術語怎么說來著?拖拽滑塊?).

注意:我們首先將Graphics設置透明后 在去畫面板 然后立刻把Graphics設置為不透明..

這樣是為了能讓我們把軌跡左邊邊界畫出來...

createIncreaseButton draw down arrowButton 小心 千萬不要use JButton button = new JButton();

should use BasicArrowButton 不然你將無法click this button 并產生你想要的效果..

你猜的到 createDecreaseButton(); 方法是干什么的..(笨蛋 上面那個Button啦);

 

  1. public class IScrollBarUI extends BasicScrollBarUI{  
  2.  public IScrollBarUI(){  
  3.   super();  
  4.  }  
  5.  
  6.  protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {  
  7.   int width = thumbBounds.width;  
  8.   int height = thumbBounds.height;  
  9.   Graphics2D g2 = (Graphics2D)g;  
  10.   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  11.           RenderingHints.VALUE_ANTIALIAS_ON);  
  12.     
  13.   g2.translate(thumbBounds.x, thumbBounds.y);  
  14.   g2.setColor(XUtil.defaultComboBoxBoundsColor);  
  15.   g2.drawRoundRect(1,1,width-2, height-2,5,5);  
  16.     
  17.   g2.setColor(Color.ORANGE);  
  18.   g2.drawLine(3,height/2,width-4,height/2);  
  19.   g2.drawLine(3,height/2+3,width-4,height/2+3);  
  20.   g2.translate(-thumbBounds.x, -thumbBounds.y);  
  21.  }  
  22.  
  23.  protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {  
  24.   g.setColor(XUtil.defaultComboBoxColor);  
  25.   int x = trackBounds.x;  
  26.   int y = trackBounds.y;  
  27.   int width = trackBounds.width;  
  28.   int height = trackBounds.height;  
  29.   Graphics2D g2 = (Graphics2D)g;  
  30.   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  31.           RenderingHints.VALUE_ANTIALIAS_ON);  
  32.   g2.setComposite(AlphaComposite  
  33.     .getInstance(AlphaComposite.SRC_OVER, 0.1f));  
  34.     
  35.   g2.fill3DRect(x, y, width, height, true);  
  36.   g2.setComposite(AlphaComposite  
  37.     .getInstance(AlphaComposite.SRC_OVER, 1f));  
  38.   g2.setColor(XUtil.defaultComboBoxBoundsColor.brighter());  
  39.   g2.fill3DRect(x, y, 1, height+1true);  
  40.   if(trackHighlight == DECREASE_HIGHLIGHT) {  
  41.       paintDecreaseHighlight(g);  
  42.   }   
  43.   else if(trackHighlight == INCREASE_HIGHLIGHT)  {  
  44.       paintIncreaseHighlight(g);  
  45.   }  
  46.  }  
  47.  
  48.  protected JButton createIncreaseButton(int orientation) {  
  49.   JButton button = new BasicArrowButton(orientation){  
  50.    public void paint(Graphics g) {  
  51.     Graphics2D g2 = (Graphics2D)g;  
  52.     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  53.             RenderingHints.VALUE_ANTIALIAS_ON);  
  54.     g2.setColor(XUtil.defaultComboBoxBoundsColor);  
  55.     g2.drawLine(0,0,0,getHeight());  
  56.     g2.drawLine(0,0,getWidth(),0-1);  
  57.     g2.drawImage(((ImageIcon)XUtil.defaultComboBoxArrowIcon_Into).getImage(),-1,0,null);  
  58.    }  
  59.   };  
  60.   button.setOpaque(false);  
  61.   return button;  
  62.  }  
  63.  
  64.  protected JButton createDecreaseButton(int orientation) {  
  65.  
  66.     
  67.   JButton button = new BasicArrowButton(orientation){  
  68.    public void paint(Graphics g) {  
  69.     Graphics2D g2 = (Graphics2D)g;  
  70.     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  71.             RenderingHints.VALUE_ANTIALIAS_ON);  
  72.     g2.setColor(XUtil.defaultComboBoxBoundsColor);  
  73.     g2.drawLine(0,0,0,getHeight());  
  74.     g2.drawLine(0,getHeight()-1,getWidth(),getHeight());  
  75.     g2.drawImage(((ImageIcon)XUtil.defaultComboBoxArrowIcon_Into).getImage(),-1,0,null);  
  76.    }  
  77.   };  
  78.   button.setOpaque(false);  
  79.   return button;  
  80.  }  

【編輯推薦】

  1. 探秘JDK 7之二:半透明和任意形狀的窗口
  2. 探秘JDK 7:將會出現新的語言特性
  3. 我們真的能沒有Java嗎?
  4. 探秘Java 7:JVM動態語言支持詳解
  5. 探秘Java 7新增垃圾回收器G1特性
責任編輯:金賀 來源: 博客園
相關推薦

2016-12-28 17:59:28

MongoDBNoSQL數據

2009-07-15 13:48:26

Swing模型和渲染器

2009-07-15 15:35:59

Swing程序Swing性能

2009-07-16 08:53:03

Swing任務Swing線程

2009-07-16 16:01:55

EventQueue

2023-11-20 17:38:07

Djangoagtailadmin

2009-06-22 13:44:00

JSFJava Web開發

2009-07-15 09:06:07

BeanTableMoSwing

2009-07-14 18:28:58

Swing入門

2009-07-10 13:36:32

Swing容器

2009-07-15 13:06:38

Swing組件

2009-07-15 14:29:24

構造JListSwing

2009-07-10 10:37:50

Swing Set示例

2009-07-15 11:19:17

invokeLaterSwing

2021-09-06 18:54:58

Java代碼表達式

2009-07-10 14:26:28

實現SwingActionListe

2009-07-10 18:06:59

JTree Swing

2009-07-17 12:54:13

2009-07-15 10:37:28

Swing外觀

2009-07-14 09:52:10

TableModelESwing
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产成人一区在线 | 日本网站免费在线观看 | 欧美中文一区 | 亚洲精品在线视频 | 成人免费视频观看 | 在线播放一区 | 草樱av| 国产一区二区在线免费视频 | 少妇性l交大片免费一 | 亚洲欧美在线观看 | 亚洲精品国产精品国自产在线 | 国产精品美女久久久久aⅴ国产馆 | 日韩一级电影免费观看 | 成人国产精品久久 | 中文字幕在线视频一区二区三区 | 亚洲人va欧美va人人爽 | 久久久久久久91 | 一区二区三区日韩精品 | 一区二区三区成人 | 欧美日韩在线精品 | 欧美一级久久 | 我要看黄色录像一级片 | 国产综合第一页 | 日韩精品在线观看免费 | 狠狠插天天干 | 一区二区三区中文字幕 | 国产成人在线视频 | 久久国产精品免费 | 精品视频 免费 | 婷婷99| 国产视频一区二区在线观看 | 日韩视频在线免费观看 | 亚洲最大看片网站 | 精品99久久久久久 | 日韩一区二区三区在线观看视频 | 成人免费看黄 | 久热爱| 欧美精品一区二区在线观看 | 91精品国产日韩91久久久久久 | 欧美午夜精品久久久久免费视 | 国产一区二区三区四区五区加勒比 |