• Tidak ada hasil yang ditemukan

Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "Programming in Java"

Copied!
8
0
0

Teks penuh

(1)

Programming in Java

CBCS BHCS03

By Parul Chachra

(2)

Chapter 31

Introducing Swing

Reference: Schildt, H. Java: The Complete Reference. 7th ediEon. McGraw-

Hill EducaEon

(3)

A Simple Swing ApplicaEon

•  Swing programs differ from both the console-based programs and the AWT-based programs, for example,

–  They use a different set of components and a different container hierarchy than does the AWT

–  Swing programs also have special requirements that relate to threading

•  The following program shows one way to write a Swing applicaEon

•  It uses two Swing components: JFrame and JLabel

•  JFrame is the top-level container that is commonly used for Swing applicaEons

•  JLabel is the Swing component that creates a label, which is a component that displays informaEon

•  The label is Swing’s simplest component because it is passive. That is, a label does not respond to user input

•  The program uses a JFrame container to hold an instance of a JLabel

(4)

A Simple Swing ApplicaEon

// A simple Swing application.

import javax.swing.*;

class SwingDemo { SwingDemo() {

// Create a new JFrame container.

JFrame jfrm = new JFrame("A Simple Swing Application");

// Give the frame an initial size.

jfrm.setSize(275, 100);

// Terminate the program when the user closes the application.

jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a text-based label.

JLabel jlab = new JLabel(" Swing means powerful GUIs.");

(5)

A Simple Swing ApplicaEon

// Add the label to the content pane.

jfrm.add(jlab);

// Display the frame.

jfrm.setVisible(true);

}

public static void main(String args[]) {

// Create the frame on the event dispatching thread.

SwingUtilities.invokeLater(new Runnable() {

public void run() { new SwingDemo();

} });

} }

(6)

A Simple Swing ApplicaEon

•  Swing programs are compiled and run in the same way as other Java applicaEons

•  To compile this program, you can use this command line:

javac SwingDemo.java

•  To run the program, use this command line:

java SwingDemo

•  When the program is run, it will produce a window similar to that shown:

(7)

A Simple Swing ApplicaEon

•   The program begins by imporEng javax.swing

•  As menEoned, this package contains the components and models defined by Swing

•  For example, javax.swing defines classes that

implement labels, buUons, text controls, and menus

•   Next, the program declares the SwingDemo class and a constructor for that class

•  The constructor begins by creaEng a JFrame, using this line of code:

JFrame jfrm = new JFrame("A Simple Swing Application”)

•   This creates a container called jfrm that defines a

rectangular window complete with a Etle bar; close,

minimize, maximize, and restore buUons; and a system

menu

(8)

A Simple Swing ApplicaEon

•  Next, the window is sized using this statement:

jfrm.setSize(275, 100);

•  The setSize( ) method sets the dimensions of the window, which are specified in pixel

•  Its general form is shown here:

void setSize(int width, int height)

•  By default, when a top-level window is closed, the window is removed from the screen, but the applicaEon is not

terminated

•  However, if you want the enEre applicaEon to terminate when its top-level window is closed, then call

setDefaultCloseOpera@on( ), as the program does:

jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

•  The general form of setDefaultCloseOperaEon( ) is shown here:

void setDefaultCloseOperation(int what)

Referensi

Dokumen terkait

A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods.. For

xxi Chapter 1 Introduction to Computers, Programs, and Java 1 Your first Java program 12 Compile and run a Java program 17 NetBeans brief tutorial 23 Eclipse brief tutorial 25

The MVC Architecture • By separaEng a component into a model, a view, and a controller, the specific implementaEon of each can be changed without affecEng the other two • For

In the next section, we illustrate by means of a simple example the manner in which a simple linear aggregate objective function leads to extreme solutions, which are seldom if ever

A Better Way As observant Java programmers, the minute we set our eyes on this code we’d quickly turn it into something more concise and easier to read, like this:

Top – Level Containers • The layered pane is an instance of JLayeredPane • The layered pane allows components to be given a depth value • The layered pane holds the content pane

• “Object” means a particular item that belongs to a class – Also called an “instance” • Example String s1 = "Hello"; – Here, String is the class, and the variable s1 and the value

poly + morphism • poly is Latin for “many”; morph is Latin for “form” • in Java, a method or constructor may have multiple signatures • this is called overloading • EXAMPLE: Cat c1