菜鸟提问:如何设置Swing控件不可用(即setEnabled(false))时的字体颜色?需要一个通用方法,高手帮我呀!

xiaoyongjun 2006-05-23 03:00:38
因为Swing输入控件(主要是JCombobox、JTextField、JSpinner、JTextArea等)在不可用时,默认字体颜色都是灰色,即Gray,这种颜色看不清楚,所以我想改变输入控件不可用时的字体颜色,但又不能每个控件都去手动设置,因为控件非常多,所以想用一个通用的方法,写一个通用类,在这个类中定义方法来设置控件不可用时的字体颜色,Swing控件继承这个类,这只是初步想法,各位高手有没有相关的代码,贴出来看看?

非常着急,只要大家参与,都有分相赠!
...全文
给本帖投票
1269 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzhexin 2006-06-14
  • 打赏
  • 举报
回复
学习下
default_class 2006-06-12
  • 打赏
  • 举报
回复
jTextArea1.setDisabledTextColor(Color.red);
可以设置Disabled时字的颜色
mgj216 2006-05-30
  • 打赏
  • 举报
回复
若楼主想要去做一个通用的class,这样真的不错.
如这样来写可以试试,直接传入组件的名称就可以.

class Component
{
public Component(Component c1)

if (c1 instanceof JTextComponent){
}
else if(c1 instanceof ......){
}

}
dqpichd 2006-05-28
  • 打赏
  • 举报
回复
高手都不在呀!
dqpichd 2006-05-28
  • 打赏
  • 举报
回复
我也遇到同样的问题,希望能给与解决!关注ing!
xiaoyongjun 2006-05-27
  • 打赏
  • 举报
回复
default_class():好像不行,要不然你换background的颜色吧,不就明显了.

这种方法不好,setEnabled(false)时,foreground颜色为gray,这时你把background换成什么颜色,才清晰呢?默认为白色,估计没有更清晰的方法了吧。

老兄,怎么个贴皮肤方法?没用过!有没有代码试一下!
default_class 2006-05-27
  • 打赏
  • 举报
回复
enabled为true时,控件处于不活动状态,默认字体为灰色.要不然只有贴皮肤了.
default_class 2006-05-27
  • 打赏
  • 举报
回复
好像不行,要不然你换background的颜色吧,不就明显了.
xiaoyongjun 2006-05-27
  • 打赏
  • 举报
回复
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Frame3 extends JFrame {
JPanel contentPane;
FlowLayout flowLayout = new FlowLayout();
JTextField txtfldTest = new JTextField();
JLabel lblTest = new JLabel();
JComboBox combTest = new JComboBox();
JTextArea txtareaTest = new JTextArea();

public Frame3() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(flowLayout);

/*这是我测试JTextField的代码,因为使用TextField.inactiveForeground没有效果,我把 TextField的所有Key均测试了一遍,还是不能实现文本框在不可用时字体为红色,真是弄不清什么原因了,怀疑是环境问题,我直接到命令行下运行,结果还是一样,真是头疼!*/
UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0));
UIManager.put("TextField.caretForeground", new Color(255, 0, 0));
UIManager.put("TextField.inactiveBackground", new Color(255, 0, 0));
UIManager.put("TextField.shadow", new Color(255, 0, 0));
UIManager.put("TextField.darkShadow", new Color(255, 0, 0));
UIManager.put("TextField.selectionForeground", new Color(255, 0, 0));
UIManager.put("TextField.background", Color.white);
UIManager.put("TextField.highlight", Color.red);
UIManager.put("TextField.light", Color.red);
UIManager.put("TextField.selectionBackground", Color.red);
UIManager.put("TextField.foreground", Color.red);

/*这是我测试标签和下拉框不可用时设置字体颜色为红色的代码,这两个测试没有问题*/
UIManager.put("Label.disabledForeground", new Color(255, 0, 0));
UIManager.put("ComboBox.disabledForeground", new Color(255, 0, 0));

/*这是测试多行文本框不可用时设置字体颜色为红色的代码,它的问题用文本框,解决不了,很是头疼!*/
UIManager.put("TextArea.inactiveForeground", new Color(255, 0, 0));

lblTest.setText("TestLabel");
lblTest.setEnabled(false);
txtfldTest.setText("Testing");
txtfldTest.setEnabled(false);
combTest.removeAllItems();
combTest.addItem("Test1");
combTest.addItem("Test2");
combTest.setSelectedIndex(0);
combTest.setEnabled(false);
txtareaTest.setText("TestingJTextArea");
txtareaTest.setEnabled(false);
contentPane.add(lblTest);
contentPane.add(txtfldTest);
contentPane.add(combTest);
contentPane.add(txtareaTest);

/*我把经过上述设置后的结果均输出出来了,结果都正确,但就是颜色没有改变,太郁闷了!输出结果如下:
TextField.inactiveForeground:java.awt.Color[r=255,g=0,b=0]
TextField.selectionBackground:java.awt.Color[r=255,g=0,b=0]
TextField.inactiveBackground:java.awt.Color[r=255,g=0,b=0]
TextField.highlight:java.awt.Color[r=255,g=0,b=0]
TextField.shadow:java.awt.Color[r=255,g=0,b=0]
TextField.light:java.awt.Color[r=255,g=0,b=0]
TextField.foreground:java.awt.Color[r=255,g=0,b=0]
TextField.background:java.awt.Color[r=255,g=255,b=255]
TextField.darkShadow:java.awt.Color[r=255,g=0,b=0]
TextField.caretForeground:java.awt.Color[r=255,g=0,b=0]
TextField.highlight:java.awt.Color[r=255,g=0,b=0]

Label.disabledForeground:java.awt.Color[r=255,g=0,b=0]
ComboBox.disabledForeground:java.awt.Color[r=255,g=0,b=0]

TextArea.inactiveForeground:java.awt.Color[r=255,g=0,b=0]*/

UIDefaults u = UIManager.getDefaults();
Enumeration elements = u.keys();
for (; elements.hasMoreElements(); ) {
Object o = elements.nextElement();
System.out.println(o + ":" + u.get(o));
}
}


public static void main(String[] args) {
Frame3 frame3 = new Frame3();
frame3.setSize(300, 400);
frame3.setVisible(true);
}
}

各位高手,我实在是找不出原因了!高手帮我看看吧,能解决我的问题,必将重谢!
xiaoyongjun 2006-05-25
  • 打赏
  • 举报
回复
yonghar(xio):我没有用错,我怀疑我的环境是不是有问题,你用什么工具?能把你的代码发过来我测试一下吗?
xiaoyongjun 2006-05-24
  • 打赏
  • 举报
回复
yonghar(xio):能把你测试的全部代码发给我吗?我看看,看问题出在哪了?我的邮箱xiao_hero@163.com,非常感谢!
mq612 2006-05-24
  • 打赏
  • 举报
回复
先后顺序换一下试试
setMyUI();
init();
xiaoyongjun 2006-05-24
  • 打赏
  • 举报
回复
最后麻烦两位高手一下,希望能和你们取上联系,就着一个问题,以后决不打扰!
xiaoyongjun 2006-05-24
  • 打赏
  • 举报
回复
我这里怎么测试都不行呀,怎么回事?太奇怪了,二位高手能否告知联系方式,我的QQ:25403519

二位高手用用我的代码测试了吗?我是在JBuilder2006里面测试的!
mq612 2006-05-24
  • 打赏
  • 举报
回复
只要你的组件和Key对应就不会有问题
yonghar 2006-05-24
  • 打赏
  • 举报
回复
我这里测试没有问题。
你确定txtfldTest1是JTextField吗?
欧在怀疑是不是拉错,用了JLabel。
xiaoyongjun 2006-05-24
  • 打赏
  • 举报
回复
采用了两位仁兄的方法,还是没有达到我的要求,感觉非常沮丧!我的代码如下,二位能否再帮我一下,感激不尽!
public Frame1() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
init();
setMyUI();
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(xyLayout1);
this.setSize(400, 600);
btnTest1.setText("Test1");
txtfldTest1.setText("jTextField1");
btnEnabledFalse.setMargin(new Insets(0, 0, 0, 0));
btnEnabledFalse.setText("setEnabledFalse");
btnEnabledFalse.addActionListener(new Frame1_btnEnabledFalse_actionAdapter(this));
btnEnabledTrue.setMargin(new Insets(0, 0, 0, 0));
btnEnabledTrue.setText("setEnabledTrue");
btnEnabledTrue.addActionListener(new Frame1_btnEnabledTrue_actionAdapter(this));
this.addWindowListener(new Frame1_this_windowAdapter(this));
contentPane.add(btnTest1, new XYConstraints(98, 81, 88, 24));
contentPane.add(txtfldTest1, new XYConstraints(99, 118, 94, 22));
contentPane.add(combTest1, new XYConstraints(97, 156, 95, -1));
contentPane.add(btnEnabledFalse, new XYConstraints(228, 151, 123, 26));
contentPane.add(btnEnabledTrue, new XYConstraints(227, 116, 122, 26));
contentPane.add(spinTest1, new XYConstraints(96, 193, 97, 20));
}

private void setMyUI() {
UIDefaults u = UIManager.getDefaults();
Enumeration elements = u.keys();
for (; elements.hasMoreElements(); ) {
Object o = elements.nextElement();
System.out.println(o + ":" + u.get(o));
}

UIManager.put("TextField.inactiveForeground", Color.blue);
UIManager.put("Button.disabledText", Color.black);
UIManager.put("ComboBox.disabledForeground", Color.black);
UIManager.put("Spinner.foreground", Color.black);
}

private void init() {
combTest1.removeAllItems();
combTest1.addItem("xiaoyongjun");
combTest1.addItem("chengdan");
combTest1.setSelectedIndex(0);

btnTest1.setEnabled(false);
combTest1.setEnabled(false);
txtfldTest1.setEnabled(false);
spinTest1.setEnabled(false);
}
这种方法对JCombobox和JButton可行,但是对于JTextField没有效果!
mq612 2006-05-23
  • 打赏
  • 举报
回复
不用继承什么类,在程序一开始设置Swing的UI管理器UIManager就可以了,下面是将JTextField的非可用状态文本颜色设置为红色
//Key:[TextField.inactiveForeground]
UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0)); // 设置为红色,一句代码就搞定了
JTextField text = new JTextField(15);
text.setText("ABCDEF");
text.setEnabled(false);

更多Key可参考:
[DesktopIcon.background]
[windowText]
[activeCaptionBorder]
[InternalFrame.borderColor]
[ScrollBar.highlight]
[ComboBox.disabledBackground]
[Tree.selectionBackground]
[FormattedTextField.inactiveBackground]
[InternalFrame.inactiveTitleBackground]
[Table.selectionForeground]
[Label.foreground]
[ToggleButton.highlight]
[Button.background]
[FormattedTextField.caretForeground]
[RadioButtonMenuItem.selectionForeground]
[ToggleButton.shadow]
[OptionPane.errorDialog.titlePane.shadow]
[EditorPane.background]
[RadioButtonMenuItem.foreground]
[RadioButton.light]
[ToggleButton.focus]
[OptionPane.warningDialog.titlePane.background]
[TextArea.caretForeground]
[Button.highlight]
[CheckBox.focus]
[Separator.foreground]
[TabbedPane.light]
[Tree.selectionBorderColor]
[inactiveCaptionText]
[ToolTip.backgroundInactive]
[PasswordField.caretForeground]
[EditorPane.inactiveForeground]
[ToolTip.foreground]
[ToolBar.dockingBackground]
[ToggleButton.disabledText]
[ToggleButton.background]
[Slider.focus]
[ToolBar.floatingForeground]
[TabbedPane.background]
[ComboBox.foreground]
[ScrollBar.shadow]
[RadioButtonMenuItem.acceleratorForeground]
[InternalFrame.borderDarkShadow]
[PopupMenu.background]
[controlLtHighlight]
[CheckBoxMenuItem.background]
[TextPane.foreground]
[FormattedTextField.background]
[Viewport.background]
[TextField.light]
[MenuItem.background]
[TabbedPane.tabAreaBackground]
[Button.focus]
[inactiveCaptionBorder]
[CheckBoxMenuItem.selectionBackground]
[textInactiveText]
[OptionPane.errorDialog.titlePane.background]
[TextField.inactiveBackground]
[Tree.selectionForeground]
[Menu.selectionBackground]
[Tree.background]
[InternalFrame.inactiveTitleForeground]
[control]
[SplitPane.background]
[textText]
[RadioButton.foreground]
[menu]
[EditorPane.foreground]
[SplitPane.shadow]
[OptionPane.warningDialog.titlePane.foreground]
[TabbedPane.selectHighlight]
[RadioButton.shadow]
[Menu.disabledForeground]
[ToggleButton.light]
[windowBorder]
[OptionPane.questionDialog.border.background]
[ToggleButton.foreground]
[Panel.background]
[TabbedPane.foreground]
[RadioButtonMenuItem.background]
[Table.gridColor]
[ScrollBar.foreground]
[TextPane.caretForeground]
[ToolBar.shadow]
[ToggleButton.select]
[Spinner.foreground]
[FormattedTextField.foreground]
[Viewport.foreground]
[RadioButton.darkShadow]
[infoText]
[MenuItem.acceleratorSelectionForeground]
[MenuItem.foreground]
[MenuBar.background]
[OptionPane.errorDialog.titlePane.foreground]
[Button.light]
[menuText]
[ToggleButton.darkShadow]
[TextPane.background]
[MenuItem.acceleratorForeground]
[TabbedPane.darkShadow]
[CheckBox.background]
[TextField.highlight]
[TextArea.selectionBackground]
[Panel.foreground]
[OptionPane.questionDialog.titlePane.background]
[RadioButton.background]
[TextArea.inactiveForeground]
[Slider.shadow]
[textHighlightText]
[SplitPane.darkShadow]
[RadioButton.select]
[PasswordField.foreground]
[ScrollBar.thumb]
[activeCaptionText]
[MenuBar.foreground]
[OptionPane.warningDialog.titlePane.shadow]
[ScrollBar.background]
[Menu.foreground]
[CheckBox.disabledText]
[Spinner.background]
[TextPane.selectionForeground]
[ComboBox.selectionBackground]
[ScrollBar.track]
[CheckBox.foreground]
[TextField.caretForeground]
[ScrollBar.thumbDarkShadow]
[List.selectionBackground]
[OptionPane.messageForeground]
[TextPane.inactiveForeground]
[Button.shadow]
[Menu.acceleratorForeground]
[TextArea.selectionForeground]
[Menu.acceleratorSelectionForeground]
[TextField.foreground]
[textHighlight]
[OptionPane.questionDialog.titlePane.foreground]
[TabbedPane.selected]
[controlShadow]
[MenuItem.disabledForeground]
[MenuItem.selectionForeground]
[MenuItem.checkIcon]
[ColorChooser.swatchesDefaultRecentColor]
[ToolBar.highlight]
[OptionPane.foreground]
[controlText]
[TextArea.background]
[Tree.line]
[CheckBoxMenuItem.disabledForeground]
[Separator.highlight]
[TextField.darkShadow]
[Tree.textForeground]
[ComboBox.selectionForeground]
[ToolBar.foreground]
[PasswordField.selectionBackground]
[Label.disabledShadow]
[FormattedTextField.selectionBackground]
[PasswordField.background]
[ProgressBar.background]
[List.selectionForeground]
[Checkbox.select]
[MenuBar.highlight]
[ComboBox.buttonShadow]
[Menu.background]
[info]
[TextPane.selectionBackground]
[PasswordField.inactiveBackground]
[controlHighlight]
[ScrollBar.darkShadow]
[SplitPane.highlight]
[TableHeader.background]
[InternalFrame.activeTitleForeground]
[TextArea.foreground]
[TextField.background]
[InternalFrame.borderShadow]
[Button.select]
[MenuItem.selectionBackground]
[TextField.shadow]
[ScrollBar.thumbShadow]
[ScrollBar.thumbHighlight]
[scrollbar]
[window]
[PasswordField.selectionForeground]
[TextField.selectionForeground]
[FormattedTextField.selectionForeground]
[OptionPane.background]
[Separator.shadow]
[ComboBox.buttonBackground]
[Table.focusCellBackground]
[ColorChooser.background]
[ProgressBar.foreground]
[Table.foreground]
[Tree.hash]
[RadioButtonMenuItem.acceleratorSelectionForeground]
[TitledBorder.titleColor]
[inactiveCaption]
[Slider.highlight]
[Tree.textBackground]
[EditorPane.selectionBackground]
[ProgressBar.selectionForeground]
[Button.disabledText]
[ToolBar.background]
[Label.disabledForeground]
[PasswordField.inactiveForeground]
[TabbedPane.shadow]
[TabbedPane.highlight]
[List.background]
[InternalFrame.borderHighlight]
[TableHeader.foreground]
[Slider.background]
[RadioButtonMenuItem.disabledForeground]
[controlDkShadow]
[OptionPane.questionDialog.titlePane.shadow]
[DesktopIcon.foreground]
[InternalFrame.activeTitleBackground]
[CheckBoxMenuItem.acceleratorForeground]
[Desktop.background]
[Table.focusCellForeground]
[ColorChooser.foreground]
[RadioButton.highlight]
[Menu.checkIcon]
[OptionPane.errorDialog.border.background]
[ComboBox.buttonHighlight]
[ToolBar.light]
[EditorPane.selectionForeground]
[ComboBox.disabledForeground]
[ScrollPane.background]
[FormattedTextField.inactiveForeground]
[CheckBoxMenuItem.acceleratorSelectionForeground]
[activeCaption]
[TextField.selectionBackground]
[Button.foreground]
[Table.background]
[RadioButton.disabledText]
[MenuBar.shadow]
[List.foreground]
[text]
[desktop]
[ComboBox.buttonDarkShadow]
[ProgressBar.selectionBackground]
[Slider.foreground]
[ToolBar.dockingForeground]
[ToolTip.foregroundInactive]
[RadioButton.focus]
[Table.selectionBackground]
[ScrollBar.trackHighlight]
[Label.background]
[OptionPane.warningDialog.border.background]
[TabbedPane.focus]
[RadioButtonMenuItem.selectionBackground]
[ToolBar.darkShadow]
[Separator.background]
[PopupMenu.foreground]
[CheckBoxMenuItem.foreground]
[EditorPane.caretForeground]
[Button.darkShadow]
[ToolTip.background]
[CheckBoxMenuItem.selectionForeground]
[ToolBar.floatingBackground]
[ComboBox.background]
[TextField.inactiveForeground]
[ScrollPane.foreground]
[Menu.selectionForeground]
[InternalFrame.borderLight]
[Tree.foreground]
yonghar 2006-05-23
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/TopicView.asp?id=4769821

yonghar(xio) ( ) 信誉:105 2006-05-23 19:06:00 得分: 0


原来印象错了 ...
不好意思。
看来只能用uimanager设置了。

//打印所有的颜色属性
UIDefaults uiDefaults = UIManager.getLookAndFeelDefaults();
Enumeration keys = uiDefaults.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object val = uiDefaults.get(key);

if (val instanceof Color) {
System.out.println(key.toString());
}
}

//设置TextField,Label,Button不可用状态的表现颜色
uiDefaults.put("TextField.inactiveForeground", Color.BLUE);
uiDefaults.put("Label.disabledForeground", Color.BLUE);
uiDefaults.put("Button.disabledText", Color.BLUE);


Top
yonghar(xio) ( ) 信誉:105 2006-05-23 19:07:00 得分: 0


其他的属性字段名称自己去找.


Top
dqpichd 2006-05-23
  • 打赏
  • 举报
回复
关注ing,学习ing,高手们,发发言呀!我也很想知道!
加载更多回复(1)

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部