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 1will 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

See image: see image.

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);
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.