SL_*_*ser 3 java swing jbutton
我正在使用netbeans IDE开发Java项目,我需要禁用特定的JButton.我使用以下代码.
IssuBtn.setEnabled(false);
Run Code Online (Sandbox Code Playgroud)
但是在禁用它之后它不会在JButton上显示文本.如何在JButton上保留该文本?
And*_*son 12
这个实验表明一个答案是'使用非金属的PLAF'.

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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
或者,调整中的值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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
正如kleopatra指出..
它不是解决方案,但可能是指向搜索解决方案的指针
我的答案在哪里'它'.事实上,我怀疑她通过评论找到了真正的原因:
仅猜测:这是因为违反了仅限于一条平台的规则.
我猜第二个猜测.
| 归档时间: |
|
| 查看次数: |
6490 次 |
| 最近记录: |