ActionListener没有绘制,也没有调用函数

时间:2016-08-10 06:05:42

标签: java function draw

ActionListener内的代码public void actionPerformed(ActionEvent e)未回复绘制线的函数和代码。为什么我不能在ActionListener内画线?为什么我不能调用绘制线的函数。

MainClass.java

import javax.swing.SwingUtilities;

public final class MainClass {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CreateFrame();
                System.out.println("GUI created Successfully");
            }
        }); 
    }
}

//CreateFrame.java
import javax.swing.JFrame;

public class CreateFrame {
    CreateFrame(){
        createFrame();
    }
    public void createFrame(){
        JFrame frame = new JFrame("Drawing Lines");
        frame.setSize(400, 300);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //calling a class and adding in the frame
        frame.add(new CreateDrawings()); 
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

//CreateDrawings.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class CreateDrawings extends JPanel {
    private static final long serialVersionUID = 1L;
    JButton drawButton;
    JPanel panel;

    CreateDrawings() {
        setLayout(null);
        drawButton = new JButton("Draw");
        drawButton.setBounds(150, 220, 120, 30);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        add(drawButton);
        g.setColor(Color.blue);
        g.drawLine(10, 50, 200, 50);
        drawButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                g.setColor(Color.red);           //NOT WORKING
                g.drawLine(10, 100, 200, 100);   //NOT WORKING
                drawSomething(g);                //NOT WORKING
                JOptionPane.showMessageDialog(null, "This is called though!");
            }
        });
    }// PaintComponent

    private void drawSomething(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawLine(10, 150, 200, 150);
    }
}

2 个答案:

答案 0 :(得分:1)

因为 g 非最终变量,所以您无法在上面的匿名内部类中访问它。 我还没有找到任何官方答案。但是,您可以在StackOverflow.com上引用another question

我猜您使用的是Java 之前的 8,因此您的源代码无效。如果您尝试使用Java 8,它将起作用。原因是Java 8引入了关于有效最终的新概念。请参阅定义here

因此,如果您切换到Java 8, g 有效的最终变量。因此,您的源代码将能够编译。

答案 1 :(得分:0)

我感谢所有程序员的支持,包括我的教授。我终于能够通过激活ActionListener编写调用类和函数来创建GUI的代码。我做了一些改动来创建2D并在框架上制作一些彩色图画。 Output graphical image is here

//MainClass.java
    import javax.swing.SwingUtilities;

    public final class MainClass {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CreateFrame frame = new CreateFrame();
                    frame.createFrame();
                    System.out.println("GUI created Successfully");
                }
            });
        }
    }


//CreateFrame.java
    import javax.swing.JFrame;

    public class CreateFrame {
        public void createFrame() {
            JFrame frame = new JFrame("Drawing Lines");
            frame.setSize(400, 300);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // calling a class and adding in the frame
            // creating an object is a good practice, because this way JVM will
            // guarantee its behavior
            CreateDrawings drawings = new CreateDrawings();
            drawings.createDrawings();
            frame.add(drawings);

            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }


//CreateDrawings.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class CreateDrawings extends JPanel {
        private static final long serialVersionUID = 1L;

        private JButton drawButton, exitButton;
        private JLabel label;
        private DrawFromClass drawFromClass;

        public void createDrawings() {
            setLayout(null);
            drawButton = new JButton("Draw");
            exitButton = new JButton("Exit");
            label = new JLabel("Draw Random Colored Lines");
            drawButton.setBounds(50, 220, 120, 30);
            exitButton.setBounds(230, 220, 120, 30);
            label.setBounds(210, 180, 170, 30);
            add(drawButton);
            add(exitButton);
            add(label);
            drawFromClass = new DrawFromClass();
            drawButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // repaint() creates an event that dispatch thread of the Swing
                    // to invoke paintCompnent method (invokes update and paint too)
                    repaint();
                }
            });

            exitButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }

        @Override
        public void paintComponent(final Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.drawLine(10, 100, 200, 100);
            g.setColor(Color.blue);
            g.drawLine(10, 50, 200, 50);

            // drawing a line from a method
            drawFromMethod(g);

            // drawing a random-colored 2D lines from a class
            drawFromClass.draw(g);

        }// PaintComponent

        // drawing a black line from this method
        public void drawFromMethod(final Graphics g) {
            g.setColor(Color.black);
            g.drawLine(10, 150, 200, 150);
        }
    }

// DrawFromClass.java
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.util.Random;

    // this class draws three 5px thick random colored lines
    public class DrawFromClass {
        public void draw(final Graphics g) {
            Graphics2D gg = (Graphics2D) g;
            // draw lines with 5 pixel thick
            gg.setStroke(new BasicStroke(5));

            gg.setColor(getColor());
            gg.drawLine(270, 20, 270, 170);
            gg.setColor(getColor());
            gg.drawLine(300, 20, 300, 170);
            gg.setColor(getColor());
            gg.drawLine(330, 20, 330, 170);
        }

        // creates a random RGB color
        public Color getColor() {
            int[] colors = new int[3];
            Random random = new Random();
            colors[0] = random.nextInt(255);
            colors[1] = random.nextInt(255);
            colors[2] = random.nextInt(255);
            Color RGB = new Color(colors[0], colors[1], colors[2]);
            return RGB;
        }
    }