对于设置坏的编译错误,向导无法找到符号(Bad compile errors for a setup

2019-10-18 08:57发布

我正在为一个纳税申报设置向导,我得到的是我不知道如何解决的错误。 请帮忙! 我的错误是:

Setup.java:16: error: cannot find symbol
                Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
                ^
  symbol:   variable Colors
  location: class Setup
Setup.java:17: error: cannot find symbol
                final JList colors = new JList(Colors);
                                               ^
  symbol:   variable Colors
  location: class Setup
Setup.java:24: error: cannot find symbol
                        Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
                        ^
  symbol: variable Colors
Setup.java:25: error: cannot find symbol

如果有人知道答案,并且使用的StackOverflow,请他们awnser这个问题! 这里是我的代码:

import java.awt.event.*;
import javax.swing.event.*;
public class Setup {
    @SuppressWarnings("unchecked")
    private static String colorSelected;
    public static void main(String[] args) {
        final JFrame f = new JFrame("Test Setup wizard");
        Container a = f.getContentPane();
        a.setBackground(Color.white);
        a.setLayout(new  FlowLayout());
        JLabel question1 = new JLabel("What would you like the background color to be?");
        JButton Next = new JButton("Next");
        Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
        final JList colors = new JList(Colors);
        colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        colors.setLayoutOrientation(JList.VERTICAL);
        JScrollPane listScroller = new JScrollPane(colors);
        colors.addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    int index = e.getFirstIndex();
                    Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
                    String colorSelected = Colors[index];
                    }
                });
        f.add(question1);
        f.add(listScroller);
        f.add(Next);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500,500);
        f.setVisible(true);
        final ImageIcon img = new ImageIcon("HardDisk.jpg");
        f.setIconImage(img.getImage());
        Next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent Ev) {
                    final Color[] Selected = new Color[1];
                    if (colorSelected .equals("black")) {
                        Selected[0] = new Color(0,0,0);
                    }
                    else if (colorSelected .equals("blue")) {
                        Selected[0] = new Color(0,0,255);
                    }
                    else if (colorSelected .equals("cyan")) {
                        Selected[0] = new Color(0,225,225);
                    }
                    else if (colorSelected .equals("darkGray")) {
                        Selected[0] = new Color(169,169,169);
                    }
                    else if (colorSelected .equals("gray")) {
                        Selected[0] = new Color(128,128,128);
                    }
                    else if (colorSelected .equals("green")) {
                        Selected[0] = new Color(0,255,0);
                    }
                    else if (colorSelected .equals("lightGray")) {
                        Selected[0] = new Color(211,211,211);
                    }
                    else if (colorSelected .equals("magenta")) {
                        Selected[0] = new Color(255,0,255);
                    }
                    else if (colorSelected .equals("orange")) {
                        Selected[0] = new Color(255,165,0);
                    }
                    else if (colorSelected .equals("pink")) {
                        Selected[0] = new Color(255,20,147);
                    }
                    else if (colorSelected .equals("red")) {
                        Selected[0] = new Color(255,0,0);
                    }
                    else if (colorSelected .equals("white")) {
                        Selected[0] = new Color(255,255,255);
                    }
                    else if (colorSelected .equals("yellow")) {
                        Selected[0] = new Color(255,255,0);
                    }
                f.dispose();
                JLabel complete = new JLabel("You are now complete.");
                JFrame f = new JFrame("Complete");
                Container a = f.getContentPane();
                a.setBackground(Selected[0]);
                f.add(complete);
                f.setSize(500,500);
                f.setVisible(true);
                f.setIconImage(img.getImage());
            }
            });
    }
}

任何帮助表示赞赏! PS:只有解决了第一个错误! 这修复了其他人也!

Answer 1:

尝试离开关中声明的阵列的大小,并使用大括号,而不是方括号。 如:

Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};


Answer 2:

这条线不正确必须用包裹{}而不是[]

new String[12]["black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"];

应该

new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};

而且你必须在JSrollPane添加到框架,而不是你的JList。

JScrollPane listScroller = new JScrollPane(colors);
f.add(listScroller);

而且还必须在添加JLabel到JFrame的contentPane中

JLabel question1 = new JLabel("What would you like the background color to be?");
f.add(question1);

作为@MadProgrammer总是建议我宁愿加入到容器作为一个JPanel,而不是JFrame的。



Answer 3:

new String[12]["black", ...]

应该

new String[]{"black", ...}

甚至只是

{"black", ...}  // no "new String[]"

你并不需要指定大小; 因为它只是你声明的元素数量将是多余的。



Answer 4:

new String[12] here is of Array is 12 but there are 13 elements in your array. 

也没必要说

 Colors = new String[12]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};

只要使用

 Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};


Answer 5:

数组使用方括号不会被初始化,它们被初始化用两个大括号 ,就这样;

Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};


Answer 6:

数组初始使用{}代替[]用于文本部分),并且不声明数组的大小,为:

Colors = new String[] {"black", "blue", "cyan", "darkGray", 
                         "gray",  "green", "lightGray", "magenta", 
                         "orange", "pink", "red", "white", "yellow"};


文章来源: Bad compile errors for a setup wizard-Cannot find symbol