java jbutton 禁用_java – 如何禁用JButton在禁用时变灰?
您只需將按鈕的禁用圖標設置為與按鈕圖標相同的值即可.看這個例子:
在左側是一個按鈕,我在其中設置了icon和disabledIcon.在右邊我只設置了圖標:
import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestDisabledButtons {
public static final String CARD_URL = "http://assets0.wordansassets.com/wvc-1345850020/wordansfiles/images/2012/8/24/156256/156256_340.jpg";
protected void createAndShowGUI() throws MalformedURLException {
JFrame frame = new JFrame("Test button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon imageIcon = new ImageIcon(new URL(CARD_URL));
JButton button = new JButton(imageIcon);
JButton button2 = new JButton(imageIcon);
button.setDisabledIcon(imageIcon);
button.setEnabled(false);
button2.setEnabled(false);
frame.add(button, BorderLayout.WEST);
frame.add(button2, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new TestDisabledButtons().createAndShowGUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
總結
以上是生活随笔為你收集整理的java jbutton 禁用_java – 如何禁用JButton在禁用时变灰?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php mysql.x86 64_cen
- 下一篇: java论文翻译_Java技术(外文文献