如何禁用JButton而不隐藏其标签? [英] How to disable JButton without hiding its label?
问题描述
我正在使用netbeans IDE开发Java项目,我需要禁用特定的JButton。我使用以下代码。
I'm developing a project in Java using netbeans IDE and I need to disable a particular JButton. I use the following code for that.
IssuBtn.setEnabled(false);
但是在禁用它之后它不显示JButton上的文本。如何在JButton上保留该文本?
But after it is disabled it doesn't show the text on the JButton. How can I keep that text on the JButton?
推荐答案
这个实验表明一个答案是'使用非金属的PLAF'。
This experiment suggests one answer is 'Use a PLAF that is not Metal'.
import java.awt.*;
import javax.swing.*;
class LookOfDisabledButton {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
JPanel pEnabled = new JPanel(new GridLayout(1,0,2,2));
pEnabled.setBackground(Color.green);
gui.add(pEnabled, BorderLayout.NORTH);
JPanel pDisabled = new JPanel(new GridLayout(1,0,2,2));
pDisabled.setBackground(Color.red);
gui.add(pDisabled, BorderLayout.SOUTH);
UIManager.LookAndFeelInfo[] plafs =
UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo plafInfo : plafs) {
try {
UIManager.setLookAndFeel(plafInfo.getClassName());
JButton bEnabled = new JButton(plafInfo.getName());
pEnabled.add(bEnabled);
JButton bDisabled = new JButton(plafInfo.getName());
bDisabled.setEnabled(false);
pDisabled.add(bDisabled);
} catch(Exception e) {
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
或者,调整 UIManager
中的值。
import java.awt.*;
import javax.swing.*;
class LookOfDisabledButton {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
JPanel pEnabled = new JPanel(new GridLayout(1,0,2,2));
pEnabled.setBackground(Color.green);
gui.add(pEnabled, BorderLayout.NORTH);
JPanel pDisabled = new JPanel(new GridLayout(1,0,2,2));
pDisabled.setBackground(Color.red);
gui.add(pDisabled, BorderLayout.SOUTH);
// tweak the Color of the Metal disabled button
UIManager.put("Button.disabledText", new Color(40,40,255));
UIManager.LookAndFeelInfo[] plafs =
UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo plafInfo : plafs) {
try {
UIManager.setLookAndFeel(plafInfo.getClassName());
JButton bEnabled = new JButton(plafInfo.getName());
pEnabled.add(bEnabled);
JButton bDisabled = new JButton(plafInfo.getName());
bDisabled.setEnabled(false);
pDisabled.add(bDisabled);
} catch(Exception e) {
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
正如kleopatra指出..
As pointed out by kleopatra..
它不是解决方案,但可能是指向搜索解决方案的指针
it's not a solution but might be a pointer to the direction to search for a solution
我的答案在哪里'它'。事实上,我怀疑她通过评论找到了真正的原因:
Where 'it' is my answer. In fact, I suspect she hit upon the real cause with the comment:
仅猜测:这是因为违反了一个 - 只有规则。
guessing only: here it's due to violating the one-plaf-only rule.
我猜的第二个。
这篇关于如何禁用JButton而不隐藏其标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!