This course will teach you about making Graphical User Interfaces or GUIs with Java
Running the following code produces the JFrame shown below.
![[quickgui.png|300]]
The JFrame contains a JLabel and a JButton OK. The OK button has an ActionListener attached. Clicking the button makes a JOptionPane appear.
Copy the code and run it. See if you can figure out what the different sections do. Don't worry if you don't understand everything, the following lessons will explain everything.
```java
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class MyFrame extends JFrame implements ActionListener {
JLabel lblExample;
JButton ok;
MyFrame() {
//Set up form behaviour
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add a Label and a Button
lblExample = new JLabel("Some Label");
ok = new JButton("OK");
ok.addActionListener(this);
//Add a simple layout
Container vert = Box.createVerticalBox();
vert.add(lblExample);
vert.add(ok);
this.add(vert);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) //check what actually fired the event
{
JOptionPane.showMessageDialog(this, "OK Pressed");
}
}
}
```