Not sure how to deal with addActionListener! :3
Code :
package appletPackage;
import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.*;
public class MainClass extends JApplet,Frame implements ActionListener {
private JButton button;
private String text = "Screenflashing";
Button btn;
public MainClass(){
}
public void init(){
button = new JButton(text);
button.addActionListener(this);
add(button);
btn = new Button("Regular button");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent e){
button.setText("BUAHAHAH.");
showStatus("actionPerformed...");
}
}
Umm, yeah... I'm trying to make two buttons with seperate actionPerformed functions so I can make them do separate things; how exactly would I do that? Thanks in advance!
Re: Not sure how to deal with addActionListener! :3
Instead of implementing ActionPerformed in that class, you want to create two separate classes that implement it instead. Those could be in separate class files, or as inner classes, or as anonymous inner classes.
Also, you could use one ActionListener, and check which JButton e.getSource() returns, then do different things depending on which one it was.
Re: Not sure how to deal with addActionListener! :3
Code :
package appletPackage;
import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.*;
public class MainClass extends JApplet,Frame implements ActionListener {
private JButton button;
private String text = "Screenflashing";
Button btn;
public MainClass(){
}
public void init(){
button = new JButton(text);
button.addActionListener(this);
add(button);
btn = new Button("Regular button");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent e){
if e.getSource() == button {
button.setText("BUAHAHAH.");
showStatus("actionPerformed...");
}
}
}
e.getSource returns an Object, so then exactly how do I correctly check with it? As you can see I tried checking of what I thought would be 2 objects, but...
Re: Not sure how to deal with addActionListener! :3
Disregard that last post, I got it; now, notice I'm 'addActionListener' to 2 buttons, but it's erroring on me? How do I add the addActionListener to multiple buttons without it erroring?
Re: Not sure how to deal with addActionListener! :3
Does this code compile?
public class MainClass extends JApplet,Frame implements ActionListener {
Quote:
but it's erroring on me?
Please copy and paste the full text of the error message here.