Reputation:
I am writing a code for a progress bar and need to declare an anonymous inner class, but when I do, I get these:
Lab2Part2.java:25: error: illegal start of expression public void actionPerformed(ActionEvent e) ^ Lab2Part2.java:25: error: illegal start of expression public void actionPerformed(ActionEvent e) ^ Lab2Part2.java:25: error: ';' expected public void actionPerformed(ActionEvent e) ^ Lab2Part2.java:25: error: ';' expected public void actionPerformed(ActionEvent e)
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lab2Part2 extends JFrame implements ActionListener
{
private JFrame frame;
private JButton button;
private JPanel panel;
public Lab2Part2()
{
setLayout(new GridLayout());
frame = new JFrame();
button = new JButton("Let's start this show");
panel = new JPanel();
panel.add(button);
InnerProgress prog1 = new InnerProgress("Progress 1: ");
InnerProgress prog2 = new InnerProgress("Progress 2: ");
button.addActionListener(new ActionListener());
//this is where it throws errors
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button)
{
Thread th1 = new Thread(prog1);
Thread th2 = new Thread(prog2);
th1.start();
th2.start();
}
}
add(panel);
add(prog1);
add(prog2);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
}
public static void main(String [] args)
{
new Lab2Part2();
}
class InnerProgress extends JPanel implements Runnable
{
private String progress;
private JProgressBar bar;
public InnerProgress(String _progress)
{
progress = _progress;
add(bar);
}
public void run()
{
System.out.println("We are running: " + progress);
}
}
}
Upvotes: 0
Views: 336
Reputation: 7461
Look precisely in these lines:
button.addActionListener(new ActionListener());
//this is where it throws errors
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == button) {
Thread th1 = new Thread(prog1);
Thread th2 = new Thread(prog2);
th1.start();
th2.start();
}
}
What you do here is instantiation of an interface, which is prohibited, then creating the method in the body of the other method, which is also prohibited.
Of course, that's not what you meant to do.
Surely, you wanted to implement actionPerformer
inside this ActionListener
and this is done so:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == button) {
Thread th1 = new Thread(prog1);
Thread th2 = new Thread(prog2);
th1.start();
th2.start();
}
});
Upvotes: 1
Reputation: 9437
You have your code mixed up, and are ending up with methods inside methods.
button.addActionListener(new ActionListener());
//this is where it throws errors
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button)
{
Thread th1 = new Thread(prog1);
Thread th2 = new Thread(prog2);
th1.start();
th2.start();
}
}
That entire actionPerformed
method should actually be in an anonymous inner class, but it isn't.
button.addActionListener(new ActionListener(){
//this is where it throws errors
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button)
{
Thread th1 = new Thread(prog1);
Thread th2 = new Thread(prog2);
th1.start();
th2.start();
}
});
keeps your method in the anonymous implementation.
Upvotes: 1