GUI Message

Anonymous
timer Asked: Dec 17th, 2017
account_balance_wallet $15

Question description

please use Eclipse Jee Oxygen and can you combine all the code to word

TOPICS

Graphical User Interfaces (GUI)

DESCRIPTION

Write a GUI (Graphical User Interface) application that will have the following GUI components:

1.A read-only JTextField object about 10 columns wide.

2.A JButton object labeled “Click Me.”

3.A JButton object labeled “Click Me Too”.

When the user clicks the button “Click Me” the first time message “Hello 1”will show in the text field. When the user clicks the button a second time, the message “Hello 2” will show in the text field. Each time the user clicks the button “Click Me”, the number in the message increments. If the user clicks the button “Click Me Too”, the number in the message decrements. The number in the message can become positive or negative depending upon the sequence in which the two buttons are pressed.

In the example below, we press the button “Click Me” two times. Then we click the button “Click Me Too” three times.

Hello 1

Hello 2

Hello 1

Hello 0

Hello -1

In the example below, we press the button “Click Me Too” two times. Then we click the button “Click Me” three times.

Hello -1

Hello -2

Hello -1

Hello 0

Hello 1

GUI Output

IMPLEMENTATION

Create the following two classes.

Class JFrameExt

Create a class JFrameExt that extends the JFrame and implements interface ActionListener. The class will provide the following:

A private instance variable JButton with label “Click Me”.

A private instance variable JButton with label “Click Me Too”.

A private instance variable JTextField of size 10 columns.

A constructor to create the GUI.

An event handler to handle button click events.

Constructor.

In the constructor of the JFrameExt class do the following:

Get the container content pane.

Create a FlowLayout object.

Call the container’s setLayout method and pass it the FlowLayout object.

Call the container’s add method and add the “Click Me” JButton object.

Call the container’s add method and add the JTextField object.

Call the container’s add method and add the “Click Me Too” JButton object.

Call the setEditable method of JTextField object and pass it false.

Call “Click Me” JButton’s addActionListener method and register yourself for listening button click events.

Call “Click Me Too” JButton’s addActionListener method and register yourself for listening button click events.

Event Handler

Write an event handler actionPerformed in the class JFrameExt for handling button click events.

Code this handler to display a message in the JTextField object.

Class TestJFrameExt

Create a class TestJFrameExt containing the main method. In the main method do the following:

Create a JFrameExt object.

Set the size of JFrameExt object to 400 by 300.

Make the JFrameExt object show up.

SAMPLE CODE

Sample Code For Class JFrameExt

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class JFrameExt extends JFrame implements ActionListener

{

//Declare and Create GUI objects

private JTextField jtfMessage = new JTextField (10);

private JButton jbtClickMe = new JButton ("Click Me");

private JButton jbtClickMeToo = new JButton ("Click Me Too");

private JPanel jpMain = new JPanel( );

private int count = 0;

//prepare the GUI interface in the constructor.

public JFrameExt ( )

{

super ( );

//Create a layout manager object

FlowLayout fl = new FlowLayout ( );

//set fl to the jpMain layout manager

jpMain.setLayout(fl);

//make jpMain as the content pane

this.setContentPane (jpMain);

//add the GUI objects to the container

jpMain.add(jbtClickMe);

jpMain.add(jtfMessage);

jpMain.add(jbtClickMeToo);

//Make the text field non editable.

jtfMessage.setEditable (false);

//register this object as an ActionListener with each button

jbtClickMe.addActionListener(this);

jbtClickMeToo.addActionListener(this);

}

//Action Event handler method.

//jbtClickMe and jtbClickMeToo objects will call this method when clicked.

public void actionPerformed (ActionEvent e)

{

//Find out which button was clicked.

if (e.getSource ( ) == jbtClickMe)

{

count++;

jtfMessage.setText(“Hello “ + count);

}

else if (e.getSource ( ) == jbtClickMeToo)

{

count--;

jtfMessage.setText(“Hello “ + count);

}

}

}

Sample Code For Class TestJFrameExt

public class TestJFrameExt

{

public static void main (String [] args)

{

//create the JFrameExt object

JFrameExt f = new JFrameExt ( );

//set frame’s size. (default is 0,0)

f.setSize (400, 200);

//show the frame (default is noshow)

f.setVisible (true);

}

}

DISCUSSION

Program Driven Input/Output

The program decides when and what to input and output. The program synchronizes input/output activity. This type of input/output is also called synchronous input/output.

Event Driven Input/Output

The input and output is performed by the program but different events dictate when and what to input and output. The events may be produced by user initiating a particular input. The program cannot predict what input the user will initiate next. Hence it cannot synchronize input/output. This type of input/output is also called asynchronous input/output.

Graphical User Interface (GUI)

Graphical User Interfaces (GUI) provide User Drive input/output. A user decides what to do next. The user may choose to do a copy or past or search or open a file.

User driven input is handled by a technique called Event Driven Programming.

Event Driven Programming

In Event Driven programming, the program waits for an event. The user initiates an input. This creates an event. The program handles the event by taking an appropriate action. Then goes back to waiting for the next event.

In this type of programming, the program consists of a number of methods. Each method is written to handle a specific event and od is called an Event Handler. Thus the program primarily consists of a group of Event Handlers. Writing an Event Driven program involves writing a bunch of event handlers.

The second part of a GUI program is that the Graphical User Interface provides a number of Graphical components to perform input/output. There are menu items, text fields, buttons, check boxes, radio buttons etc to do various inputs and there are components such as labels, text areas, dialog boxes etc to perform output.

The program is written to do input and output using GUI components and it provides event handler to handle events created by user inputs.

GUI Component

A GUI component is much like a Java object. For example, two buttons displayed are really two JButton objects created by the program. Similarly, other GUI components are simply instances of classes such as JTextField, JLabel, JButton, JRadioButton, JList, JTextArea etc.

What is the difference between an instance of a regular class versus a Graphical Component class?

What is the difference between an instance of a Student clas versus a JButton class.

A Student object has an internal representation that includes values of its instance variable and contains associated instance methods.

A GUI object such as a JButton object has an internal representation just like any other regular object. The program interacts or manipulates this representation. It calls the method of this representation. For example, if the programmer wants to change the contents of a JButton objece, it calls its setText method and changes its text. Similarly, it if wants to determine the current JButton text, it calls its method getText and gets the current text. However, it addition to an internal representation, a GUI component also has an external representation. The two representations are synched together. In a JTextfield, if the program wants to change its contents, it calls its method setText. That changes its text. Similary, if the user types in a text in a Text field, its text value changes for the programmer. Either the programmer can change its instance variable values by calling the methods of its internal representation or the user can change the same properties by interaction with the external representation. The two representations always remain in synch.

The programmer manipulates the internal representation that changes the external representation. Alternatively, the user may change the external representation which will change the internal representation.

For example in displaying a window (JFrame), the programmer may set its width and height by calling its setWidth and setHeight method. Alternatively, the user may change its width and height using a mouse drag mechanism. In both cases, the values of instance variables width and height are kept in synch.

JFrame as A Window

In Java, each GUI program starts by showing an input/output window. This window corresponds to a JFrame object. In other words, Java provides a built in object that can be used to display the initial window.

Of course, this window does very little. In a GUI program, the programmer extends the JFrame and adds additional functionality and then displays the extended JFrame. JFrame acts as a container. The programmer can display in the JFrame container other Gui objects such as JButtons, JTextfields etc.

Creating A GUI program.

Creating a GUI program involves the following:

Create a class that extends JFrame. In the constructor of this class, create the GUI as below:

Creating GUI components such as JButtons, JTextfields.

Get JFrame’s container (content pane).

Create a layout manager such as FlowLayout.

Give the layout manager to the container (call container’s setLayout method)

Add Gui components to container (call container’s add method)

Register with the component (such as JButton object) from which you want to get the event from.

Write the event handler for receiving the event (such as event handler actionPerformed).

Create another class containing the main method. In the main method, do the following:

Create an object of extended JFrame class above (use new to create the object).

Set the size of the extended JFrame. (call setSize)

Show the JFrame (call setVisible)

Note that when you create the JFrameExt, its constructor is called which creates its GUI. So when the main method shows the JFrameExt object, you will see the GUI.

Tutor Answer

lasermaster
School: UIUC

Attached.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyFrame extends JFrame implements ActionListener
{

private JTextField jtf= new JTextField (10);
private JBu...

flag Report DMCA
Review

Anonymous
Outstanding Job!!!!

Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors