Java – why does a program compile for me, but not for another person?

My code is as follows. It's good for me, but my professor said he was receiving an error because few variables in my class were declared final Eclipse doesn't seem to have this problem on my machine, so I don't know how to solve the errors I can't see

I understand that some variables need to work in nested classes, but the variables I created seem to work normally, but they don't work in the end

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class JColorFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokelater(new Runnable() {
            public void run() {
                try {
                    JColorFrame frame = new JColorFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public JColorFrame() {
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,522,339);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5));
        contentPane.setLayout(new BorderLayout(0,0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel,BorderLayout.NORTH);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1,BorderLayout.WEST);

        JPanel panel_2 = new JPanel();
        contentPane.add(panel_2,BorderLayout.EAST);

        JPanel panel_3 = new JPanel();
        contentPane.add(panel_3,BorderLayout.soUTH);

        Color[] color = new Color[8];
        color[0] = Color.black;
        color[1] = Color.white;
        color[2] = Color.red;
        color[3] = Color.blue;
        color[4] = Color.green;
        color[5] = Color.yellow;
        color[6] = Color.magenta;
        color[7] = Color.orange;

        JButton btnChangeColor = new JButton("Change Color");
        btnChangeColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Random random = new Random();
                int random_1 = random.nextInt(4);
                switch (random_1) {
                    case 0:
                        int random_2 = random.nextInt(8);
                        panel.setBackground(color[random_2]);
                        break;
                    case 1:
                        random_2 = random.nextInt(8);
                        panel_1.setBackground(color[random_2]);
                        break;
                    case 2:
                        random_2 = random.nextInt(8);
                        panel_2.setBackground(color[random_2]);
                        break;
                    case 3:
                        random_2 = random.nextInt(8);
                        panel_3.setBackground(color[random_2]);
                        break;

                }

            }
        });

        contentPane.add(btnChangeColor,BorderLayout.CENTER);
    }
}

Solution

An inner class (such as actionlistener) cannot access non - final variables from the scope containing it In Java versions less than 8, if this happens, the compiler will automatically throw an error

In Java 8, if there is no change anywhere in the file, the compiler will simply set the variable to final because you are not here

For more information, see this post

Perhaps your professor uses Java 7 or earlier, so your code will not compile Just use any local variables in your inner class final

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>