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"};

また、JList の代わりに JSrollPane をフレームに追加する必要があります。

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 が常に提案しているように、JFrame ではなく JPanel としてコンテナに追加することを好みます。

于 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 に答える