Tutorial 8:
Well let me explain what a gui is first gui mean Graphical User Interface. Meaning Buttons, text and other things. Lets get started.
Note: Dont attempt this if you are new to java.
First our imports we will just need the javax.swing import this time.
This is the set of classes we will need that hold the methods for drawing buttons and what not.
Now lets declare our class name
You dont have to use this im just using it for a quick example.
now lets declare some objects.
public static JFrame frame;
public static JButton button;
You can call these whatever you want just make sure that you declare the frame and button correctly.
Now add a main method like so
public static void main(String[] args) {
Now we need to initialize our Frame and Button like so
frame = new JFrame("Gui");
button = new JButton("Click Me");
now we need to make the frame add the button like so.
Now if you closed the method and class now this would compile error free but when you ran it it would not show you the button because we have to tell the frame to show itself and were gonna tell it to pack itself to like so
frame.show();
frame.pack();
There you go thats it for now
Full-Code
import javax.swing.*;
public class GUI {
public static JFrame frame;
public static JButton button;
public static void main(String[] args) {frame = new JFrame("Gui");
button = new JButton("Click Me");
frame.add(button);
frame.show();
frame.pack();
}
}
if theres any errors with this coding please post here.