Custom Search

Friday, May 20, 2011

Intro to GUI

In this post we will cover basics of GUI's(graphical user interface).  In this post we will build a frame with a text area.  Well, lets begin!


import javax.swing.*;
public class Main {
public static void main(String[]args){
JFrame frame = new JFrame("The title bar");
JTextArea text = new JTextArea();
frame.setSize(300,300);
frame.setVisible(true);
frame.getContentPane().add(text);
frame.setdefaultcloseoperation(JFrame.EXIT_ON_CLOSE);
}
}
In this code we are importing the javax.swing package( the * means import everything in the package), declaring a new JFrame variable named frame(a JFrame is a frame or window), Creating a text area named text, setting the size to 300 300(300 pixels in width, 300 pixels in height), and finally getting the content pane of the frame(think of the frame as a window pane and we are painting(adding) test to fill the window pane.  When you build and compile this program no matter how to alter the dimensions of the frame the text area will always fit the size of the frame.  This is because we added text to the whole frame(in later posts we will learn how to control placement and size of things such as text areas) so the size of the frame is the size of text.

If you have any questions on GUI's or ideas for future posts feel free to leave a comment below!  Well, that wraps it up for this post, see you in the next!

No comments:

Post a Comment