• Tidak ada hasil yang ditemukan

Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "Programming in Java"

Copied!
9
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

•  The value passed in what determines what happens when the window is closed

•  There are several other opEons in addiEon to JFrame.EXIT_ON_CLOSE

•  They are shown here:

DISPOSE_ON_CLOSE HIDE_ON_CLOSE

DO_NOTHING_ON_CLOSE

•  The next line of code creates a Swing JLabel component:

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

•  JLabel is the simplest and easiest-to-use component because it does not accept user input

•  The label created by the program contains only text, which is passed to its constructor.

(4)

A Simple Swing ApplicaEon

•  The next line of code adds the label to the content pane of the frame:

jfrm.add(jlab);

•  To add a component to a frame, you must add it to the frame’s content pane by calling add( ) on the JFrame reference (jfrm in this case)

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

Component add(Component comp)

•  The version of add( ) just shown adds the label to the center locaEon

•  Other versions of add( ) enable you to specify one of the border regions

(5)

A Simple Swing ApplicaEon

•  The last statement in the SwingDemo constructor causes the window to become visible:

jfrm.setVisible(true)

•  The setVisible( ) method is inherited from the AWT Component class

•  If its argument is true, the window will be displayed.

Otherwise, it will be hidden

•  By default, a JFrame is invisible, so setVisible(true) must be called to show it

(6)

A Simple Swing ApplicaEon

•  Inside main( ), a SwingDemo object is created, which causes the window and the label to be displayed

•  NoEce that the SwingDemo constructor is invoked using these lines of code:

SwingUtilities.invokeLater(new Runnable() {

public void run() { new SwingDemo();

} });

•  This sequence causes a SwingDemo object to be

created on the event dispatching thread rather than

on the main thread of the applicaEon

(7)

A Simple Swing ApplicaEon

•  In general, Swing programs are event-driven

•  For example, when a user interacts with a component, an event is generated

•  An event is passed to the applicaEon by calling an event handler defined by the applicaEon

•  However, the handler is executed on the event dispatching thread provided by Swing (not created by your program) and not on the main thread of the applicaEon

•  To avoid problems (including the potenEal for deadlock), all Swing GUI components must be created and updated from the event dispatching thread, not the main thread of the applicaEon

•  However, main( ) is executed on the main thread. Thus, main( ) cannot directly instanEate a SwingDemo object.

Instead, it must create a Runnable object that executes on the event dispatching thread and have this object create the GUI.

(8)

A Simple Swing ApplicaEon

•  main( ) is executed on the main thread

•  Thus, main( ) cannot directly instanEate a SwingDemo object

•  Instead, it must create a Runnable object that executes on the event dispatching thread and have this object create the GUI

•  To enable the GUI code to be created on the event

dispatching thread, you must use one of two methods which are invokeLater( ) and invokeAndWait( )

•  The general form are shown here:

static void invokeLater(Runnable obj) static void invokeAndWait(Runnable obj)

throws InterruptedException, InvocationTargetException

•  Here, obj is a Runnable object that will have its run( ) method called by the event dispatching thread

(9)

A Simple Swing ApplicaEon

•  The difference between the two methods is that

–  invokeLater( ) returns immediately

–  invokeAndWait( ) waits unEl obj.run( ) returns

•  You will normally want to use invokeLater( ), as the preceding program does

•  However, when construcEng the iniEal GUI for an applet, you will need to use invokeAndWait( )

Referensi

Dokumen terkait

Furthermore, using such a static factory method mandates that the client refer to the returned object by its interface rather than by its implementation class, which is generally

The Fundamentals of Java Programming Language course provides a conceptual understanding of Object Oriented programming.. The course also teaches students how to use the JAVA 

FileReader is a class or object that is used to read files, and should be utilized with the BufferedFileReader object, which is going to help save data when reading

This chapter introduced many concepts: invoking object and class methods, creating objects, and how to create

Ways to Create Threads  When a Java program is launched, it has a single thread running main..  This is called the main

//Java Program to create and call a default constructor class Bike1{ //creating a default constructor Bike1{System.out.println"Bike is created";} //main method public static void

Why constructors are efficient instead of a function init defined by the user to initialize the data members of an object?. a Because user may forget to call init using that object

• “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