0

我正在为税务应用程序制作设置向导,但我遇到了不知道如何解决的错误。请帮忙!我的错误是:

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,请让他们回答这个问题!这是我的代码:

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:只解决第一个错误!!!这也修复了其他人!

4

6 回答 6

5

尝试在声明中省略数组的大小并使用花括号而不是方括号。如:

Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
于 2013-07-08T17:52:12.350 回答
3

此行不正确必须用{}而不是换行[]

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 添加到容器中。

于 2013-07-08T17:55:25.603 回答
1

数组不是用方括号初始化的,而是用大括号初始化的,所以;

Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
于 2013-07-08T17:52:28.607 回答
1

数组初始值设定项使用{}代替[](对于文字部分),并且您不声明数组的大小,如:

Colors = new String[] {"black", "blue", "cyan", "darkGray", 
                         "gray",  "green", "lightGray", "magenta", 
                         "orange", "pink", "red", "white", "yellow"};
于 2013-07-08T17:52:29.447 回答
1
new String[12]["black", ...]

应该

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

甚至只是

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

您无需指定尺寸;这将是多余的,因为它只是您声明的元素数量。

于 2013-07-08T17:52:38.477 回答
1
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"};
于 2013-07-08T17:57:20.083 回答