DOC

javalessonsdoc - httpjava

By Bernice Brooks,2014-04-24 13:27
9 views 0
javalessonsdoc - httpjava

    Topics illustrated in this example:

    ? Look and Feel

    ? Setting Up Buttons and Labels

    ? Handling Events

    ? Adding Borders Around Components Let's look at another simple program, SwingApplication. Each time the user clicks the button (JButton), the label (JLabel) is updated.

The following figure shows three views of a GUI that uses Swing

    components. Each picture shows the same program,

    SimpleExample,

    but with a different look and feel.

    Java look and feel

    CDE/Motif look and feel

    Windows look and feel

    Swing allows you to specify which look and feel your program uses--

    SwingApplication specifies its look and feel: Java look and feel, CDE/Motif look and feel, Windows look and feel,

    and so on. The code in boldface type in the following snippet shows

    you how public static void main(String[] args) {

     try {

     } catch (Exception e) { }

     ...// Create and show the GUI...

    }

    The preceding code essentially says, "I don't care whether the user has

    chosen a look and feel-use the cross-platform look and feel (the Java

    look and feel)."

    Like most GUIs, the SwingApplication GUI contains a button and a label.

    (Unlike most GUIs, that's about all that SwingApplication contains.)

    Here's the code that initializes the button:

    JButton button = new JButton("I'm a Swing button!");

    button.setMnemonic('i');

    button.addActionListener(...create an action listener...); The first line creates the button. The second sets the letter "i" as the

    mnemonic that the user can use to simulate a click of the button. For

    example, in the Java look and feel, typing Alt-i results in a button click.

    The third line registers an event handler for the button click, as

    discussed later in this section.

    Here's the code that initializes and manipulates the label:

    ...// where instance variables are declared:

    private static String labelPrefix = "Number of button clicks: ";

    private int numClicks = 0;

    ...// in GUI initialization code:

    final JLabel label = new JLabel(labelPrefix + "0 ");

    ...

    label.setLabelFor(button);

    ...// in the event handler for button clicks:

    label.setText(labelPrefix + numClicks);

    The preceding code is pretty straightforward, except for the line that

    invokes the setLabelFor method. That code exists solely to hint to assistive technologies that the label describes the button.

    Now that you know how to set up buttons, you also know how to

    set up check boxes and radio buttons, as they all inherit from the

    AbstractButton class. Check boxes are similar to radio buttons, but by convention their selection models are different. Any number

    of check boxes in a group-none, some, or all-can be selected.

    On the other hand, only one button can be selected from a

    group of radio buttons. The following figures show screenshots

    of two programs that use check boxes and radio buttons.

    Caption: As you'd expect, the

    CheckBoxDemo

    application shows the use of check boxes, and the

    RadioButtonDemo application shows the use of radio

    buttons.

    You'll get a chance to take a closer look at radio buttons in the section

    Example Five: VoteDialog. Every time the user types a character or pushes a mouse button, an

    event occurs. Any object can be notified of the event. All the object

has to do is implement the appropriate interface and be registered as an

    event listener on the appropriate event source.

    Every event handler requires three pieces of code:

    1. In the declaration for the event handler class, one line of code

    specifies that the class either implements a listener interface or 2. public class MyClass implements extends a class that implements a listener interface. For

    example: ActionListener {

    3. Another line of code registers an instance of the event handler

    class as a listener on one or more components. For example:

    4. someComponent.addActionListener(instance

    OfMyClass);

    5. In the event handler class, a few lines of code implement the

    methods in the listener interface. For example:

    6. public void actionPerformed(ActionEvent e) {

    7. ...//code that reacts to the action...

    8. } Event handlers can be instances of any class. Often an event handler

    that has only a few lines of code is implemented using an anonymous

    inner class--an unnamed class defined inside of another class.

    Anonymous inner classes can be confusing at first, but once you're

    used to them, they make the code clearer by keeping the

    implementation of an event handler close to where the event handler is

    registered.

    SwingApplication has two event handlers. One handles window closing (window events); the other handles button clicks (action

    events). We've already seen the window-closing code. Here is

    the code that handles button clicks in the SwingApplication:

    button.addActionListener(new ActionListener() {

     public void actionPerformed(ActionEvent e) {

     numClicks++;

     label.setText(labelPrefix + numClicks);

     }

    });

In general, to detect when the user clicks an on-screen button (or does

    the keyboard equivalent), a program must have an object that

    implements the ActionListener interface. The program must register this

    object as an action listener on the button (the event source), using the

    addActionListener method. When the user clicks the on-screen button,

    the button fires an action event. This results in the invocation of the

    action listener's actionPerformed method (the only method in the

    ActionListener interface). The single argument to the method is an

    ActionEvent object that gives information about the event and its source.

Swing components can generate many kinds of events. The following

    table lists a few examples.

    Examples of Events and Their Associated Event Listeners

    Act that Results in the Event Listener Type

    User clicks a button, presses Return while typing

    in a text field, or chooses a menu item ActionListener

    User closes a frame (main window) WindowListener

    User presses a mouse button while the cursor is MouseListener over a component

    User moves the mouse over a component MouseMotionListener

    Component becomes visible ComponentListener

    Component gets the keyboard focus FocusListener

    Table or list selection changes ListSelectionListener

    To learn more about how to detect events from a particular

    component, refer to each component's how-to section in Using

    Swing Components.

    Note: Event-handling code executes in an single thread,

    the event-dispatching thread. This ensures that each

    event handler finishes execution before the next one

    executes. For instance, the

    actionPerformed method in

    the preceding example executes in the event-dispatching

    thread. Painting code also executes in the event-

    dispatching thread. Therefore, while the actionPerformed

    method is executing, the program's GUI is frozen--it

    won't repaint or respond to mouse clicks. See the

    section Threads and Swingfor more information.

    SwingApplication, you'll

    notice that there is extra space surrounding the JPanel on all four edges.

    If you take another look at the snapshot of

    Here is the code that adds a border to the panel:

    pane.setBorder(BorderFactory.createEmptyBorder(

     30, //top

     30, //left

     10, //bottom

     30) //right

     );

    This border simply provides some empty space around the panel's

    contents--30 extra pixels on the top, left, and right, and 10 extra pixels

    on the bottom. Borders are a feature that JPanel inherits from the

    JComponent class.

****************************************************************

http://java.sun.com/docs/books/tutorial/idl/hello/

    Lesson: The Hello Client-Server Example

    This tutorial teaches the basic tasks in building a CORBA distributed application

    using Java IDL. You will build the classic Hello World program as a distributed

    application, with both applet and application clients. The Hello World program has a

    single operation that returns a string to be printed. CORBA terminology and the

    underlying functionality of the application is discussed in

    The CORBA

    Architecture. The application diagram is repeated here, along with a review of the

    steps in the process of communication between the client and server.

    sayHello operation of the HelloServer.

     The ORB transfers that invocation to the servant object registered for that IDL Communication between the client and server. interface.

    The client (applet or application) invokes the The servant's sayHello method runs, returning a Java String.

    The ORB transfers that String back to the client.

    The client prints the value of the String.

    Despite its simple design, the Hello World program lets you learn and experiment

    with all the tasks required to develop almost any CORBA program that uses static

    invocation.

    Getting Started

    Before you start working with Java IDL, you need two things: version 1.2 of the JDK

    software and the idltojava compiler. The JDK provides the API and ORB needed to

    enable CORBA-based distributed object interaction. The idltojava compiler uses the

    IDL-to-Java mapping to convert IDL interface definitions to corresponding Java

    interfaces, classes, and methods, which you can then use to implement your client and

    server code. Click here to download and install the idltojava compiler

     TMNote: The Java 2 Platform, Standard Edition, v. 1.3, now includes a new version

    of the IDL-to-Java compiler, idlj. The idlj compiler replaces the idltojava compiler

    that was available as a separate download in previous releases. You can download the

    TMJava 2 Platform, Standard Edition, v. 1.3 and visit Java IDL Documentation for more information on Java IDL in the 1.3 release.

Writing the IDL Interface

    This section teaches you how to write a simple IDL interface definition and how to

    translate the IDL interface to Java. It also describes the purpose of each file generated

    by idltojava compiler.

    Developing a Client Application

    Learn how to write a simple client application, including how to create an ORB object,

    how to use the naming service to get an initial object reference, and how to invoke an

    operation on a CORBA object.

    Developing the Hello World Server

    This section has all the information you need to write a simple IDL server, including

    how to create an ORB object for the server, how to instantiate the servant and connect

    Compiling and Running the Hello World Application

    After you've written the client and the server, you're ready to compile the code and it to the ORB, how to register the servant with the naming service, and how to make run the programs. the server wait for invocations on the servant.

    Using Stringified Object References

    This section tells you how to make an object reference when there is no naming

    service.

Report this document

For any questions or suggestions please email
cust-service@docsford.com