-
Notifications
You must be signed in to change notification settings - Fork 56
IntelliJ Introduction to Swing
Swing is a GUI widget toolkit for Java. Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT).
Though Swing is intended to be replaced by JavaFX, it will remain part of the Java SE specification for the foreseeable future.
In order to create a Swing application you will have to complete the following steps:
-
Create a new project.
-
Right click on the wanted package and choose
New | GUI Form
-
In the created
.form
file you can now add elements, such as buttons. -
Add names to the elements you would like to be able to change.
- Notice that every named element appears as a field in a class in the corresponding
.java
file
- Notice that every named element appears as a field in a class in the corresponding
-
To add functionality to your buttons you should add a listener for each of them. You can easily do that by right clicking the button (in the
.form
file) and choosingCreate Listener
and choose the type of listener you would like to add. You will usually want to add anAction Listener
(it is called when the button is pressed), but there are different types of listeners. For more information about listeners see the links below. -
Now for the window to appear you will have to create a
Jframe
and set some things (can be seen in the tutorials linked below).-
You can either do it in your action class by doing something like this:
-
or you can extend
Jframe
in the created class and just add to the constructor something like this:In this case you just need to create an instance of the class in order to open the window.
-