Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JOptionPane;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- public class Gui extends JFrame{
- // Variables
- private JButton reg;
- private JButton custom;
- // Constructor
- public Gui(){
- // Basics
- super("JFrame's title");
- setLayout(new FlowLayout());
- Icon green = new ImageIcon(getClass().getResource("green.png"));
- Icon red = new ImageIcon(getClass().getResource("red.png"));
- // Buttons
- reg = new JButton("Regular button");
- reg.setSize(300, 300);
- add(reg);
- custom = new JButton("Custom button", red);
- reg.setSize(300, 300);
- custom.setRolloverIcon(green);
- add(custom);
- // Calling the handler - action listener
- HandlerClass handler = new HandlerClass();
- reg.addActionListener(handler);
- custom.addActionListener(handler);
- }
- // New class inside classes - ActionListener is abstract!
- // We promise that we will override the methods
- private class HandlerClass implements ActionListener{
- public void actionPerformed(ActionEvent event){
- JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
- }
- }
- // Main Function
- public static void main(String[] args){
- Gui gui = new Gui();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- gui.setSize(600, 600);
- gui.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement