Learning Java/Swing and AWT
There are two main ways to create a GUI in Java. There is the AWT, which was the first one, and is older. Then there is Swing. Swing is a newer way of making a GUI. All objects in Swing are derived from AWT, and most objects in Swing start with the letter J.
Hint: Oracle has developed JavaFX to replace both Swing and AWT. Since Java SE 7, update 6 it is bundled with Java SE. "JavaFX is a set of graphics and media packages that enables developers to (...) deploy rich client applications that operate consistently across diverse platforms" [1]. As of 2017, Swing and AWT are not declared as deprecated.
Making a JFrame
edita JFrame is an extended kind of window in Java, it is created like this:
import java.awt.*;
import javax.swing.*; // needed for frame creation
public class testJFrame
{
public static void main(String[] arg)
{
JFrame frame = new JFrame("windowName");
frame.setBounds( 100,100,500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
explanation:
The first statement creates the frame. It is not necessary to give the frame a title, JFrame() works as well.
The second statement sets the size and position of the frame, first the position(x,y) (in pixels) and the second the size(x,y)
The third statement sets what happens when you press "x". there is also DO_NOTHING_ON_CLOSE, I think you can guess what this does.
The last statement sets the frame visible. It is no use making a frame if it is not visible. You can also say setVisible(false), which you can use when you would like to remove a component from a frame (for example if you want a warning message to disappear).
Components
editthere are quite a lot of components in Java. Here are some examples (a full list for your JDK is in the java documentation):
JLabel a simple label that cannot be pressed, and shows a piece of text.
JLabel label = new JLabel("name");
JButton a Button that can be pressed.
JButton button = new JButton("name");
to figure out if a button is pressed you can write:
button.addActionListener(this);
then you should make a ActionPerformed method(see example)
JCheckBox is a widely known on/off switch, common in many user interfaces.
JCheckBox checkBox = new JCheckBox("name");
To figure out if it is activated, you need to call its method isSelected(). JCheckBox also fires an action event when changing the state, so it is possible to register an action listener for it, same as for a button. This allows to update something else immediately when the box state changes.
notice that you can only add one Item to a window. This Item will take up all the window room. To add more components to a frame, add them to a Container, and set a flowLayout, like this:
Container c = new Container(); c = window.getContentPane(); FlowLayout flow = new FlowLayout(); c.setLayout(flow); c.add(component1); c.add(component2); ...
Example
editin this example a JCheckBox and a JLabel are added to a JFrame, so that the Label is only visible when the CheckBox is activated.
import javax.swing.*; // for the frame import java.awt.*; // for the checkBox and the label import java.awt.event.*; // for the checkBox listener public class tst implements ActionListener { // create pointers private boolean checkedOnce; // this boolean says if the checkBox is checked or not private JFrame window; private JLabel label; private JCheckBox cb; private Container c; public static void main(String[]x) { new tst(); } public tst() //Constructor { //create objects to pointers label = new JLabel("checkBox activated"); cb = new JCheckBox("check here" , false); window = new JFrame("test"); window.setBounds(100,100,200,200); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // define c c = window.getContentPane(); // create and set layout FlowLayout fl = new FlowLayout(); c.setLayout(fl); // add Items to Container c.add(cb); c.add(label); checkedOnce = false; // checkedOnce must have a value, otherwise it is null window.setVisible(true); label.setVisible(false); cb.addActionListener(this); } public void actionPerformed(ActionEvent e) // this method is called when one of the objects that have a listener is activated { if(checkedOnce == false) { label.setVisible(true); checkedOnce = true; // set true, so that the next time the checkBox is klicked on, the label dissapears } else { checkedOnce = false; label.setVisible(false); } } }