Answer to Question #58985 in Java | JSP | JSF for Jessica

Question #58985
private void calculate() {
double tfEarth =
Double.parseDouble(tfEarth.getText());
int weight = Integer.parseInt(tfMoon.getText());
double
// TODO - get tfEarth and convert to a double and then
// set each planet text field by calculating the corresponding weight
// using the supplied constants
I think I have the right start but I can't seem to figure out what to do next
1
Expert's answer
2016-04-08T08:55:05-0400
public class Main {
public static void main(String[] args) {
new MyFrame();
}
}





import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame{
private JTextField tfEarth = new JTextField();
private JTextField tfMoon = new JTextField();
private JLabel lEarth = new JLabel("Earth:");
private JLabel lMoon = new JLabel("Moon:");
private JLabel answer = new JLabel();
private JButton okButton = new JButton("Calculate");
private String calc = "Answer is ";

public MyFrame() throws HeadlessException {
setTitle("MyFrame");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
tfEarth.setColumns(10);
tfMoon.setColumns(10);

okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculate();
}
});

setLayout(new GridLayout(0, 2));
add(lEarth);
add(tfEarth);
add(lMoon);
add(tfMoon);
add(okButton);
add(answer);
pack();
setLocationRelativeTo(null);
setVisible(true);
}

private void calculate() {
if(!(tfEarth.getText().equals("") && tfMoon.getText().equals(""))) {
double earth = Double.parseDouble(tfEarth.getText());
int weight = Integer.parseInt(tfMoon.getText());
answer.setText(calc + (earth * weight));
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS