import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class ActionExample extends JFrame {
private ExampleAction exampleAction = new ExampleAction();
public ActionExample() {
super("Example Action");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setLocationByPlatform(true);
this.setSize(320, 240);
List<Image> images = new ArrayList<Image>();
images.add(new SmallIcon().getImage());
images.add(new BigIcon().getImage());
this.setIconImages(images);
JMenuItem menuItem = new JMenuItem(exampleAction);
JMenu menu = new JMenu("Menu");
menu.add(menuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JButton tButton = new JButton(exampleAction);
JToolBar toolBar = new JToolBar();
toolBar.add(tButton);
JButton button = new JButton(exampleAction);
this.setJMenuBar(menuBar);
this.add(toolBar, "North");
this.add(button, "Center");
this.setVisible(true);
}
public static void main(String[] args) {
new ActionExample();
}
class ExampleAction extends AbstractAction {
public ExampleAction() {
putValue(Action.NAME, "Hallo");
putValue(Action.SMALL_ICON, new SmallIcon());
putValue(Action.LARGE_ICON_KEY, new BigIcon());
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ActionExample.this, "Hallo Welt");
}
}
class SmallIcon implements Icon {
private BufferedImage bImage = new BufferedImage(16, 16,
BufferedImage.TYPE_INT_ARGB);
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.red);
g.fillOval(x, y, getIconWidth(), getIconHeight());
g.drawImage(bImage, x, y, null);
}
public int getIconWidth() {
return 16;
}
public int getIconHeight() {
return 16;
}
public Image getImage() {
Graphics bg = bImage.getGraphics();
bg.setColor(Color.red);
bg.fillOval(0, 0, getIconWidth(), getIconHeight());
return bImage;
}
}
class BigIcon implements Icon {
private BufferedImage bImage = new BufferedImage(32, 32,
BufferedImage.TYPE_INT_ARGB);
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.red);
g.fillOval(x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public Image getImage() {
Graphics bg = bImage.getGraphics();
bg.setColor(Color.red);
bg.fillOval(0, 0, getIconWidth(), getIconHeight());
return bImage;
}
}
}