JButton b = new JButton(Icon icon);ou:
JButton b = new JButton(字符串文本,图标图标);示例:
import java.awt.Dimension;Output
import javax.swing.ImageIcon;
import javax.swing.JButton;
导入 javax.swing.JFrame;
公共类 JButtonTest 扩展 JFrame{
JButton b = new JButton(Open”, new ImageIcon(icon.png”));
public JButtonTest(){
JFrame frame = new JFrame(JFrame test”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
b.setPreferredSize(new Dimension(150,100));
frame.getContentPane().add(b);
frame.pack();
}
public static void main(String[] args) {
new JButtonTest();
}
}
ImageIcon image = new ImageIcon(icon.png”);
b.setIcon(图像);
try {References:
Image img = ImageIO.read(getClass().getResource(/icon.png”));
b.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}
Please disable your ad blocker and refresh the window to use this website.