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
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!
Learn more about our help with Assignments:
JavaJSPJSF