thenewboston

Java Programming Tutorial - 63 - JButton Final Program

Aug 22nd, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import javax.swing.JFrame;
  2.  
  3. public class apples {
  4.    public static void main(String[] args){
  5.       Gui go = new Gui();
  6.       go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7.       go.setSize(300,200);
  8.       go.setVisible(true);
  9.      
  10.    }
  11.  
  12. }
  13.  
  14. import java.awt.FlowLayout;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17. import javax.swing.JFrame;
  18. import javax.swing.JButton;
  19. import javax.swing.Icon;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JOptionPane;
  22.  
  23. public class Gui extends JFrame{
  24.    
  25.    private JButton reg;
  26.    private JButton custom;
  27.    
  28.    public Gui(){
  29.       super("The title");
  30.       setLayout(new FlowLayout());
  31.      
  32.       reg = new JButton("reg Button");
  33.       add(reg);
  34.      
  35.       Icon b = new ImageIcon(getClass().getResource("a.png"));
  36.       Icon x = new ImageIcon(getClass().getResource("x.png"));
  37.       custom = new JButton("Custom", b);
  38.       custom.setRolloverIcon(x);
  39.       add(custom);
  40.      
  41.       HandlerClass handler = new HandlerClass();
  42.       reg.addActionListener(handler);
  43.       custom.addActionListener(handler);
  44.    }
  45.    
  46.    private class HandlerClass implements ActionListener{
  47.       public void actionPerformed(ActionEvent event){
  48.          JOptionPane.showMessageDialog(null, String.format("%s",
  49.  
  50. event.getActionCommand()));
  51.       }
  52.    }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment