Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with homework please :) 1

Status
Not open for further replies.

darwin101

Programmer
Mar 7, 2001
156
US
hi
i have a small frame/applet to do and the book is no help.
would someone point me in the right direction?
"Write a java app that displays a list of at least 10 items in a store in a frame. Allow the user to choose any number of times from the list, display the items that the user chooses"
i can not figure out how to get the action listner in the panel to recognize each button seperatly
import java.awt.*;
import java.awt.event.*;
public class Store extends Panel implements ActionListener
{
Button aButton = new Button("Shirt");
Label aLabel = new Label(" Location Info ");
Button bButton = new Button("Pants");
Label bLabel = new Label(" Location Info ");
Button cButton = new Button("Socks");
Label cLabel = new Label(" Location Info ");

public Store()
{
setLayout(new GridLayout(3,0));
add(aButton);
aButton.addActionListener(this);
add(aLabel);

add(bButton);
bButton.addActionListener(this);
add(bLabel);

add(cButton);
cButton.addActionListener(this);
add(cLabel);
}
public void actionPerformed(ActionEvent e)
{
aLabel.setText("Blue Shirt");
bLabel.setText("Grey Pants");
cLabel.setText("Purple Socks");
}

}

thanks if you have the time to direct me :)
 
You just need to say something like

if(e.getSource.equals("Shirt")){
// do your stuff
}
else if(e.getSource.equals("Pants")){
//etc etc
}
else{
//blah blah blah
}

If getSource doesn't help then try e.getText()
 
Thanks Pipk
that got me going in the right direction.
here is what i changed it to
if(e.getSource() == aButton)
aLabel.setText("Blue Shirt ");
else if(e.getSource() == bButton)
bLabel.setText("Grey Pants ");
else if(e.getSource() == cButton)
cLabel.setText("Red Socks");
....ect
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top