怎么设置按钮图标下的颜色?(类似word) word中的填充颜色,线条颜色等按钮,从下拉按钮中选择颜色后,他就在主按钮的下面显示刚才所选的颜色。这是怎么做得呢?不同的icon应该不会吧,因为可以自定义颜色。是不是可以按钮一部分显示固定图标,下部分显示一个线条,可以改变颜色。大家帮忙啊,谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该不行换图标吧或者楼主重写button类,重载setIcon函数,使其能实现添加下划线功能 用BufferedImage,动态创建自已的图标。 import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class UnderLineIcon implements Icon { private Icon mainIcon = null; private Color underLineColor = null; private int underLineHei; private int gap; public UnderLineIcon(Icon mainIcon, Color underLineColor, int underLineHei, int gap) { this.mainIcon = mainIcon; this.underLineColor = underLineColor; this.underLineHei = underLineHei; this.gap = gap; } public void paintIcon(Component c, Graphics g, int x, int y) { mainIcon.paintIcon(c, g, x, y); Color oldColor = g.getColor(); g.setColor(underLineColor); g.fillRect(x, y + mainIcon.getIconHeight() + gap, getIconWidth(), underLineHei); g.setColor(oldColor); } public int getIconWidth() { return mainIcon.getIconWidth(); } public int getIconHeight() { return mainIcon.getIconHeight() + gap + underLineHei; } public Color getUnderLineColor() { return underLineColor; } public void setUnderLineColor(Color underLineColor) { this.underLineColor = underLineColor; } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } Icon mainIcon = new ImageIcon(UnderLineIcon.class.getResource("/imgs/mycomputer.png")); final UnderLineIcon underLineIcon = new UnderLineIcon(mainIcon, Color.BLUE, 4, 1); final JButton colorChooserBtn = new JButton(underLineIcon); colorChooserBtn.setFocusPainted(false); colorChooserBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color newColor = JColorChooser.showDialog( colorChooserBtn, "Choose Color", underLineIcon.getUnderLineColor()); if (newColor != null) { underLineIcon.setUnderLineColor(newColor); colorChooserBtn.repaint(); } } }); JToolBar toolbar = new JToolBar(); toolbar.setRollover(true); toolbar.add(colorChooserBtn); JFrame f = new JFrame(); f.getContentPane().add(toolbar, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }} ya, got it.Thanks every body :) 如何在一个tree上加两个初始根节点? 三层架构和单例模式的命名 环境变量的设置。蔡鸟提问 求救!在两个Jframe类怎么传递数据?在线等…… sort函数如何实现降序排列? 怎样用java终止系统中运行的另外一个程序? 有没有这样的方法?(急!急!!急!!!) ★几个Java语法上面的问题★ 我用mDeJava.exe反编译出来的源文件,怎么不能使用啊? 请教:Applet引用了Swing,为何在IE中没法运行啊? 菜鸟求一算法或思路 请教一个问题:如何让ButtonGroup的边框线显示出来;
换图标吧
或者楼主重写button类,重载setIcon函数,使其能实现添加下划线功能
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class UnderLineIcon implements Icon {
private Icon mainIcon = null;
private Color underLineColor = null; private int underLineHei; private int gap;
public UnderLineIcon(Icon mainIcon, Color underLineColor, int underLineHei, int gap) {
this.mainIcon = mainIcon;
this.underLineColor = underLineColor;
this.underLineHei = underLineHei;
this.gap = gap;
} public void paintIcon(Component c, Graphics g, int x, int y) {
mainIcon.paintIcon(c, g, x, y);
Color oldColor = g.getColor();
g.setColor(underLineColor);
g.fillRect(x, y + mainIcon.getIconHeight() + gap, getIconWidth(), underLineHei);
g.setColor(oldColor);
} public int getIconWidth() {
return mainIcon.getIconWidth();
} public int getIconHeight() {
return mainIcon.getIconHeight() + gap + underLineHei;
} public Color getUnderLineColor() {
return underLineColor;
} public void setUnderLineColor(Color underLineColor) {
this.underLineColor = underLineColor;
} public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} Icon mainIcon = new ImageIcon(UnderLineIcon.class.getResource("/imgs/mycomputer.png"));
final UnderLineIcon underLineIcon = new UnderLineIcon(mainIcon, Color.BLUE, 4, 1);
final JButton colorChooserBtn = new JButton(underLineIcon);
colorChooserBtn.setFocusPainted(false);
colorChooserBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color newColor = JColorChooser.showDialog(
colorChooserBtn, "Choose Color", underLineIcon.getUnderLineColor());
if (newColor != null) {
underLineIcon.setUnderLineColor(newColor);
colorChooserBtn.repaint();
}
}
}); JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
toolbar.add(colorChooserBtn);
JFrame f = new JFrame();
f.getContentPane().add(toolbar, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}