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 program and ask a user to enter a numeric value between 0 - 99....
Write a program and ask a user to enter a numeric value between 0 - 99. Your program will spell out the numbers into words. You must use at least three switch cases in addition to multiple if statements. Each missing switch statement will reduce your grade for this problem by 20%. Note: The total number of if statements and switch cases should not exceed 28. Additional if/case statement will further reduce your grade by 5%. in c++ please
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
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
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...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT