extends JButton : JButton « javax.swing « Java by API






extends JButton

  
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageTest {

  public static void main(String[] args) {
    JPanel panel = new JPanel();
    final ImageButton button = new ImageButton("button.png");
    button.setPressedIcon(new ImageIcon("down.png"));
    button.setRolloverIcon(new ImageIcon("over.png"));
    button.setSelectedIcon(new ImageIcon("sel.png"));
    button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
    button.setDisabledIcon(new ImageIcon("disabled.png"));
    button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
    button.setLocation(60, 74);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        button.setSelected(!button.isSelected());
        System.out.println("selecting");
      }
    });
    // button.setSelected(true);
    // button.setDisabled(false);
    panel.add(button);

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

class ImageButton extends JButton {

  public ImageButton(String img) {
    this(new ImageIcon(img));
  }

  public ImageButton(ImageIcon icon) {
    setIcon(icon);
    setMargin(new Insets(0, 0, 0, 0));
    setIconTextGap(0);
    setBorderPainted(false);
    setBorder(null);
    setText(null);
    setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
  }

}

           
         
    
  








Related examples in the same category

1.Button.background
2.Button.font
3.Button.foreground
4.new JButton(Action act)
5.new JButton(Icon ic)
6.new JButton(String text)
7.new JButton(String str) (HTML String)
8.new JButton(String text, Icon icon)
9.JButton: addActionListener(ActionListener act)
10.JButton: addAncestorListener(AncestorListener listener)
11.JButton: addChangeListener(ChangeListener l)
12.JButton: addItemListener(ItemListener l)
13.JButton: addPropertyChangeListener(PropertyChangeListener lis)
14.JButton: contains(int x, int y)
15.JButton: createToolTip()
16.JButton: getAccessibleContext()
17.JButton: getActionMap()
18.JButton: getBackground()
19.JButton: getInputMap()
20.JButton: removeActionListener(ActionListener l)
21.JButton: setActionCommand(String text)
22.JButton: setAlignmentX(float alignmentX)
23.JButton: setAlignmentY(float alignmentY)
24.JButton: setBackground(Color c)
25.JButton: setBorder(Border border)
26.JButton: setBorderPainted(boolean b)
27.JButton: setDefaultCapable(boolean defaultCapable)
28.JButton: setDisabledIcon(Icon disabledIcon)
29.JButton: setDisabledSelectedIcon(Icon disabledSelectedIcon)
30.JButton: setHorizontalAlignment(int alignment)
31.JButton: setIconTextGap(int iconTextGap)
32.JButton: setMnemonic(KeyEvent.VK_A)
33.JButton: setPressedIcon(Icon pressedIcon)
34.JButton: setRolloverIcon(Icon rolloverIcon)
35.JButton: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
36.JButton: setSelectedIcon(Icon selectedIcon)
37.JButton: setToolTipText(String str)
38.JButton: setVerticalAlignment(int alignment)