Question

In: Computer Science

How can I make this program able to implement listener for the editable text field: ///////////////////////////////...

How can I make this program able to implement listener for the editable text field:

/////////////////////////////// KiloConverter.java //////////////////////////

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class KiloConverter extends JFrame {

// constant for converting km to miles

static final double KM_TO_MILES = 0.6214;

// important components that needs to be accessed throughout the program

private JTextField input, output;

// constructor initializing GUI

public KiloConverter() {

// passing title to super class

super("Kilometer Converter");

// will exit on click

setDefaultCloseOperation(EXIT_ON_CLOSE);

// creating a panel

JPanel panel1 = new JPanel();

// creating components for first row, adding to the above panel

JLabel label1 = new JLabel("Enter a distance in kilometers");

input = new JTextField(15);

JButton calc = new JButton("Calculate");

panel1.add(label1);

panel1.add(input);

panel1.add(calc);

// creating another panel

JPanel panel2 = new JPanel();

// creating components for second row, adding to the above panel

output = new JTextField(15);

output.setEditable(false); // not editable

output.setBackground(Color.LIGHT_GRAY);

JLabel label2 = new JLabel("Miles");

JButton exit = new JButton("Exit");

panel2.add(output);

panel2.add(label2);

panel2.add(exit);

// adding first panel to the north of window, second to the south

add(panel1, BorderLayout.NORTH);

add(panel2, BorderLayout.SOUTH);

// adding action listeners to the buttons

calc.addActionListener(new CalculateButtonListener());

exit.addActionListener(new ExitButtonListener());

// using compact size

pack();

}

// private inner class representing action to be performed when calculate

// button is clicked

private class CalculateButtonListener implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

// getting input, parsing to double, converting to miles, displaying

// with 3 digits precision

double km = Double.parseDouble(input.getText());

double miles = km * KM_TO_MILES;

output.setText(String.format("%.3f", miles));

}

}

// private inner class representing action to be performed when exit

// button is clicked

private class ExitButtonListener implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

//simply exiting

System.exit(0);

}

}

}

// /////////////////////// Driver.java /////////////////////////////////

//public driver program to run the KiloConverter class

public class Driver {

public static void main(String[] args) {

KiloConverter converter = new KiloConverter();

converter.setVisible(true);

}

}

Solutions

Expert Solution

// KiloConverter.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class KiloConverter extends JFrame {

   // constant for converting km to miles

   static final double KM_TO_MILES = 0.6214;

   // important components that needs to be accessed throughout the program

   private JTextField input, output;

   // constructor initializing GUI

   public KiloConverter() {

   // passing title to super class

   super("Kilometer Converter");

   // will exit on click

   setDefaultCloseOperation(EXIT_ON_CLOSE);

   // creating a panel

   JPanel panel1 = new JPanel();

   // creating components for first row, adding to the above panel

   JLabel label1 = new JLabel("Enter a distance in kilometers");

   input = new JTextField(15);

   JButton calc = new JButton("Calculate");

   panel1.add(label1);

   panel1.add(input);

   panel1.add(calc);
  
   // creating another panel

   JPanel panel2 = new JPanel();

   // creating components for second row, adding to the above panel

   output = new JTextField(15);

   output.setEditable(false); // not editable

   output.setBackground(Color.LIGHT_GRAY);

   JLabel label2 = new JLabel("Miles");

   JButton exit = new JButton("Exit");

   panel2.add(output);

   panel2.add(label2);

   panel2.add(exit);

   // adding first panel to the north of window, second to the south

   add(panel1, BorderLayout.NORTH);

   add(panel2, BorderLayout.SOUTH);

   // adding action listeners to the buttons

   // adds action listener for the input textfield, so that after the user enters the input value and press enter, it calculates and displays the resultant value in output textfield

input.addActionListener(new CalculateButtonListener());
  
   calc.addActionListener(new CalculateButtonListener());

   exit.addActionListener(new ExitButtonListener());

   // using compact size

   pack();

   }
  
   // private inner class representing action to be performed when calculate

   // button is clicked

   private class CalculateButtonListener implements ActionListener {

   @Override

   public void actionPerformed(ActionEvent e) {

   // getting input, parsing to double, converting to miles, displaying

   // with 3 digits precision

   double km = Double.parseDouble(input.getText());

   double miles = km * KM_TO_MILES;

   output.setText(String.format("%.3f", miles));

   }

   }
  
   // private inner class representing action to be performed when exit

   // button is clicked

   private class ExitButtonListener implements ActionListener {

   @Override

   public void actionPerformed(ActionEvent e) {

   //simply exiting

   System.exit(0);

   }

   }
  
}

//end of KiloConverter.java

// /////////////////////// Driver.java /////////////////////////////////

//public driver program to run the KiloConverter class

public class Driver {

public static void main(String[] args) {

KiloConverter converter = new KiloConverter();

converter.setVisible(true);

}

}

//end of Driver.java


Related Solutions

Swift - Implement a simple text editor program that includes a text input field (with several...
Swift - Implement a simple text editor program that includes a text input field (with several lines), a text label, and two buttons, called "Open" and "Save". When the button "Save" is touched, the app will save the contents of the text input field to a data file (sample.txt). When the button "Open" is touched, the app will read the content from the data file, (sample.txt)) and feed the content into the text label. Thank you.
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
As a challenge question i was asked to make a program that can make a newly...
As a challenge question i was asked to make a program that can make a newly made Directory that has the name input from the user. I am to create a program that can make a txt file in the newly created directory. I am doing this in windows and this is what i got. #include <windows.h> #include <string> #include <iostream> #include <string.h> #include <fstream> #include <TlHelp32.h> using namespace std; int main() { string Text = "Test string"; ofstream file;...
The text is too large so I am not able to provide you with the text....
The text is too large so I am not able to provide you with the text. That is why I have provided a direct link for my assignment. Thank you https://www.researchgate.net/publication/271271253_Gut_microbiota_composition_correlates_with_changes_in_body_fat_content_due_to_weight_los I need to answer these questions on it. List TWO sources other than those listed in the periodicals, that you could refer to if you were to research the article’s topic further. (1 point) 8. Did you learn anything interesting (diet related) that may cause you to alter your...
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
how can i make this program in python? Captain Øks is on his way to conquer...
how can i make this program in python? Captain Øks is on his way to conquer Mosefjell. He has a map of the area and he is in his camp at the designated place on the map. The map is divided into cells. For each cell, the height is given. Captain Øks is not happy, the landscape is full of hills, and he hates to climb both up and down. He has hired you to write a program that will...
Hello, How can I make the program print out "Invalid data!!" if the file has a...
Hello, How can I make the program print out "Invalid data!!" if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student grade. Smith Kelly 438975 98.6 A Johnson Gus 210498 72.4 C Reges...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
how can i make Activity duration estimates?
how can i make Activity duration estimates?
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT