Java如何自定义JButton图标?
下面的示例向您展示了如何自定义JButton摇摆组件的图标。
package org.nhooo.example.swing;
import javax.swing.*;
import java.awt.*;
public class JButtonCustomIcon extends JFrame {
public JButtonCustomIcon() throws HeadlessException {
initialize();
}
private void initialize() {
setSize(300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JButton button = new JButton("Press Me!");
//下面是我们如何为JButton摆动设置各种图标
//零件。有默认图标,已选中,已禁用,已显示
//和翻转图标。
button.setIcon(new ImageIcon("default.png"));
button.setSelectedIcon(new ImageIcon("selected.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabledSelected.png"));
button.setPressedIcon(new ImageIcon("pressed.png"));
button.setRolloverIcon(new ImageIcon("rollover.png"));
button.setRolloverSelectedIcon(new ImageIcon("rolloverSelected.png"));
getContentPane().add(button);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JButtonCustomIcon().setVisible(true);
}
});
}
}
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志