JAVA: You are making a small program utilizing Swing GUI components. Your frame
ID: 3697548 • Letter: J
Question
JAVA:
You are making a small program utilizing Swing GUI components. Your frame has the following components defined and initialized:
JButton compare
JButton clear
JTextField value 1
JTextField value 2
JTextField output
Provide the implementation of the internal Handler class such that when the compare button is pressed the two values in the JTextFields value1 and value2 are compared (in that order) and the String “Less than”, “Greater than”, or “Equal to” is placed in output depending on their relationship, and when the clear button is pressed the fields value1 and value2 are set to “0”, and the output field is cleared. You can assume that the Strings inputted to the JTextFields are representative of int values.
private class CompareHandler implements ActionListener
{
Public void actionPerformed(ActionEvent event) {
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingGuiextends javax.swing.JFrame
{ public SwingGuiextends()
{
initComponents();
}
private void initComponents()
{
jLabel1 = new javax.swing.JLabel();
value1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
value2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
Output = new javax.swing.JTextField();
Compare = new javax.swing.JButton();
clear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel.setText(" Enter integer Value1");
jLabel2.setText(" Enter integer value2");
jBUtton1.setText("Compare");
jButton1.addActionListener(new java.wt.event.ActionListener(){
public void actionPerformed(java.at.event.actionEvent evt){
jButton1ActionPerformed(evt);
});
jBUtton2.setText("Clear");
jButton2.addActionListener(new java.wt.event.ActionListener(){
public void actionPerformed(java.at.event.actionEvent evt){
jButton2ActionPerformed(evt);
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
int num1=Integer.parseInteger(jTextField1.getText());
int num2=Integer.parseInteger(jTextField2.getText());
if(num1 > num2){
jTextField3.setText("greater than");
}
else if(num1 < num2){
jTextField3.setText ("less than " );
}
else{
jTextField3.setText(" equal to " );
}
}
private class CompareHandler implements ActionListener
{
Public void actionPerformed(ActionEvent event) {
jTextField1.setText("0");
jTextField2.setText("0");
jTextField3.setText(" ");
}
}
public SwingControlDemo(){
prepareGUI();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.