Jbutton that draws on a new Jpanel

i'd like this button to (on click) draw an Oval. Problem is that eclipse says something about missing semicolons (in the action listener definition) and i dont understand why. Whats the proper way of passing methods (bulid in or custom) to the action listeners?

public class figury implements ActionListener {

    public figury() {

        frame();
    }

    public void frame() {

        JFrame f = new JFrame();
        f.setVisible(true);
        f.setSize(480, 480);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel();
        JButton kolo = new JButton("Rysuj kolo");


        JButton kolo = new JButton("Rysuj kwadrat");
        kwadrat.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                public void paintComponent(Graphics g){
                    g.fillOval(50,50,100,100);
                    g.setColor(Color.RED);
                }
            }
        });

        p.add(kolo);
        f.add(p);
    }

    public static void main(String[] args) {

        new figury();

    }
}

Answers


You are trying to define a method inside another method there. In your case, the problem is in the line containing

public void paintComponent(Graphics g) {
...

This cannot be defined inside another method in java. There are good ideas for painting in java in these official documentation links and stackoverflow questions:

How to make canvas with Swing?

http://www.oracle.com/technetwork/java/painting-140037.html


If your figury class implements ActionListener, it should implement public void actionPerformed(ActionEvent e) method.

Also you are trying to declare a method paintComponent() inside another actionPerformed() method.

I think your code should look something like this:

public class figury implements ActionListener {

     public figury() {

         frame();
     }

     public void frame() {

         JFrame f = new JFrame();
         f.setVisible(true);
         f.setSize(480, 480);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         JPanel p = new JPanel();
         JButton kolo = new JButton("Rysuj kolo");


         JButton kwadrat = new JButton("Rysuj kwadrat");
         kwadrat.addActionListener(this);

         p.add(kolo);
         p.add(kwadrat);
         f.add(p);
     }

     @Override
     public void actionPerformed(ActionEvent e) {
         paintComponent(/*pass here a Graphics object*/);
     }

     public void paintComponent(Graphics g) {
         g.fillOval(50,50,100,100);
         g.setColor(Color.RED);
     }

     public static void main(String[] args) {
         new figury();
     }
}

Need Your Help

How can I get started with Emacs + fcsh + Flex3?

flex actionscript-3 emacs fcsh

I've got some reasonable emacs-fu, and I know how to use the Flash Builder IDE to work in Flex/Actionscript, but I'd like to move to using Emacs to do most of my coding instead of the flash builder.