Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mastrgamr.tutorial;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame; //import the component
- import javax.swing.JOptionPane;
- public class Window extends JFrame implements ActionListener {
- public Window(){
- //initialize the window with a title
- super("Hello World, Window()");
- //sets a layout manager
- this.setLayout(new FlowLayout());
- //create a new JButton
- JButton button = new JButton("Click Me!");
- this.add(button); //add the button to the window
- button.addActionListener(this);
- //create a new JButton
- JButton button2 = new JButton("Click Me Too!");
- this.add(button2); //add the button to the window
- button2.addActionListener(this);
- //set window size
- this.setSize(400, 300);
- //Optional: Determine what happens when you press the "X"
- //in the window frame
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //set window to be visible
- this.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String buttonText = e.getActionCommand();
- if(buttonText.equals("Click Me!")) {
- System.out.println(buttonText);
- } else if(buttonText.equals("Click Me Too!")) {
- System.out.println(buttonText);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement