So our first example will be to build a simple GUI interface that launches from within the main. This isn’t a standard procedure, but is good to get a start into see how components work. Typically we’d have a separate class which would call and load the window.
We’re going to start with a basic Java Class with a static main function like below.
public class SampleSwingGUI1 {
public static void main(String args[]) {
}
}
Importing the Library
Next we need to import the library to work with Swing.
import javax.swing.*;
You will find there are numerous packages under javax.swing
which you may or may not need to import depending upon what it is you will be doing.
Creating the Window
Now we’re going to create the window. Inside the main method, you will need to create a JFrame, and get it a size, layout manager, and set it to be visible.
JFrame frame = new JFrame();
frame.setSize(250,500);
frame.setLayout(null);
frame.setVisible(true);
You can also pass a dimension object to the setSize, instead of the horizontal and vertical and horizontal sizes if you want. However, I find setting the size directly to make more sense.
If we run this, we should see a tall window, which is resizable, appear. It will have no components on it, but it will close if you click on the close button.
Adding a Button
We’re now going to add a button.
JButton button = new JButton("Press Me");
button.setBounds(50, 100, 100, 20);
frame.add(button);
Here, we create a JButton
, with a String in the constructor. This will become the text that is on the button.
We then setBounds
, which lets us know where to place it. We have to do this since there is no layout to the Frame (it was set to null earlier).
When we run this, we now see our Window appear, with a button on it.
Total So Far
public static void main(String args[]) {
// create a JFrame, with a default size
JFrame frame = new JFrame();
frame.setSize(250,500);
frame.setLayout(null);
// create a button to add to the frame
JButton button = new JButton("Press Me");
button.setBounds(50, 100, 100, 20);
frame.add(button);
frame.setVisible(true);
}
We can start to tweak this, by adding a title to the frame, etc.
frame.setTitle("Sample Swing Frame");
Adding an Event Handler
Any time we want to work with a button press, or something similar, we will have to work with Event Handlers. Like their name says, they will listen for an event to occur (key press, mouse click, generic “action”, etc) and then determine what needs to happen (handle it).
Let’s look at a simple event handler for the button. We’re going to use the method addActionListener()
.
In order to add this, we will need to import a new package.
import java.awt.event.*;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.setText("Button was pressed");
}
});
We pass in the ActionListener
we want to use. Notice two things however, one it is anonymous. We didn’t have to create an instance of it first. Second, ActionListener
is actually an abstract class. So we have to include the actionPreformed
method.
Inside the new event’s method, we define what it is we’re going to do. In this simple example, we’re just changing the text of the button.
A Quick Sample of Swing GUI was originally found on Access 2 Learn
4 Comments
Comments are closed.