Java Swings/Swings
Contents |
[edit] First GUI
OK, its now time that you start to dive into swings. Fire up your text editor or IDE and type the code as shown below.
import javax.swing.JFrame; public class HelloSwing { public static void main(String[] args) { JFrame f = new JFrame(); //We create a new frame f.setSize(200, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.setVisible(true); } }
Output
Compile the code and run it. If you get the output as shown, you have done it all right.
[edit] How it works?
Now let's examine how it works. As in all java programs, this program starts with creating a class, we have named this class HelloSwing. This class contains the main method from where the program starts execution. The first line in this function is
JFrame f = new JFrame();
In the first line we create new Java Frame or JFrame. A frame is nothing but a Swing container that holds other components like labels, buttons, text field, menu bars etc. Now since we have created a frame and before displaying it, we need to set its size. To set the size of the frame we use the following code.
f.setSize(200, 100);
The setSize(int width, int height), receives two arguments, the first one is the frames width and the second one as the frame height. We have assigned the frame size to be 200 pxles (px) wide and 100px in height. So the frames dimensions has been set successfully.
Now what will happen if one clicks the close button in the frame? We want the program to exit and clear off the memory when close button is clicked, hence to close the frame we use the function setDefaultCloseOperation(). We want the program to exit when the close button is pressed and ence we use the following code
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
All has been done right, all we need to do now is to make the frame visible. For that we use the setVisible(bool b) function. This function accepts one boolean input. If passed true, the frame is visible, else if passed flase the frame is invisible. To make the frame visible, we use the following code.
f.setVisible(true);
So we have created our HelloSwing program, all must be done now is to run it!
[edit] Diving Into Swings
[edit] Frame with Title
In the last example we saw how to create a frame that has nothing in it. This time we will create a frame that has a title. For that lets create a class FrameWithTitle. All the code is just the same as previous example with just one change. Notice the first statement in main method its
JFrame f = new JFrame("Hello Swing");
Here the JFrame() constructor gets a string argument. This argument forms the title of the frame. Simple!
import javax.swing.JFrame; public class FrameWithTitle { public static void main(String[] args) { JFrame f = new JFrame("Hello Swing"); //We create a new frame f.setSize(200, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.setVisible(true); } }
Output
Compiling the FrameWithTitle code will give an output as shown above.
[edit] Adding Labels
We hve created an empty frame, a frame with a title, now let us see how to add a simple component to a frame. Label is a simple component and we will add it to our frame. Read the program below carefully. It has a new statement when compared to the previous example.
import javax.swing.JFrame; import javax.swing.JLabel; public class AddingLabel { public static void main(String[] args) { JFrame f = new JFrame("Hello Swing"); //We create a new frame f.setSize(200, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.add(new JLabel("Diving into swing!")); //Adding a label f.setVisible(true); } }
Output
Look at the line
f.add(new JLabel("Diving into swing!"));
In this line we tell the computer to add a component or object to the frame using add() function. Since we want to add a label we pass a new label object using new JLabel() command.
The new JLabel("Diving into swing!") creates a new label. This is added to the frame f using the command using f.add(new JLabel("Diving into swing!")); . Once added, the frame is displayed using f.setVisible(true);
If all had been done right, you must be getting output as shown above.
[edit] Label and Text
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class LabelnText { public static void main(String[] args) { JFrame f = new JFrame("Hello Swing"); //We create a new frame f.setSize(200, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.add(new JLabel("Diving into swing!")); //Adding a label f.add(new JTextField("Type something here"));//Adding text field f.setVisible(true); } }
Output
In the last example (AddingLabel.java) we have seen how to add a JLabel to a JFrame. Now we will increase our knowledge by adding more components. Lets add a text field (that's JTextField) along with label. The program shown above (LabelnText.java) has the same code of AddingLabel.java but with a minor difference. Look at the penultimate line f.add(new JTextField("Type something here"));, in this line we have added a JTextField to frame f using the add() method. To the add method we pass a new JTextField using new JTextField("Type something here") statement. The JTextField() constructor can accept a string argument which is set as text value of the text field.
So when running the exaple we would expect the frame to have a label and text field at the right of it. But look at the output. Whats wrong? All We see is a text field that occupies the entire frame! Java has a bug? The answer is both label and text fields are present in the frame. The text field is drawn on top of the label.
Java Virtual Machine is dumb. You told to put an label, so it did put one onto the frame, next you told to put a text field so it did faithfully. But it put the text field on top of the label. To get a desired output like a label and text field laid next to each other we must use whats called a Layout Manager which you will be briefed about shortly.
[edit] Layout Manager
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class LayoutManagerDemo { public static void main(String[] args) { JFrame f = new JFrame("Hello Swing"); //We create a new frame f.setSize(200, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.setLayout(new FlowLayout());//Adding layout, flow layout in this cae f.add(new JLabel("Diving into swing!")); //Adding a label f.add(new JTextField("Type something here"));//Adding text field f.setVisible(true); } }
Output
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class LayoutManagerDemo1 { public static void main(String[] args) { JFrame f = new JFrame("Hello Swing"); //We create a new frame f.setSize(500, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.setLayout(new FlowLayout());//Adding layout, flow layout in this case f.add(new JLabel("Diving into swing!")); //Adding a label f.add(new JTextField("Type something here"));//Adding text field f.setVisible(true); } }
Output
[edit] Packing Frames
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class PackingFrames { public static void main(String[] args) { JFrame f = new JFrame("Hello Swing"); //We create a new frame //f.setSize(500, 100); //Setting size of width=200px and height=100px f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when closed button is presses f.setLayout(new FlowLayout()); f.add(new JLabel("Diving into swing!")); //Adding a label f.add(new JTextField("Type something here"));//Adding text field f.pack();//Packing components into frmes. This is like auto sizing of frames f.setVisible(true); } }
Output





