In: Computer Science
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);
}
}
// 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