当 JButton 禁用时,文本显示为灰色
我正在为一个学校项目制作扫雷游戏。单击某个字段/按钮时,它将被禁用,并根据其邻居的数量以不同的颜色显示其邻居。我正在 Eclipse 中处理这个问题。一切都很完美,我几乎准备好提交它了。唯一的问题是,颜色在 Eclipse 和 JCreator 中运行时有效,但是当我使用 .bat
/command (java Minesweeper
) 运行它时,数字显示为灰色出来而不是彩色。
当我调用 setText() 时,我会使用 html 标签更改颜色。例如: setText("3")
等
为什么会发生这种情况?颜色在 Eclipse/JCreator 中工作正常,但当我通过 cmd
或批处理脚本运行游戏时,颜色就不行了
试试这个:它对我不起作用...
在 Eclipse/JCreator 中编译并运行它。然后尝试使用 java Test
运行它。
在 Eclipse/JCreator 中运行时文本将为红色,在脚本中运行时文本将为灰色
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JButton testButton = new JButton("Click this");
MouseAdapter buttonListener = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
int modifier = e.getModifiers();
JButton clicked = (JButton)e.getSource();
clicked.setForeground(Color.RED);
clicked.setText("<html><font color=red>" + clicked.getText() + "</font></html>");
clicked.setEnabled(false);
}
};
mainFrame.setMinimumSize(new Dimension(640,480));
mainFrame.getContentPane().add(testButton);
testButton.addMouseListener(buttonListener);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
}
I'm making Minesweeper for a school project. When a field/button is clicked, it gets disabled, and it shows its neighbors with a different color depending on the number of neighbors it has. I'm working on this in Eclipse. It works all perfectly and I'm almost ready to submit it. The only problem is that the colors work when run in Eclipse and JCreator, but when I run it with a .bat
/command (java Minesweeper
), the numbers show up greyed out instead of colored.
I change the colors with html tags when I call setText()
. Ex: setText("<html><font color=red>3</font></html>")
etc
Why is this happening? Colors work fine in Eclipse/JCreator, but not when I run the game through cmd
or a batch script
Try this: It doesn't work for me...
Compile and run this in Eclipse/JCreator. Then try running it using java Test
The text will be red when run in Eclipse/JCreator, and grey when run in a script
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JButton testButton = new JButton("Click this");
MouseAdapter buttonListener = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
int modifier = e.getModifiers();
JButton clicked = (JButton)e.getSource();
clicked.setForeground(Color.RED);
clicked.setText("<html><font color=red>" + clicked.getText() + "</font></html>");
clicked.setEnabled(false);
}
};
mainFrame.setMinimumSize(new Dimension(640,480));
mainFrame.getContentPane().add(testButton);
testButton.addMouseListener(buttonListener);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例如,您必须测试
JButton#isEnabled()
,JButton#setDisabledIcon(Icon)
也非常好you have to test for
JButton#isEnabled()
, for exampleand very nice is
JButton#setDisabledIcon(Icon)
too结果我系统上的命令
java
指向 JRE 1.7.0_1 而不是 JRE 1.6.0_29(即使我从未将 JRE 7 的目录添加到PATH
变量中... )。由于某种原因,此代码在两个 JRE 上的行为有所不同。在 JRE 7 上,文本会显示为灰色。在 JRE 6 上,它的行为符合我想要的方式,并且文本不会变灰。Turns out the command
java
on my system points to JRE 1.7.0_1 instead of JRE 1.6.0_29 (even though I never added JRE 7's directory to thePATH
variable...). And for some reason, this code behaves differently on the two JREs. On JRE 7, the text gets greyed out. On JRE 6, it behaves the way I want it to, and the text doesn't get greyed out.