测试设置向导出错

时间:2013-07-09 03:55:36

标签: java swing colors compiler-errors jlist

我想制作一个设置向导,询问你寡妇的背景颜色。另外,我想要一个保存IconImage类型变量的浏览按钮。我收到此代码的编译错误。请帮忙!这是开始代码:

import java.awt.color.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Setup {
     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");
        final String 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();
                    colorSelected = Colors[index];
                    }
                });
        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 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());
            }
            });
    }
}

错误是:

Setup.java:15: error: incompatible types
                final String Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
                                      ^
  required: String
  found:    String[]
Setup.java:16: error: no suitable constructor found for JList(String)
                final JList colors = new JList(Colors);
                                     ^
    constructor JList.JList() is not applicable
      (actual and formal argument lists differ in length)
    constructor JList.JList(Vector) is not applicable
      (actual argument String cannot be converted to Vector by method invocation conversion)
    constructor JList.JList(Object[]) is not applicable
      (actual argument String cannot be converted to Object[] by method invocation conversion)
    constructor JList.JList(ListModel) is not applicable
      (actual argument String cannot be converted to ListModel by method invocation conversion)
Setup.java:23: error: array required, but String found
                        colorSelected = Colors[index];
                                              ^
3 errors

任何帮助/新代码表示赞赏! 谢谢 顺便说一句,JList可以使用字符串数组。我不明白!请帮忙!

2 个答案:

答案 0 :(得分:2)

您可以尝试使用此代码,每次单击“颜色”时它都会侦听并将颜色保存为“colorSelected”。你可能需要稍微调整它,因为它被调用了两次,因为点击是两个事件。无论如何,希望它能满足你的要求:

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Setup {

    private static String colorSelected;

    public static void main(String[] args) {
        JFrame f = new JFrame("Test Setup wizard");
        JLabel question1 = new JLabel("What would you like the background color to be?");
        final String 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);
        colors.setVisibleRowCount(-5);
        colors.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                int index = e.getFirstIndex();
                colorSelected = Colors[index];
                System.out.println(colorSelected);
            }
        });

        JScrollPane listScroller = new JScrollPane(colors);
        f.add(colors);
        f.setSize(500, 500);
        f.setVisible(true);
    }
}

另外,我认为大多数人都对“色彩”是一个枚举感到困惑,因为它是大写的。颜色只是一个String数组。惯例是camelCase对象名称,因此“colors”将是一个更好的名称,你可能应该将你的JList“colors”重命名为“jlist”。

答案 1 :(得分:1)

我认为Color不是一个枚举;它有一堆静态变量,但它没有被定义为枚举。

话虽如此,为给定的类添加更多值通常是个坏主意。最好的选择是实例化Color变量,然后为其分配现有颜色之一或使用Color(int r, int g, int b)构造函数,如下所示:

Color purple = new Color((139, 0, 139);
guiObject.setBackground(purple);

如果您必须采用Color.//my variable格式,只需创建一个新课程,比如public class CustomColor extends Color,然后调整您心中的内容。