How do I code this? I know the ActionListeners are way wrong but I don’t know how to do them. Please Help.?
Question by AngelEyes7780: How do I code this? I know the ActionListeners are way wrong but I don’t know how to do them. Please Help.?
//import utilities to use in the program
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
//name program
public class mortgageCalculator {
//Create Window and begin main method
static JFrame aWindow = new JFrame(“Week 1 Mortgage Calculator”);
public static void main(String[] args) {
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
// Set the position to screen center & size to half screen size
aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
//Create Buttons
JButton button1 = new JButton(“Reset”);
JButton button2 = new JButton(“Done”);
aWindow.add(button1);
aWindow.add(button2);
public class DoneButton implements ActionListener {
button2.addActionListener(DoneButton);
public void actionPerformed (system.exit){
}
public class ResetButton implements ActionListener {
button1.addActionListener(ResetButton);
public void actionPerformed (mortgageCalculator){
}
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.setVisible(true); // Display the window
FlowLayout flow = new FlowLayout(); // Create a layout manager
Container content = aWindow.getContentPane(); // Get the content pane
content.setLayout(flow); // Set the container layout mgr
//Create Pop-ups for user input
String Input1 = JOptionPane.showInputDialog(“Enter the Total Amount of the Mortgae”);
String Input2 = JOptionPane.showInputDialog(“Enter the Term in Years”);
String Input3 = JOptionPane.showInputDialog(“Enter the Annual Percentage Rate…without the percent sign”);
//Convert responses to integers
int Amount = Integer.parseInt(Input1);
int Term = Integer.parseInt(Input2);
double Rate = Double.parseDouble(Input3);
//Define Mathmatical Variables
double RateCalculation = ((Rate/12) /100);
double TermCalculation = Term * 12;
double PaymentCalculation = Amount * (RateCalculation/(1-Math.pow(1+ RateCalculation, -TermCalculation)));;
//Output the answer
JOptionPane.showMessageDialog(null, “The Monthly Payment is” + PaymentCalculation, “Montly Payment”, JOptionPane.PLAIN_MESSAGE);
}
}
}
Best answer:
Answer by Vaibhav
It should be
button2.addActionListener(new DoneButton() );
button1.addActionListener(new ResetButton() );
now at the end of your code
class DoneButton implements ActionListener
{
public void actionPerformed( ActionEvent ae)
{
System.exit();
}
}
class ResetButton implements ActionListener
{
public void actionPerformed( ActionEvent a)
{
new mortageCalculator();
}
Give your answer to this question below!
Tags: ActionListeners, code, Don't, help, know, Please, them, this, wrongRelated Posts
Tagged with: ActionListeners • code • Don't • help • know • Please • them • this • wrong
Filed under: NET Pay Calculator Answers
Like this post? Subscribe to my RSS feed and get loads more!
here is how you can do it …
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg)
{
text1.setValue= “” …blah blah.
}
} );
This becomes an Anonymous inner class which would do the action.
If you want to implement a class, then dont make it public. (There can be only 1 public class in 1 java file) So you can implement it as
class DoneReset implements ActionListener {
public void actionPerformed(ActionEvent arg)
{
text1.setValue= “” …blah blah.
}
}
and in the main () where you create the button just add
button2.addActionListener(new DoneReset() );
Action Events have an object on which they are performed. So you can use 1 class to do the actions for all types of Objects on which you want to the listening on …
Hope you got the concept and can take it forward.