There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop malware monitor motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound spyware trojan usb virus vista vista 32-bit windows windows xp windowsxp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Novis java student needs help


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
techforray's Avatar
Junior Member with 7 posts.
 
Join Date: Oct 2003
13-Dec-2004, 05:45 PM #1
Novis java student needs help
I am in a begining Java class and I cant figure something out. I am trying to add actionlistener to my program so that the buttons will do something. The reading is vague and I dont understand it very well. I have spent hours trying to figure this out. Can someone plese help. I have attached the code below.

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


public class MortgageDriver extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent event);{
}

// set up row 1
JPanel row1 = new JPanel();
ButtonGroup option = new ButtonGroup();
JLabel yearsLable = new JLabel("How many years of Mortgage: ", JLabel.RIGHT);
JButton byears7 = new JButton("7");
JButton byears15 = new JButton("15");
JButton byears30 = new JButton("30");

// set up row 2
JPanel row2 = new JPanel();
JLabel lyears7 = new JLabel(" The intrest rate for 7 year loan is: .??? ", JLabel.CENTER);
JLabel lyears15 = new JLabel(" The intrest rate for 15 year loan is: .??? ", JLabel.CENTER);
JLabel lyears30 = new JLabel(" The intrest rate for 30 year loan is: .??? ", JLabel.CENTER);

// set up row 3
JPanel row3 = new JPanel();
JLabel lamount = new JLabel("Amount of Loan: ", JLabel.RIGHT);
JTextField namount = new JTextField();

// set up row 4
JPanel row4 = new JPanel();
ButtonGroup option4 = new ButtonGroup();
JButton calcmortg = new JButton("Calculate Mortgage");

// set up row 5
JPanel row5 = new JPanel();
ButtonGroup option5 = new ButtonGroup();
JButton exit = new JButton("Exit");

public MortgageDriver() {
super("Mortgage Calculator");
setSize(750, 270);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(5, 1, 10, 10);
Container pane = getContentPane();
pane.setLayout(layout);

FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER,
10, 10);
option.add(byears7);
option.add(byears15);
option.add(byears30);
row1.setLayout(layout1);
row1.add(yearsLable);
row1.add(byears7);
row1.add(byears15);
row1.add(byears30);
pane.add(row1);

FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER,
10, 10);
row2.setLayout(layout2);
row2.add(lyears7);
row2.add(lyears15);
row2.add(lyears30);
pane.add(row2);

GridLayout layout3 = new GridLayout(0, 7);
row3.setLayout(layout3);
row3.add(lamount);
row3.add(namount);
pane.add(row3);

FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER,
10, 10);
row4.setLayout(layout4);
option4.add(calcmortg);
row4.add(calcmortg);
pane.add(row4);

FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER,
10, 10);
row5.setLayout(layout5);
option5.add(exit);
row5.add(exit);
pane.add(row5);



setContentPane(pane);
setVisible(true);
}
public static void main(String[] arguments) {
MortgageDriver md = new MortgageDriver();
}
}
Doggan's Avatar
Junior Member with 18 posts.
 
Join Date: Nov 2004
14-Dec-2004, 08:41 PM #2
You're not adding your buttons to the action listener. They don't know where to go to get their behavior. For example:

theButton = new JButton( "Some Name" );

theButton.addActionListener( this );

If you do that code in your MortgageDriver code, theButton will now look to that class for it's implementation. Now you need to implement something in the actionPerformed() method (get rid of that semi-colon). In that method, you'd do something like this:

if (event.getSource() == theButton ) {
// do something
}
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:37 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.