Java JButton(Icon icon) Constructor
Syntax
JButton(Icon icon) constructor from JButton has the following syntax.
public JButton(Icon icon)
Example
In the following code shows how to use JButton.JButton(Icon icon) constructor.
// w ww. j a v a 2 s.co m
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel implements ActionListener {
public Main() {
JButton jb = new JButton(new MyIcon());
jb.setActionCommand("France");
jb.addActionListener(this);
add(jb);
}
// Show text when user presses ENTER.
public void actionPerformed(ActionEvent ae) {
System.out.println(ae);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Main());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.drawString("java2s.com", 0, 20);
}
}
Home »
Java Tutorial »
javax.swing »
Java Tutorial »
javax.swing »