often GUI (Graphical User Interface) development goes unmentioned. This is a quick lesson on how to get started with a Java-based GUI of your own.
Why do we care about GUIs? Most software applications today have to have some sort of user control over them, and this is often accomplished through GUIs.
We’ll start with the idea of a frame. The frame is an box that can house your application, move around the window, resize etc. To make a frame in java is very simple. All you have to do is create a JFrame object and tell it to be visible.
JFrame myFrame = new JFrame("Frame Title");
// Make sure to kill the process when you hit the close button
// Set the Size so that you will be able to see the frame
// Makes it show up!
This example will simply make a frame with a title “Frame Title” and size 400px by 400px show up on your screen. When you hit the X button it will exit the process and close the frame. We can make our GUI’s cooler and more useful by using panels. Panels are containers for components that live inside of frames. In Java we use JPanel objects. To add a JPanel to the above frame, simply add the following code:
JPanel firstPanel = new JPanel(new BorderLayout());
This code will add a new JPanel called firstPanel to the frame we created earlier. To a new user, all of the above code should be pretty straightforward except possibly
new BorderLayout(). This piece of code allows the Panel to organize the components inside of it if it uses a specific layout manager. If this piece of code was omitted, the panels layout manager would be null and it would only be able to do absolute positioning of components inside it. There are a variety of layout managers already provided by default, so before trying to go make your own check out what is already offered here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html.
Additionally, you can add components to your panels. Components can be a variety of things such as text fields or text areas, buttons, or drop down lists. A list of all Java premade components can be found here http://docs.oracle.com/javase/tutorial/ui/features/components.html
By using a combination of these components and panels you can create a variety of looks and input forms for your user to input data.
Hope you enjoyed this quick introduction. Make sure to play around with this stuff as next week we will discuss how to use events to make these components do a variety of tasks.