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 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...
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)
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
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
I just finished this program where the program reads a text file of full names ,...
I just finished this program where the program reads a text file of full names , first and last name, and a zip code. It reads the items then stores the first name and last name and zipcodes as an objects and prints the items.   However, when i use my text file i get this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 " I notice this issue only happens when the names are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT