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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Buttons in applets

Status
Not open for further replies.

Beracosta

Programmer
Oct 25, 2000
47
0
0
SE
Hi! I have a problem. I wanna have 2 buttons in my applet.

When u click on button 1 it should print a text in the applet.

When u click on button 2 a singel line should be painted in the applet.

I don't know how to get these buttons and how to register listeners for them. can anybody help me?

/Björn Helin
 
Something like this:

private JButton B1;
private JButton B2;

public void init()
{
// JApplet init with your code.

B1 = new JButton("Button 1");
B1.addActionListener(new ButtonActionListener()); // reg
B2 = new JButton("Button 2");
B2.addActionListener(new ButtonActionListener()); // reg

// more code you may want in the init.
}

// Inner Class for the Buttons.
class ButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == B1)
{
// your code here for Button 1
}
if (ev.getSource() == B2)
{
// your code here for Button 2
}
}
}

I'll leave the text part and drawing line part to you. Hint: Use the Graphics class in your JApplet paint method.
Use the Sun API Docs which you can download for free. Consider buying a begginers Java book or take a java class.

Good luck,
Brian
 
Thanx. I already knew how to deklare the buttons but how will i make em show in the applet? pls help!

/Björn Helin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top