• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jButtons are driving me crazy

 
Tami Hart
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say you need to visit Sun's tutorial on Buttons.

 
Eddie Vanda
Ranch Hand
Posts: 281
  • 0
  • Mark post as helpful
  • send pies
  • 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
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how I might do it:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic