java内部类函数调用按钮监听器

时间:2017-03-13 03:57:45

标签: java user-interface jpanel drawing

所以我有一个按钮监听器,它在函数setPaintFlag()的范围外部,我需要调用它,但是java给了我运行时错误。另外我的绘图面板没有显示出来。谁能发现我的错误?该程序的目的是在随机位置绘制一个圆形或正方形,具有随机大小和颜色。程序编译并且窗口将显示,只有按钮可见,但它们不会触发。当我将鼠标悬停在setPaintFlag()上时,可见的错误是The method setPaintFlag(int) is undefined for the type PaintLab.buttonListener

按下按钮后出现更详细的错误消息

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintLab extends JPanel {
    private JButton circle, square;
    private JPanel drawingPanel;

    public PaintLab() {
        circle = new JButton("Draw Circle");
        circle.addActionListener(new ButtonListener());
        square = new JButton("Draw Square");
        square.addActionListener(new ButtonListener());
        JPanel buttonPanel = new JPanel();
        JPanel drawingPanel = new DrawingPanel(400, 400);
        add(drawingPanel);
        BoxLayout bX = new BoxLayout(buttonPanel, BoxLayout.X_AXIS);

        buttonPanel.setLayout(bX);
        buttonPanel.add(circle);
        buttonPanel.add(square);

        add(buttonPanel);

    }

    private class DrawingPanel extends JPanel { // outer
        private boolean paintFlag = false;
        private int theMode = -1;
        private int width, height;

        private DrawingPanel(int width, int height) { // inner
            this.width = width;
            this.height = height;
        }

        public void setPaintFlag(int current) { // inner
            paintFlag = true;
            theMode = current;
        }

        public void paintComponent(Graphics g) { // inner
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            int r = (int) (Math.random() * 255) + 1;
            int gn = (int) (Math.random() * 255) + 1;
            int b = (int) (Math.random() * 255) + 1;
            g2.setColor(new Color(r, gn, b));
            int w = (int) (Math.random() * width) + 1;
            int h = (int) (Math.random() * height) + 1;
            int x = (int) (Math.random() * width) + 1;
            int y = (int) (Math.random() * height) + 1;

            if (theMode == 0) {
                g2.drawOval(x, y, w, h);
                g2.fillOval(x, y, w, h);
            } else if (theMode == 1) {
                g2.drawRect(x, y, w, h);
                g2.fillRect(x, y, w, h);
            }
            g2.dispose();
        }

    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            if (event.getSource() == circle) {
                setPaintFlag(0);
                drawingPanel.repaint();
            } else if (event.getSource() == square) {
                setPaintFlag(1);
                drawingPanel.repaint();
            }
        }
    }

}

这是主要的。

    public class paintLabMain {

    public static void main(String[] args) {
        JFrame frame=new JFrame("Paint Lab");
        frame.setPreferredSize(new Dimension (300,600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new PaintLab());
        frame.pack();
        frame.setVisible(true);


    }

}

1 个答案:

答案 0 :(得分:3)

按钮不会触发,因为它们没有动作侦听器:

Circle = new JButton("Draw Circle")
Circle.addActionListener(new buttonListener())

错误消息是因为您致电setPaintFlag(int current)表单ButtonListener,但它在DrawingPanel中定义。

修改 请参阅注释(代码无法编译,因为我删除了部分内容并留下了骨架):

    public class PaintLab extends JPanel {
    private JButton circle, square;
    private DrawingPanel drawingPanel;//declare drawingPanel as field 

    public PaintLab() {
        circle = new JButton("Draw Circle");
        circle.addActionListener(new ButtonListener());
        square = new JButton("Draw Square");
        square.addActionListener(new ButtonListener());
        JPanel buttonPanel = new JPanel();
        /*JPanel already declared as field*/  
        drawingPanel = new DrawingPanel(400, 400);
        add(drawingPanel);
        BoxLayout bX = new BoxLayout(buttonPanel, BoxLayout.X_AXIS);

        buttonPanel.setLayout(bX);
        buttonPanel.add(circle);
        buttonPanel.add(square);

        add(buttonPanel);
    }

    private class DrawingPanel extends JPanel { // outer

            //code removed
        }

        public void paintComponent(Graphics g) { // inner
            //code removed     
        }

        //add setters 
        void setPaintFlag(boolean paintFlag) {
            this.paintFlag = paintFlag;
        }

        void setTheMode(int theMode) {
            this.theMode = theMode;
        }
    }

    class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            if (event.getSource() == circle) {
                drawingPanel.setPaintFlag(true);
                drawingPanel.repaint();
            } else if (event.getSource() == square) {
                drawingPanel.setPaintFlag(false);
                drawingPanel.repaint();
            }
        }
    }
}