?
Solved

Where to Add Code?

Posted on 2005-03-21
6
Medium Priority
?
236 Views
Last Modified: 2010-03-31
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
Comment
Question by:RenitlahHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 13596221
add a method named doCalculation

private void doCalculation()
{
   // calc here
}
0
 
LVL 7

Accepted Solution

by:
lhankins earned 1700 total points
ID: 13596229
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
 

Author Comment

by:RenitlahHelp
ID: 13596325
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RenitlahHelp
ID: 13596338
lhankins, please answer if you know the answer to my reply.

Thanks!
0
 
LVL 7

Expert Comment

by:lhankins
ID: 13596562
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
 

Author Comment

by:RenitlahHelp
ID: 13596645
Ok - makes sense. Thanks!
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question