Homework help.

I want it so when I press the "Yes" buttons, the corresponding message and text fields appear. They are not showing up. The program compiles fine.

Here is my code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.DecimalFormat;

publicclass TravelExpensesextends JFrameimplements ActionListener{

private JPanel iPanel;// To hold a text field

private JPanel buttonPanel;// To hold a button

private JTextField days, airfare, carRental, milesDriven, parkingFees, taxiCharges, confrenceFees, lodgingCharges;

private JLabel message1, message2, message3, message4, message5, message6, message7, message8, message9, message10, message11, message12, message13, message14;

private JButton YesButton, YesButton2, YesButton3, YesButton4, YesButton5, YesButton6;

public TravelExpenses(){

setTitle("Travel Expense Calculator");

setSize(400, 300);

// Create the panel.

iPanel =new JPanel();

iPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

message1 =new JLabel("Number of days on the trip: ");

days =new JTextField(10);

message2 =new JLabel("Did you have any airfare?");

YesButton =new JButton("Yes");

YesButton.addActionListener(this);

message3 =new JLabel("Please enter the amount of airfare: ");

message3.setVisible(false);

airfare =new JTextField(10);

airfare.setVisible(false);

message4 =new JLabel("Did you have any car rental fees?");

YesButton2 =new JButton("Yes");

YesButton2.addActionListener(this);

message5 =new JLabel("Please enter the amount of car rental fees: ");

message5.setVisible(false);

carRental =new JTextField(10);

carRental.setVisible(false);

message6 =new JLabel("Did you use a private vehicle?");

YesButton3 =new JButton("Yes");

YesButton3.addActionListener(this);

message7 =new JLabel("Please enther the number of miles driven: ");

message7.setVisible(false);

milesDriven =new JTextField(10);

milesDriven.setVisible(false);

message8 =new JLabel("Did you have any parking fees?");

YesButton4 =new JButton("Yes");

YesButton4.addActionListener(this);

message9 =new JLabel("Please enter the amount of parking fees: ");

message9.setVisible(false);

parkingFees =new JTextField(10);

parkingFees.setVisible(false);

message10 =new JLabel("Did you have any taxi charges?");

YesButton5 =new JButton("Yes");

YesButton5.addActionListener(this);

message11 =new JLabel("Please enter the amount of taxi charges: ");

message11.setVisible(false);

taxiCharges =new JTextField(10);

taxiCharges.setVisible(false);

message12 =new JLabel("Did you have any conference or seminar registration fees?");

YesButton6 =new JButton("Yes");

YesButton6.addActionListener(this);

message13 =new JLabel("Please enter the amount of conference and/or seminar registration fees: ");

message13.setVisible(false);

confrenceFees =new JTextField(10);

confrenceFees.setVisible(false);

message14 =new JLabel("Please enter the amount of Lodging charges: ");

lodgingCharges =new JTextField(10);

iPanel.setLayout(new GridLayout(14, 2));

// Add the label and text field to the panel.

iPanel.add(message1);

iPanel.add(days);

iPanel.add(message2);

iPanel.add(YesButton);

iPanel.add(message3);

iPanel.add(airfare);

iPanel.add(message4);

iPanel.add(YesButton2);

iPanel.add(message5);

iPanel.add(carRental);

iPanel.add(message6);

iPanel.add(YesButton3);

iPanel.add(message7);

iPanel.add(milesDriven);

iPanel.add(message8);

iPanel.add(YesButton4);

iPanel.add(message9);

iPanel.add(parkingFees);

iPanel.add(message10);

iPanel.add(YesButton5);

iPanel.add(message11);

iPanel.add(taxiCharges);

iPanel.add(message12);

iPanel.add(YesButton6);

iPanel.add(message13);

iPanel.add(confrenceFees);

iPanel.add(message14);

iPanel.add(lodgingCharges);

setLayout(new BorderLayout());

// Add the panels to the content pane.

add(iPanel, BorderLayout.CENTER);

//add(buttonPanel, BorderLayout.SOUTH);

setVisible(true);

}

publicvoid actionPerformed(ActionEvent e){

if (e.equals(YesButton)){

message3.setVisible(true);

airfare.setVisible(true);

}elseif (e.equals(YesButton2)){

message5.setVisible(true);

carRental.setVisible(true);

}elseif (e.equals(YesButton3)){

message7.setVisible(true);

milesDriven.setVisible(true);

}elseif (e.equals(YesButton4)){

message9.setVisible(true);

parkingFees.setVisible(true);

}elseif (e.equals(YesButton5)){

message11.setVisible(true);

taxiCharges.setVisible(true);

}elseif (e.equals(YesButton6)){

message13.setVisible(true);

confrenceFees.setVisible(true);

}

}

publicstaticvoid main(String[] args)

{

TravelExpenses mc =new TravelExpenses();

}

}

[9609 byte] By [PoleVaulta] at [2007-11-27 2:55:25]
# 1

If you pay attention to your e.equals() behaviour you could notice that it doesn't compares the event with any button. So try using this:

if(e.getSource().equals(YesButton)) {

message3.setVisible(true);

airfare.setVisible(true);

}

nofearinca at 2007-7-12 3:32:09 > top of Java-index,Java Essentials,New To Java...
# 2
works... thank you.
PoleVaulta at 2007-7-12 3:32:10 > top of Java-index,Java Essentials,New To Java...