import java.awt.BorderLayout;//Just some imports that are needed
import java.awt.GridLayout;//Just some imports that are needed
import java.awt.event.ActionEvent;//Just some imports that are needed
import java.awt.event.ActionListener;//Just some imports that are needed
import javax.swing.BorderFactory;//Just some imports that are needed
import javax.swing.JButton;//Just some imports that are needed
import javax.swing.JFrame;//Just some imports that are needed
import javax.swing.JLabel;//Just some imports that are needed
import javax.swing.JPanel;//Just some imports that are needed
public 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"
int count = 0;//a VAR for the function of the button for the number of clicks
private JLabel label;
private JPanel panel;
private JFrame frame;
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
//JFrame - we have removed this from the line below to make a var that we can use in multiple methods {a}
frame = new JFrame();//This sets the frame for the goooooooooey so the user can customise it to their needs
JButton button = new JButton("Howdy");//This is used to make a button
button.addActionListener(this);//This refers to the class that we are using like GUI implements Action listener
//JLabel {a}
label = new JLabel("Number of clicks: 0"); //This is used to add a label to our program
//JPanel {a}
panel = new JPanel();//This sets the panel for the gooey for the user so that they can add elements, delete import etc etc
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
panel.setLayout(new GridLayout(0,1));// This is the default constructor for the borders/grids
panel.add(button);//This adds the button ( basically packing like btn.pack())
panel.add(label);//This adds the button (packs)
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Our GOOEY");//sets the title
frame.pack();//like py
frame.setVisible(true);//kinda literal this just lets the dev choose if the user can see this or not
}
public static void main(String[] args) {
new GUI();//Creates a constructor but this needs to be implemented to actually show us the gui window
}
@Override
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
count++;//Increases the count by 1
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
}
}
Posted by: Guest
on April-10-2021
Code answers related to "howto make simple gui in java"
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email