Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Where to Add Code?

Hi,

I have the following code and where do I add the calculation code?:

CALCULATION CODE:

calculate.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
           doCalculation();
      }
});


PROGRAM GUI CODE:

import java.text.NumberFormat;
import javax.swing.*;
import java. awt.event.*;
import java.awt.*;

class Mortgage extends JFrame // Gives the program a name
 {                // program starts here

public Mortgage() {
     super("Mortgage");
     setSize(600,250);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     Container pane= getContentPane();
     FlowLayout flo = new FlowLayout();
     pane.setLayout(flo);
     JPanel mortgageRow = new JPanel();
     JLabel mortgageLabel = new JLabel("Mortgage Amount: ");
     JTextField mortgage = new JTextField(5);
     JLabel monthLabel = new JLabel("Months of loan: ");
     JTextField month = new JTextField(5);
     JLabel rateLabel = new JLabel("Percentage rate: ");
     JTextField rate = new JTextField(5);
     JButton calculate = new JButton("Calculate");
     JButton reset = new JButton("Reset values");
     JButton quit = new JButton("Quit");
     JLabel resultsLabel = new JLabel("Mortgage Results: ");
     JTextField results = new JTextField(20);
     pane.add(mortgageRow);
     pane.add(mortgageLabel);
     pane.add(mortgage);
     pane.add(monthLabel);
     pane.add(month);
     pane.add(rateLabel);
     pane.add(rate);
     pane.add(calculate);
     pane.add(reset);
     pane.add(quit);
     pane.add(resultsLabel);
     pane.add(results);
     setContentPane(pane);
}
public static void main(String[]arguments){
Mortgage mg = new Mortgage();
}
}

0
RenitlahHelp
Asked:
RenitlahHelp
  • 3
  • 2
2 Solutions
 
objectsCommented:
add a method named doCalculation

private void doCalculation()
{
   // calc here
}
0
 
lhankinsCommented:
class Mortgage extends JFrame // Gives the program a name
 {                // program starts here

public Mortgage() {
     super("Mortgage");
     setSize(600,250);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     Container pane= getContentPane();
     FlowLayout flo = new FlowLayout();
     pane.setLayout(flo);
     JPanel mortgageRow = new JPanel();
     JLabel mortgageLabel = new JLabel("Mortgage Amount: ");
     JTextField mortgage = new JTextField(5);
     JLabel monthLabel = new JLabel("Months of loan: ");
     JTextField month = new JTextField(5);
     JLabel rateLabel = new JLabel("Percentage rate: ");
     JTextField rate = new JTextField(5);
     JButton calculate = new JButton("Calculate");

//--- wire the calculate button to call your doCalculation Method (shown below)
calculate.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
           doCalculation();
      }
});

     JButton reset = new JButton("Reset values");
     JButton quit = new JButton("Quit");
     JLabel resultsLabel = new JLabel("Mortgage Results: ");
     JTextField results = new JTextField(20);
     pane.add(mortgageRow);
     pane.add(mortgageLabel);
     pane.add(mortgage);
     pane.add(monthLabel);
     pane.add(month);
     pane.add(rateLabel);
     pane.add(rate);
     pane.add(calculate);
     pane.add(reset);
     pane.add(quit);
     pane.add(resultsLabel);
     pane.add(results);
     setContentPane(pane);
}


public void doCalculation()
{
   // put your calculation logic here...
}


public static void main(String[]arguments){
Mortgage mg = new Mortgage();
}
}
0
 
RenitlahHelpAuthor Commented:
Hi objects,

So the code in the:

public void doCalculation()
{
   // put your calculation logic here...
}

.... would be the code connecting the button to the doCalculation?  How do I run this from the Command Prompt to test?

Thanks!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
RenitlahHelpAuthor Commented:
lhankins, please answer if you know the answer to my reply.

Thanks!
0
 
lhankinsCommented:
This is a member function on the Mortgage class where you will put your business logic :

   public void doCalculation()
   {
      // put your calculation logic here...
   }

This is where you wire the calculate button to your business logic

   //--- wire the calculate button to call your doCalculation Method (shown below)
   calculate.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              doCalculation();
         }
   });
0
 
RenitlahHelpAuthor Commented:
Ok - makes sense. Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now