本文介绍了当JButton被禁用时,文本变灰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为一个学校项目进行扫雷。当单击一个字段/按钮时,它将被禁用,并根据它所具有的邻居数量用不同的颜色显示其邻居。我正在Eclipse中处理这个问题。它完美的工作,我几乎准备好提交它。唯一的问题是,在Eclipse和JCreator中运行颜色时,但是当我使用 .bat
/命令( java Minesweeper
$ b 当我调用 setText()时,我用html标签更改颜色)
。例如: setText(< html>< font color = red> 3< / font>< / html>)
etc
为什么会发生这种情况?在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);
$ div $解析方案
Turn在我的系统上输出命令 java
指向JRE 1.7.0_1而不是JRE 1.6.0_29(即使我从未将JRE 7的目录添加到 PATH
变量...)。由于某些原因,这些代码在两个JRE上表现不同。在JRE 7上,文字变灰。在JRE 6上,它的行为符合我的要求,并且文本不会变灰。
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);
}
}
解决方案
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 the PATH
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.
这篇关于当JButton被禁用时,文本变灰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!