|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Java Basics Lesson 12: An Introduction to Graphical User Interfaces with Swing - Part I
Everything is a little better, faster, and more convenient in Swing
By: Yakov Fain
Aug. 18, 2005 09:00 AM
Java comes with the whole bunch of classes that you'll be using to create graphical applications. There are two main groups of classes (libraries) that are used for creating windows in Java: AWT and Swing.
AWT and Swing Everything is a little better, faster, and more convenient in Swing, but you can use it only if you're sure that your programs will run on the computers with JVMs that support Swing classes. In this lesson we'll create a simple calculator program using Swing. Note. There is yet another set of Java classes which is a part of Eclipse platform called Standard Widget Toolkit (SWT), but since it's not a part of Java SDK, we won't discuss it here.
Packages and Import Statements Java compiler knows where to find classes that are located in java.lang, but there are many other packages with useful classes, and it's your responsibility to let the compiler know where the classes from your program live. For example, most of the Swing classes live in one of the following two packages:
javax.swing It would be annoying to write a full class name every time you use it, and to avoid this you can write import statements just once above the class declaration line, for example:
import javax.swing.JFrame; These import statements will allow you to use the short class names like JFrame or JButton, and Java compiler will know where to look for these classes. If your need to use several classes from the same package, you do not have to list each of them in the import statement, just use the wild card. In the following example the star (asterisk) makes all classes from the package javax.swing visible to your program: import javax.swing.*; Still, it's better to use separate import statements, so you can see what exactly the class is importing from each package. You can find more about packages in the lesson on packages and imports (http://java.sys-con.com/read/49108.htm).
Major Swing Elements
But displaying a frame is only half of the job, because the window controls should know how to respond to various events, for example a click on the button. In this Lesson we'll learn how to display nice-looking windows, and the next lesson is about writing code that will respond to events that may happen with elements of this window. Our next goal is to create a simple calculator that knows how to add two numbers and display the result. Create a new project in Eclipse named My Calculator and add a new class SimpleCalculator with the following code: Compile and run this program and it'll display a window that looks like this one: ![]()
Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||