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

Swing不丑系列:JComboBox

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

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

[[20608]]

 

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

[[20609]]

 

 

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

 

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

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

所以 著手寫ComboBoxRenderer. 總是覺的 JComboBox 似乎不太好搞定 因?yàn)樗坏酗@示框 還有小鍵頭.

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

那么我們要做幾件事.

1: 重載JComboBox 并且設(shè)置面板透明

2: 新建renderer 實(shí)現(xiàn)ListCellRenderer接口

3: 重載BasicComboBoxUI

1.重載JComboBox 并且設(shè)置面板透明 設(shè)置renderer 以及設(shè)置 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 實(shí)現(xiàn)ListCellRenderer接口.注意.這里的ComboBoxRenderer它是控制combobox彈出的List 并非控制JComboBox的 注意 他實(shí)現(xiàn)的是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 這里當(dāng)然要注意.因?yàn)樗荍ComboBox的繪制機(jī)制

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

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

這里存在一個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.    //重點(diǎn):JComboBox的textfield 的繪制 并不是靠Renderer來控制 這點(diǎn)讓我很吃驚.  
  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這塊已經(jīng)end 但是似乎還有個問題存在 那就是createPopup 方法里的ScrollPane的滾動條還是有點(diǎn)丑.

so。.next 我們搞定 it.

1:繼承 ScrollBar 并且 setUI();

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

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

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

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

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

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

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

你猜的到 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:將會出現(xiàn)新的語言特性
  3. 我們真的能沒有Java嗎?
  4. 探秘Java 7:JVM動態(tài)語言支持詳解
  5. 探秘Java 7新增垃圾回收器G1特性
責(zé)任編輯:金賀 來源: 博客園
相關(guān)推薦

2016-12-28 17:59:28

MongoDBNoSQL數(shù)據(jù)

2009-07-15 13:48:26

Swing模型和渲染器

2009-07-15 15:35:59

Swing程序Swing性能

2009-07-16 08:53:03

Swing任務(wù)Swing線程

2009-07-16 16:01:55

EventQueue

2023-11-20 17:38:07

Djangoagtailadmin

2009-06-22 13:44:00

JSFJava Web開發(fā)

2009-07-14 18:28:58

Swing入門

2009-07-15 13:06:38

Swing組件

2009-07-15 14:29:24

構(gòu)造JListSwing

2009-07-10 13:36:32

Swing容器

2009-07-10 10:37:50

Swing Set示例

2009-07-15 11:19:17

invokeLaterSwing

2009-07-15 09:06:07

BeanTableMoSwing

2021-09-06 18:54:58

Java代碼表達(dá)式

2009-07-10 14:26:28

實(shí)現(xiàn)SwingActionListe

2009-07-10 18:06:59

JTree Swing

2009-07-17 12:54:13

2009-07-15 10:37:28

Swing外觀

2009-07-17 12:44:01

NetBeans開發(fā)S
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 久久久性| 日日碰狠狠躁久久躁96avv | 久久久久久久国产 | 日日日操| 91成人免费 | 婷婷99 | 欧美亚洲国产日韩 | 在线观看国产网站 | 久草视频观看 | 亚洲欧美精品国产一级在线 | 在线观看亚洲精品视频 | 99视频精品| 一区二区精品视频 | 性高朝久久久久久久3小时 av一区二区三区四区 | 亚洲成人免费视频在线观看 | 在线观看中文字幕视频 | 久草视频在线播放 | 99久久精品免费看国产小宝寻花 | 午夜视频免费在线观看 | 天天综合网天天综合色 | 一区视频| 亚洲视频欧美视频 | 毛片免费在线 | 国产精品成人一区 | 国产日韩欧美激情 | 一级免费毛片 | 国产精品污www一区二区三区 | 99视频入口 | 国产三区四区 | 日韩成人在线视频 | 国产一区二区三区免费视频 | 国产成人精品一区二 | 国产精品欧美一区二区三区不卡 | 亚洲精品一区二区三区在线 | 成人精品国产 | 国产a视频| 特级毛片爽www免费版 | 成人精品毛片 | 国产精品一区二区不卡 | 欧美区在线 | 国产日产久久高清欧美一区 |