Creating a simple GUI application in java
Creating a simple GUI
We’ll start with a very basic GUI. I started by creating a new package, without module.java
, if you want a module.java
file please add the following line:
requires java.desktop;
I created a simple program to ask for two numbers then calculate them:
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
int x = Integer.parseInt(JOptionPane.showInputDialog("Enter your first number: "));
int y = Integer.parseInt(JOptionPane.showInputDialog("Enter your Second number: "));
int total = x + y;
JOptionPane.showMessageDialog(null, "The answer is " + total, "Sum of number one and number two", JOptionPane.PLAIN_MESSAGE);
}
}
We can mostly use JOptionPane
as soon as we import it without initializing it first. Taking input from a dialog box will always give us a string value, to convert these to integers I used Integer.parseInt
. We due the usual math operation of adding the two values and t hen display these back. The arguements for JOptionPane.showMessageDialog
are a little confusing.
The first argument determines the position of which the box will appear, to make things simple I placed it in the middle, in which we just pass the argument null
. We then pass the value for the message and the then the title. Finally is the message style, ours is just a plain message so we can use that.
JFrames
Another alternative we can use is JFrame
which allows us to easily manage a GUI and tap into default windows GUI functionality such as scroll bars, maximize, minimize and close. We create a seperate class to manage this:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class layoutManager extends JFrame {
private JLabel item1;
public layoutManager() {
super("Calculator");
setLayout(new FlowLayout());
item1 = new JLabel("Welcome to my simple calculator");
add(item1);
}
}
The top three imports are what we’re going to use to manage the GUI, our constructor is the most important part and will execute everything we need to create the actual window. Using the extends
instruction lets us work with JFrame
directly throughout the course of the constructor. We also use the FlowLayout
class to create a default window frame that we can use.
We then want to execute the constructor from main:
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
layoutManager simpleGUI = new layoutManager();
simpleGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simpleGUI.setSize(275,180);
simpleGUI.setVisible(true);
}
}
We start by initializing our layoutManager. We need to set a few things from JFrame before this executes. Our close operation should almost always be set the EXIT_ON_CLOSE
or the program won’t know what to do when we exit the program. We also want to set our visibility (otherwise we can’t actually see the windows) and set the size of the windows (if not dynamic).