Question

In: Computer Science

Code in Java Change the instructionLabel to ask the user to enter a numeric value into...

Code in Java

Change the instructionLabel to ask the user to enter a numeric value into the textField and click the button to convert the entered value from kilometers to miles (1.609344 km = 1 mile).

When the actionButton on the form is clicked, ActionWindow should take the value entered in the textField, convert it from kilometers to miles, and display the result in the resultField.

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ActionWindow extends JFrame {

   JLabel label;
   JButton button;
   JFrame frame;
   JTextField text;

   public ActionWindow() {
       label = new JLabel();
       button = new JButton();
       frame = new JFrame();
       text = new JTextField();
   }

   public static void main(String[] args) {
       WindowRunnable myWindow = new WindowRunnable();
       javax.swing.SwingUtilities.invokeLater(myWindow);
   }


   public static void createAndShowGUI() {
       ActionWindow frame = new ActionWindow();
       frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
       frame.addComponentsToPane(frame.getContentPane());
       frame.pack();
       frame.setVisible(true);
   }

   public void addComponentsToPane(Container pane) {
       JPanel panel = new JPanel();
       panel.add(label);

       pane.add(panel);
      
       JLabel instructionLabel = new JLabel("");
       panel.add(instructionLabel);
      
       JTextField textField = new JTextField("");
       panel.add(textField);
      
       JButton actionButton = new JButton("");
       panel.add(actionButton);
      
       JLabel resultField = new JLabel("");
       panel.add(resultField);

   }
}

class WindowRunnable implements Runnable {
   public void run() {
       ActionWindow.createAndShowGUI();
   }
}

Solutions

Expert Solution

//Create class ActionWindow.java


import java.awt.Container;
import java.awt.Dimension;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ActionWindow extends JFrame {

JLabel label;
JButton button;
JFrame frame;
JTextField text;

public ActionWindow() {
label = new JLabel();
button = new JButton();
frame = new JFrame();
text = new JTextField();
}

public static void main(String[] args) {
WindowRunnable myWindow = new WindowRunnable();
javax.swing.SwingUtilities.invokeLater(myWindow);
}

public static void createAndShowGUI() {
ActionWindow frame = new ActionWindow();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

public void addComponentsToPane(Container pane) {
JPanel panel = new JPanel();
  
panel.add(label);

pane.add(panel);
//change the label of instructionLabel
JLabel instructionLabel = new JLabel("Enter Kilometers to convert to miles(Integer)");
panel.add(instructionLabel);
//set size to textField to get input
JTextField textField = new JTextField(12);

panel.add(textField);//add textfield to panel

JButton actionButton = new JButton("Convert to Miles");
panel.add(actionButton);//add button to panel

JLabel resultField = new JLabel("");//resultLabel to displayy result
panel.add(resultField);//add result label to panel
  
actionButton.addActionListener(new ActionListener() {
//add action listener to button to calculate entered kilometers in miles
@Override
public void actionPerformed(ActionEvent arg0) {
//after clicking button should print on window
if(textField.getText().toString().equals(""))
{
JOptionPane.showMessageDialog(panel, "Please enter kiltometers to converts in miles");
}else
{
try
{
double kilometers=Double.parseDouble(textField.getText().toString());//get kilometers from user
double miles = kilometers * 0.621;//1.609344 km=1 mile, hence
resultField.setText(kilometers+" (km) = "+miles+" miles ");//display result to resulField
}
catch(NumberFormatException e)
{
//if kilometers is not in number format
JOptionPane.showMessageDialog(panel, "Please enter numbers only");
}
}
}
  
});
panel.setPreferredSize(new Dimension(300, 190));//set preferrable size to panel

}
}

class WindowRunnable implements Runnable {

public void run() {
ActionWindow.createAndShowGUI();//show window
}
}

___________________________________________________________________________________________

OUTPUT

____________________________________________________________________________________________


Related Solutions

Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT