Swing Tutorial

Páginas: 77 (19249 palabras) Publicado: 18 de abril de 2011
The Java Swing tutorial
Introduction First Programs Menus and Toolbars Swing Layout Management Swing Events Swing Dialogs Basic Swing Components Basic Swing Components II Swing models Drag and Drop Painting Resizable component Puzzle Tetris This is a Java Swing tutorial. The Java Swing tutorial is for beginners and intermediate Swing developers. After reading this tutorial, you will be able todevelop non-trivial Java Swing applications. Introduction to the Java Swing Toolkit This is an introductory Swing tutorial. The purpose of this tutorial is to get you started with the Java Swing toolkit. The tutorial has been created and tested on Linux. About Swing Swing library is an official Java GUI toolkit released by Sun Microsystems. It is used to create Graphical user interfaces with Java.The main characteristics of the Swing toolkit • • • • • platform independent customizable extensible configurable lightweight

The Swing API has 18 public packages: • • • • • • • javax.accessibility javax.swing javax.swing.border javax.swing.colorchooser javax.swing.event javax.swing.filechooser javax.swing.plaf

• • • • • • • • • • •

javax.swing.plaf.basic javax.swing.plaf.metaljavax.swing.plaf.multi javax.swing.plaf.synth javax.swing.table javax.swing.text javax.swing.text.html javax.swing.text.html.parser javax.swing.text.rtf javax.swing.tree javax.swing.undo

Swing is an advanced GUI toolkit. It has a rich set of widgets. From basic widgets like buttons, labels, scrollbars to advanced widgets like trees and tables. Swing itself is written in Java. Swing is a part of JFC,Java Foundation Classes. It is a collection of packages for creating full featured desktop applications. JFC consists of AWT, Swing, Accessibility, Java 2D, and Drag and Drop. Swing was released in 1997 with JDK 1.2. It is a mature toolkit. The Java platform has Java2D library, which enables developers to create advanced 2D graphics and imaging. There are basically two types of widget toolkits. •Lightweight • Heavyweight A heavyweight toolkit uses OS's API to draw the widgets. For example Borland's VCL is a heavyweight toolkit. It depends on WIN32 API, the built in Windows application programming interface. On Unix systems, we have GTK+ toolkit, which is built on top of X11 library. Swing is a lightweight toolkit. It paints it's own widgets. Similarly does the Qt4 toolkit. SWT library Thereis also another GUI library for the Java programming language. It is called SWT. The Standard widget toolkit. The SWT library was initially developed by the IBM corporation. Now it is an open source project maintained by the Eclipse community. The SWT is an example of a heavyweight toolkit. It lets the underlying OS to create GUI. SWT uses the java native interface to do the job. There is atutorial dedicated to SWT on ZetCode. Java Swing first programs In this chapter, we will program our first programs in Swing toolkit. The examples are going to be very simple. We will cover some basic functionality. Our first example In our first example, we will show a basic window on the screen.

package zetcode; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Exampleextends JFrame { public Example() { setTitle("Simple example"); setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Example ex = new Example(); ex.setVisible(true); } }); }

}

While this code is very small, the application window can do quite a lot. Itcan be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer.
import javax.swing.JFrame; import javax.swing.SwingUtilities;

Here we import Swing classes, that will be used in the code example.
public class Example extends JFrame {

The Example class inherits from the JFrame widget. JFrame is a toplevel container, which is used...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Tutorial De Swing
  • Swing
  • Swing
  • swinger
  • Swing
  • Swinger
  • swing
  • swing

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS