• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

jButtons are driving me crazy

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do I get my buttons to have functionality? the window is set up the way I want it. however, when the user clicks on the calculate button I want it to perform a mathmatical function and then display the results in the payment field.

Please Help

here is my program:

import javax.swing.*;
import java.awt.*;

class PosMortg1 extends JFrame {

PosMortg1() {
super("Calculate a Mortgage Payment?");
setSize(370, 270);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
pane.setLayout(flow);
JPanel row1 = new JPanel();
JLabel amountLabel = new JLabel("How much are you Borrowing?");
row1.add(amountLabel);
JTextField amount = new JTextField(14);
row1.add(amount);
pane.add(row1);
JPanel row2 = new JPanel();
JLabel percentLabel = new JLabel("Enter Percent Rate:");
row2.add(percentLabel);
JTextField percent = new JTextField(14);
row2.add(percent);
pane.add(row2);
JPanel row3 = new JPanel();
JLabel yearsLabel = new JLabel("How Many Years?");
row3.add(yearsLabel);
JTextField years = new JTextField(14);
row3.add(years);
pane.add(row3);
JPanel row4 = new JPanel();
JButton calculate = new JButton("Calculate");
row4.add(calculate);
pane.add(row4);
JPanel row5 = new JPanel();
JLabel paymentLabel = new JLabel("Monthly Payments");
row5.add(paymentLabel);
JTextArea payment = new JTextArea(4, 14);
payment.setLineWrap(true);
payment.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(payment,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row5.add(scroll);
pane.add(row5);
setContentPane(pane);
setVisible(true);
}

public static void main(String[] arguments) {
PosMortg1 mortgage = new PosMortg1();
}
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say you need to visit Sun's tutorial on Buttons.

 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not think you need the second last line of your constructor:

But it probably does not do any harm.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tami asked:


if I want to use the fields the user fills in How do I call them into the same math equation? I need to use all three in the same math equation.



In your event method you can simply call amount.getText() to get a String of the value entered in the textfield. Keep in mind though that you will then need to convert your String to an Integer to do calculations. Also note that the compile will probably complain that your variables like amount, percent, etc will need to be declared final.
[ July 03, 2004: Message edited by: Gregg Bolinger ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how I might do it:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic