1import java.awt.BorderLayout;
2import java.awt.event.ActionEvent;
3import java.awt.event.ActionListener;
4
5import javax.swing.JButton;
6import javax.swing.JFrame;
7import javax.swing.JTextArea;
8
9public class Foo{
10
11 public static void main(String[] args) {
12
13 JFrame f = new JFrame("A JFrame");
14 f.setSize(250, 250);
15 f.setLocation(300,200);
16 final JTextArea textArea = new JTextArea(10, 40);
17 f.getContentPane().add(BorderLayout.CENTER, textArea);
18 final JButton button = new JButton("Click Me");
19 f.getContentPane().add(BorderLayout.SOUTH, button);
20 button.addActionListener(new ActionListener() {
21
22 @Override
23 public void actionPerformed(ActionEvent e) {
24 textArea.append("Button was clicked\n");
25
26 }
27 });
28
29 f.setVisible(true);
30
31 }
32
33}
1import java.awt.BorderLayout;//Just some imports that are needed
2import java.awt.GridLayout;//Just some imports that are needed
3import java.awt.event.ActionEvent;//Just some imports that are needed
4import java.awt.event.ActionListener;//Just some imports that are needed
5
6import javax.swing.BorderFactory;//Just some imports that are needed
7import javax.swing.JButton;//Just some imports that are needed
8import javax.swing.JFrame;//Just some imports that are needed
9import javax.swing.JLabel;//Just some imports that are needed
10import javax.swing.JPanel;//Just some imports that are needed
11
12public class GUI implements ActionListener{//If there is a red line after writing GUI its cause you have no code for the bit that's after implements so just hover over gui and just press "add unimplemented methods"
13
14 int count = 0;//a VAR for the function of the button for the number of clicks
15 private JLabel label;
16 private JPanel panel;
17 private JFrame frame;
18
19
20 public GUI(){//This implements for the constructor that is being create in the void main, Here you can make frames for the gooooooey and define its characteristics :P
21
22 //JFrame - we have removed this from the line below to make a var that we can use in multiple methods {a}
23 frame = new JFrame();//This sets the frame for the goooooooooey so the user can customise it to their needs
24
25 JButton button = new JButton("Howdy");//This is used to make a button
26 button.addActionListener(this);//This refers to the class that we are using like GUI implements Action listener
27
28 //JLabel {a}
29 label = new JLabel("Number of clicks: 0"); //This is used to add a label to our program
30
31 //JPanel {a}
32 panel = new JPanel();//This sets the panel for the gooey for the user so that they can add elements, delete import etc etc
33 panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));//This makes a border object for the goooey and the number in the field describes the distance it should be from the borders of the gui
34 panel.setLayout(new GridLayout(0,1));// This is the default constructor for the borders/grids
35 panel.add(button);//This adds the button ( basically packing like btn.pack())
36 panel.add(label);//This adds the button (packs)
37
38 frame.add(panel, BorderLayout.CENTER);
39 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40 frame.setTitle("Our GOOEY");//sets the title
41 frame.pack();//like py
42 frame.setVisible(true);//kinda literal this just lets the dev choose if the user can see this or not
43 }
44
45 public static void main(String[] args) {
46 new GUI();//Creates a constructor but this needs to be implemented to actually show us the gui window
47
48 }
49
50 @Override
51 public void actionPerformed(ActionEvent e) {//After implementing the unimplemented methods this autogenerated method is initiated to help us write our code in for the action we need to perform
52 count++;//Increases the count by 1
53 label.setText("Number of clicks: " + count);//This just changes the text of the label like lable.config("Hey") and then adds the number of clicks to the string
54
55 }
56
57}
58