Java program. Please only answer if you understand how to do the problem. Thank
ID: 3716251 • Letter: J
Question
Java program. Please only answer if you understand how to do the problem. Thank you!
Write a Java program that converts miles and kilometer.
If you enter values in the Mile text field, the Kilometer text field is focused (or the cursor is blinking in the Kilometer text field), and press the ENTER key, the corresponding kilometer is displayed in the Kilometer text filed.
If you enter values in the Kilometer text field, the Mile text field is focused (or the cursor is blinking in the Mile text field), and press the ENTER key, the corresponding mile is displayed in the Mile text filed.
Sample runs of the program are shown below:
Exercise16 Mile Kilometer 1.60230732254446 Exercise165- Mile Kilometer 14.3543 23Explanation / Answer
package testswing.upload; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class SwingFirstExample implements KeyListener { JFrame frame = null; JPanel panel=null; JLabel mile=null; JTextField mileV=null; JTextField kmV=null; public SwingFirstExample(){ // Creating instance of JFrame frame = new JFrame("Layout Question"); // Setting the width and height of frame frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* Creating panel. This is same as a div tag in HTML * We can create several panels and add them to specific * positions in a JFrame. Inside panels we can add text * fields, buttons and other components. */ panel = new JPanel(); // adding panel to frame frame.add(panel); /* calling user defined method for adding components * to the panel. */ // Setting the frame visibility to true frame.setVisible(true); panel.setLayout(null); mile=new JLabel("Mile"); mile.setBounds(5,10,50,20); JLabel km=new JLabel("Kilometer"); km.setBounds(5,30,60,20); mileV=new JTextField(); mileV.setBounds(70,10,190,20); kmV=new JTextField(); kmV.setBounds(70,30,190,20); panel.add(kmV); panel.add(mile); panel.add(km); panel.add(mileV); Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { mileV.requestFocusInWindow(); mileV.setText((Double.parseDouble(kmV.getText())*0.621371)+""); } }; kmV.addActionListener(action); Action action2 = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { kmV.requestFocusInWindow(); kmV.setText((Double.parseDouble(mileV.getText())*1.60934)+""); } }; mileV.addActionListener(action2); mileV.addKeyListener(this); kmV.addKeyListener(this); frame.repaint(); } @Override public void keyTyped(KeyEvent e) { Object src = e.getSource(); //to check which button is clicked System.out.println(); if(src==kmV){ mileV.getCaret().setVisible(true); } if(src==mileV){ kmV.getCaret().setVisible(true); } } @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] s){ SwingFirstExample sw=new SwingFirstExample(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.