Advertisement
mastrgamr

mastrgamr JFrame Tutorial

Mar 18th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. package net.mastrgamr.tutorial;
  2.  
  3. import java.awt.FlowLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame; //import the component
  9. import javax.swing.JOptionPane;
  10.  
  11. public class Window extends JFrame implements ActionListener {
  12.  
  13.     public Window(){
  14.         //initialize the window with a title
  15.         super("Hello World, Window()");
  16.        
  17.         //sets a layout manager
  18.         this.setLayout(new FlowLayout());
  19.        
  20.         //create a new JButton
  21.         JButton button = new JButton("Click Me!");
  22.         this.add(button); //add the button to the window
  23.         button.addActionListener(this);
  24.        
  25.         //create a new JButton
  26.         JButton button2 = new JButton("Click Me Too!");
  27.         this.add(button2); //add the button to the window
  28.         button2.addActionListener(this);
  29.        
  30.         //set window size
  31.         this.setSize(400, 300);
  32.         //Optional: Determine what happens when you press the "X"
  33.         //in the window frame
  34.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.        
  36.         //set window to be visible
  37.         this.setVisible(true);
  38.     }
  39.  
  40.     @Override
  41.     public void actionPerformed(ActionEvent e) {
  42.         String buttonText = e.getActionCommand();
  43.        
  44.         if(buttonText.equals("Click Me!")) {
  45.             System.out.println(buttonText);
  46.         } else if(buttonText.equals("Click Me Too!")) {
  47.             System.out.println(buttonText);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement