The Deep End

go to! email!

Thursday, March 03, 2005

Swing Java Apps Tutorial

Java applications (not Java applets, which are a lot different) begin with a JFrame--although the best place to begin is here, at the Java Tutorial. In the tutorial, they use threads, which may make the program safe but is also very confusing and not fun if you have to hard-code the entire thing anyways.

frame.pack() shrinks the app down to the size of the components--so you don't have a lot of extra space. setVisible(true) makes it visible...

The JFrame is the main window--this kind of makes sense because it's like a picture frame into which you add contents You do this using frame.getContentPane().add(your Component) You can read more about JFrame here.

After you've got a JFrame, you can add all sorts of things. Reading the Swing Tutorial would be a lot better than me putting up everything I know (which isn't a lot) but a few spiffy items to get started with are:


Organizing Your App

To organize your gui (graphical user interface), if you're hard-coding everything, then you'll need to use JPanels. JPanels are like mini-frames in that they start out empty and you can add stuff (Components) like JTextFields and JButtons to them. This makes your app more organized in that you can have inputPanel, which contains all the input Components (like JTextFields and JButtons), and labelPanel which would be the labels that you'll place above the input Components.

Positioning Stuff

To do things like place one JPanel above another, you'll need to use a Layout Manager. A great Java Tutorial is located here. By default, every Container (like a JPanel, JFrame, or anything that has an add() method) uses FlowLayout, which, as stated in the Java Tutorial, basically just adds things in a row.

A lot of the other layouts are really confusing, so the main one that I use is BorderLayout. For how to use BorderLayout, refer to the Java Tutorial segment "How to Use BorderLayout".

Hope that helps!


Post a Comment

<< Home