JButton b = new JButton(Icon icon);ou:
JButton b = new JButton(String text, Icon icon);Example:
import java.awt.Dimension;Output
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JButtonTest extends 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.