1

我有 39 个错误,但在同一件事上都是相同的错误(kindve),我不知道出了什么问题!错误都与此类似:

Setup.java:31: error: ')' expected
                                                Selected[0] = (0,0,0);
                                                                ^
Setup.java:31: error: not a statement
                                                Selected[0] = (0,0,0);
                                                                 ^
Setup.java:31: error: ';' expected
                                                Selected[0] = (0,0,0);
                                                                  ^

这真的很混乱,我不知道出了什么问题请帮助!这是我的代码:

import java.awt.color.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Setup {
    public static void main(String[] args) {
        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");
        String Colors[];
        Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
        JList colors = new JList(Colors);
        colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        colors.setLayoutOrientation(JList.VERTICAL);
        JScrollPane listScroller = new JScrollPane(colors);
        f.add(question1);
        f.add(colors);
        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 String selected = colors.getSelectedItem().toString();
                    final Color[] Selected = new Color[1];
                    if (selected == "black") {
                        Selected[0] = (0,0,0);
                    }
                    else if (selected == "blue") {
                        Selected[0] = (0,0,255);
                    }
                    else if (selected == "cyan") {
                        Selected[0] = (0,225,225);
                    }
                    else if (selected == "darkGray") {
                        Selected[0] = (169,169,169);
                    }
                    else if (selected == "gray") {
                        Selected[0] = (128,128,128);
                    }
                    else if (selected == "green") {
                        Selected[0] = (0,255,0);
                    }
                    else if (selected == "lightGray") {
                        Selected[0] = (211,211,211);
                    }
                    else if (selected == "magenta") {
                        Selected[0] = (255,0,255);
                    }
                    else if (selected == "orange") {
                        Selected[0] = (255,165,0);
                    }
                    else if (selected == "pink") {
                        Selected[0] = (255,20,147)
                    }
                    else if (selected == "red") {
                        Selected[0] = (255,0,0);
                    }
                    else if (selected == "white") {
                        Selected[0] = (255,255,255);
                    }
                    else if (selected == "yellow") {
                        Selected[0] = (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());
            }
            });
    }
}

任何帮助表示赞赏!谢谢!

注意:此帖子已移至: 此处。 谢谢您的合作

4

3 回答 3

3

我怀疑你想要类似的东西:

Selected[0] = new Color(0,0,0);

但是您也应该阅读这个问题,因为您目前正在使用==. 老实说,您最好填充 aMap<String, Color>而不是使用所有这些if语句。

您使用数组而不是简单的 type 变量也没有明显的原因Color

于 2013-07-09T17:02:20.890 回答
2

好吧,(0, 0, 0)它本身并不是一个声明。

我认为您的意思是创建一个新Color对象,例如

Selected[0] = new Color(0, 0, 0);

此外,不要使用运算符比较String值,该==运算符会比较对象引用以确定它们是否是同一个对象。使用中的equals方法String比较String值。

于 2013-07-09T17:01:43.850 回答
2

您缺少使用 RGB 值调用构造函数的 Color 类:

替换这个

Selected[0] = (0,0,0);

Selected[0] = new Color(0,0,0);
于 2013-07-09T17:02:04.170 回答