JComboBox自定义滚动条和去掉默认背景
来源:互联网 发布:mysql 指定数据库 编辑:程序博客网 时间:2024/03/29 04:51
JComboBox在选择之后整个方框都会变成灰色,这一点在开发自己的界面,完全不能忍。
下面贴一下我在另外一个博客看到的解决办法,隐藏得比较深,希望能给以后遇到类似的人一些指导。传送门 点击打开链接
解决的思路就是:拿到JcomboBox的editor,然后改变JTextField的背景颜色。
private void disableFocusBackground(javax.swing.JComboBox combo){if (combo == null){return;}java.awt.Component comp = combo.getEditor().getEditorComponent();if (comp instanceof javax.swing.JTextField){javax.swing.JTextField field = (javax.swing.JTextField) comp;field.setEditable(false);field.setSelectionColor(field.getBackground()/* java.awt.Color.WHITE */);combo.setEditable(true);}}
传入JComboBox即可。
下面简单介绍下怎么替换JComboBox丑陋的滑动条
新建一个类继承BasicScrollBarUI
package net.java.sip.communicator.impl.gui.message.yjm;import java.awt.Color;import java.awt.Dimension;import java.awt.GradientPaint;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.FileInputStream;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JScrollBar;import javax.swing.plaf.basic.BasicArrowButton;import javax.swing.plaf.basic.BasicScrollBarUI;public class CBScrollBarUI extends BasicScrollBarUI{public Dimension getPreferredSize(JComponent c){return new Dimension(16, 16);}// 重绘滚动条的滑块public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds){super.paintThumb(g, c, thumbBounds);int tw = thumbBounds.width;int th = thumbBounds.height;// 重定图形上下文的原点,这句一定要写,不然会出现拖动滑块时滑块不动的现象g.translate(thumbBounds.x, thumbBounds.y);Graphics2D g2 = (Graphics2D) g;GradientPaint gp = null;gp = new GradientPaint(0, 0, new Color(124, 124, 124), tw, 0,new Color(124, 124, 124));g2.setPaint(gp);g2.fillRoundRect(0, 0, tw - 1, th - 1, 5, 5);g2.setColor(new Color(124, 124, 124));g2.drawRoundRect(0, 0, tw - 1, th - 1, 5, 5);}// 重绘滑块的滑动区域背景public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds){Graphics2D g2 = (Graphics2D) g;GradientPaint gp = null;gp = new GradientPaint(0, 0, new Color(196, 195, 194),trackBounds.width, 0, new Color(196, 195, 194));g2.setPaint(gp);g2.fillRect(trackBounds.x, trackBounds.y, trackBounds.width,trackBounds.height);g2.setColor(new Color(196, 195, 194));g2.drawRect(trackBounds.x, trackBounds.y, trackBounds.width - 1,trackBounds.height - 1);if (trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT)this.paintDecreaseHighlight(g);if (trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT)this.paintIncreaseHighlight(g);}// 重绘当鼠标点击滑动到向上或向左按钮之间的区域protected void paintDecreaseHighlight(Graphics g){g.setColor(Color.green);int x = this.getTrackBounds().x;int y = this.getTrackBounds().y;int w = 0, h = 0;if (this.scrollbar.getOrientation() == JScrollBar.VERTICAL){w = this.getThumbBounds().width;h = this.getThumbBounds().y - y;}if (this.scrollbar.getOrientation() == JScrollBar.HORIZONTAL){w = this.getThumbBounds().x - x;h = this.getThumbBounds().height;}g.fillRect(x, y, w, h);}// 重绘当鼠标点击滑块至向下或向右按钮之间的区域protected void paintIncreaseHighlight(Graphics g){g.setColor(Color.blue);int x = this.getThumbBounds().x;int y = this.getThumbBounds().y;int w = this.getTrackBounds().width;int h = this.getTrackBounds().height;g.fillRect(x, y, w, h);}protected JButton createIncreaseButton(int orientation){return new BasicArrowButton(orientation){// 重绘按钮的三角标记public void paintTriangle(Graphics g, int x, int y, int size,int direction, boolean isEnabled){FileInputStream is;BufferedImage img = null;try{is = new FileInputStream("resources/images/message/群发/上三角常态.png");img=ImageIO.read(is);} catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();} Graphics2D g2 = (Graphics2D) g;GradientPaint gp = null;switch (this.getDirection()){case BasicArrowButton.SOUTH:gp = new GradientPaint(0, 0,Color.white,getWidth(), 0,Color.white);break;case BasicArrowButton.EAST:gp = new GradientPaint(0, 0, Color.white, 0,getHeight(), Color.white);break;}g2.setPaint(gp);g2.fillRect(0, 0, getWidth(), getHeight());g2.setColor(Color.white);g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);g2.drawImage(img, (getWidth() - 2) / 2 - 5,(getHeight() - 2) / 2 - 5, 13, 13, null);}};}protected JButton createDecreaseButton(int orientation){return new BasicArrowButton(orientation){public void paintTriangle(Graphics g, int x, int y, int size,int direction, boolean isEnabled){FileInputStream is;BufferedImage img = null;try{is = new FileInputStream("resources/images/message/群发/下三角常态.png");img=ImageIO.read(is);} catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();} Graphics2D g2 = (Graphics2D) g;GradientPaint gp = null;switch (this.getDirection()){case BasicArrowButton.NORTH:gp = new GradientPaint(0, 0,Color.white,getWidth(), 0, Color.white);break;case BasicArrowButton.WEST:gp = new GradientPaint(0, 0,Color.white, 0,getHeight(),Color.white);break;}g2.setPaint(gp);g2.fillRect(0, 0, getWidth(), getHeight());g2.setColor(Color.white);g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);g2.drawImage(img, (getWidth() - 2) / 2 - 5,(getHeight() - 2) / 2 - 5, 13, 13, null);}};}}
到这里只能算完成了最重要的三分之二的任务。在网上找了资料,的确看到有人这么做,继承BasicScrollBarUI。但是困惑我好久的是怎么把这个类给传到JCombBox中去。下面简单介绍下应用这个“继承者”的方法。
新建一个类
MyComboBoxUI extends BasicComboBoxUI
public class MyComboBoxUI extends BasicComboBoxUI{public static ComponentUI createUI(){return new MyComboBoxUI();}@Override
<span style="white-space:pre"></span>//替换默认的箭头protected JButton createArrowButton(){// JButton button = new BasicArrowButton(BasicArrowButton.EAST);CommonButton button = new CommonButton("resources/images/message/群发/下拉箭头常态.png","resources/images/message/群发/下拉箭头掠过.png","resources/images/message/群发/下拉箭头掠过.png");return button;}<span style="white-space:pre"></span>//自定义点击右边小箭头之后的下拉框,这个方法被我忽略了很久。。。@Overrideprotected ComboPopup createPopup(){ComboPopup popup = new BasicComboPopup(comboBox){protected JScrollPane createScroller(){
<span style="white-space:pre"></span>//新建一个<span style="font-family: Arial, Helvetica, sans-serif;">JScrollPane,禁止横向滑动</span>JScrollPane sp = new JScrollPane(list,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);//sp.getVerticalScrollBar().setUI(new MyScrollBarUI());
<span style="white-space:pre"></span>//传入上面的继承BasicScrollBarUI的类sp.getVerticalScrollBar().setUI(new CBScrollBarUI());return sp;}};return popup;}}
最后一步就是传入MyComboBoxUI
buildingBox.setUI((ComboBoxUI) MyComboBoxUI.createUI());
好久都没有写博客了,因为在网上找这方面有点难找,贴出来希望能给大家有点帮助。
0 0
- JComboBox自定义滚动条和去掉默认背景
- 关于listview自定义滚动条和背景
- webview 去掉滚动条和上下黑灰色背景
- ios UIWebView隐藏右侧和底部的滚动条,去掉滚动边界的黑色背景
- UIWebView隐藏右侧和底部滚动条,去掉滚动边界的黑色背景,禁止左右滑动
- textView 自定义滚动条和背景,listivew也可以用
- IOS UIWebView去掉滚动条和黑色背景即拖拽后的上下阴影
- CCScrollView添加滚动条和滚动背景
- 去掉 iframe 边框以及滚动条以及背景透明
- 去掉 iframe 边框以及滚动条以及背景透明
- 去除ListView默认点击背景,去除LietView滚动条
- intellij idea 自定义选中字符的背景颜色和右侧滚动条上显示的颜色
- 如何去掉滚动条?
- 去掉浏览器滚动条
- 去掉dbgrid滚动条
- 去掉滚动条
- iframe去掉滚动条
- ScrollView去掉滚动条
- 《Linux》天影linux系列笔记一——linux常用命令详解:less
- Xcode的View关联问题
- 音视频处理框架
- c语言之————有头循环双链表实现栈存储
- zz 上海交大ACM队长建议——谈谈ACM比赛中的代码能力
- JComboBox自定义滚动条和去掉默认背景
- C++使用libcurl做HttpClient
- HTTP协议详解
- {}如何在B2B网站上发布信息
- FMDatabase使用sum函数,ResultSet的bug
- Dell vostro安装win7摄像头常亮解决办法
- java-判断一个自然数是否是某个数的平方。当然不能使用开方运算
- 《Linux》天影linux系列笔记一——linux常用命令详解:head
- spark学习六 DStream的运行原理解析