获取前景色的用户输入并将其应用于JTextArea?为什么String前景色不起作用?

时间:2016-01-31 22:25:15

标签: java swing colors user-input

import java.applet.Applet;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;

public class font_chooser extends JApplet {

    public static void main(String[] args) {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Object[] possibilities = ge.getAvailableFontFamilyNames();
        Object[] colors = { "red", "yellow", "blue", "orange", "pink", "cyan", "magenta", "black", "white", "gray" };
        String font = (String) JOptionPane.showInputDialog(null, "Choose a Font", "Font Chooser",
                +JOptionPane.PLAIN_MESSAGE, null, possibilities, "");

我将变量foregroundcolor设置为用户输入

        String foregroundcolor = (String) JOptionPane.showInputDialog(null, "Chose Font Color", "Color",
                JOptionPane.PLAIN_MESSAGE, null, colors, "");

如果用户正确选择字体和颜色,我正在打印此pangram

        if ((font != null) && (font.length() > 0) && (colors != null)) {
            JTextArea textArea = new JTextArea("The quick brown fox jumped over the lazy dog’s back."
                    + "\n Pack my box with five dozen liquor jugs" + "\n Jackdaws love my big sphinx of quartz."
                    + "\n Mr. Jock, TV quiz PhD, bags few lynx." + "\n abcdefghijklmnopqrstuvwxyz"
                    + "\n ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n 01234567890"
                    + "\n €†™´¸¢©¤°÷½¼¾>¡¿«‘’<¯µ ·¬ªº¶±£\"»®§­¹²³ß×™¥" + "\n ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔ ÕÖØÙÚÛÜÝÞÿ"
                    + "\n àáâãäåæçèé êëìíîïðñòóô õöøùúûüýþÿ" + "\n !\"#$%&'()*+,-./:;<=>?@[\\^_z{|}~"
                    + "\n uvw wW gq9 2z 5s il17|!j oO08 `'\" ;:,. m nn rn {[()]}u");
            textArea.setFont(new Font((String) font, Font.ITALIC, 16));

我将textArea前景应用于userinput,这似乎没有正常工作,想法?

            textArea.setForeground(Color.getColor(foregroundcolor));
            JOptionPane.showMessageDialog(null, textArea);
            // JOptionPane.showMessageDialog(null, "You chose " + font);
        }
    }

}

1 个答案:

答案 0 :(得分:2)

您错误地解释了Color.getColor(...)方法。它需要一个String,但它是Color int的String表示形式,而不是Color String的String表示形式。根据Color API:

  

该参数被视为要获取的系统属性的名称。然后将此属性的字符串值解释为整数,然后将其转换为Color对象。

考虑使用Map<String, Color>来帮助您设置颜色。

例如(虽然丑陋,因为它使用并行数组:

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

public class TestColors {
    public static void main(String[] args) {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Object[] possibilities = ge.getAvailableFontFamilyNames();
        String[] colors = { "red", "yellow", "blue", "orange", "pink", "cyan", "magenta", "black", "white", "gray" };
        Color[] realColors = { Color.RED, Color.YELLOW, Color.BLUE, Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA,
                Color.BLACK, Color.WHITE, Color.GRAY };
        Map<String, Color> colorMap = new HashMap<>();
        for (int i = 0; i < colors.length; i++) {
            colorMap.put(colors[i], realColors[i]);
        }
        String font = (String) JOptionPane.showInputDialog(null, "Choose a Font", "Font Chooser",
                +JOptionPane.PLAIN_MESSAGE, null, possibilities, "");

        String foregroundcolor = (String) JOptionPane.showInputDialog(null, "Chose Font Color", "Color",
                JOptionPane.PLAIN_MESSAGE, null, colors, "");
        System.out.println(foregroundcolor);

        if ((font != null) && (font.length() > 0) && (colors != null)) {
            JTextArea textArea = new JTextArea("The quick brown fox jumped over the lazy dog’s back."
                    + "\n Pack my box with five dozen liquor jugs" + "\n Jackdaws love my big sphinx of quartz."
                    + "\n Mr. Jock, TV quiz PhD, bags few lynx." + "\n abcdefghijklmnopqrstuvwxyz"
                    + "\n ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n 01234567890"
                    + "\n €†™´¸¢©¤°÷½¼¾>¡¿«‘’<¯µ ·¬ªº¶±£\"»®§­¹²³ß×™¥" + "\n ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔ ÕÖØÙÚÛÜÝÞÿ"
                    + "\n àáâãäåæçèé êëìíîïðñòóô õöøùúûüýþÿ" + "\n !\"#$%&'()*+,-./:;<=>?@[\\^_z{|}~"
                    + "\n uvw wW gq9 2z 5s il17|!j oO08 `'\" ;:,. m nn rn {[()]}u");
            textArea.setFont(new Font((String) font, Font.ITALIC, 16));

            textArea.setForeground(colorMap.get(foregroundcolor));
            JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
            // JOptionPane.showMessageDialog(null, "You chose " + font);
        }
    }
}

请注意

  1. 如果不将JApplet用作applet,则不应扩展JApplet。
  2. 现在什么都不应该使用applet。
  3. 一点清洁:

    import java.awt.*;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import javax.swing.*;
    
    public class TestColors {
        public static void main(String[] args) {
    
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            Object[] possibilities = ge.getAvailableFontFamilyNames();
            Map<String, Color> myColorMap = new LinkedHashMap<>();
            myColorMap.put("Red", Color.RED);
            myColorMap.put("Orange", Color.ORANGE);
            myColorMap.put("Yellow", Color.YELLOW);
            myColorMap.put("Green", Color.GREEN);
            myColorMap.put("Blue", Color.BLUE);
            myColorMap.put("Magenta", Color.MAGENTA);
            myColorMap.put("Black", Color.BLACK);
            myColorMap.put("Gray", Color.GRAY);
    
            String[] myColors = myColorMap.keySet().toArray(new String[] {});
            String font = (String) JOptionPane.showInputDialog(null, "Choose a Font", "Font Chooser",
                    +JOptionPane.PLAIN_MESSAGE, null, possibilities, "");
    
            String foregroundcolor = (String) JOptionPane.showInputDialog(null, "Chose Font Color", "Color",
                    JOptionPane.PLAIN_MESSAGE, null, myColors, "");
    
            if ((font != null) && (font.length() > 0) && (foregroundcolor != null)) {
                JTextArea textArea = new JTextArea("The quick brown fox jumped over the lazy dog’s back."
                        + "\n Pack my box with five dozen liquor jugs" + "\n Jackdaws love my big sphinx of quartz."
                        + "\n Mr. Jock, TV quiz PhD, bags few lynx." + "\n abcdefghijklmnopqrstuvwxyz"
                        + "\n ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n 01234567890"
                        + "\n €†™´¸¢©¤°÷½¼¾>¡¿«‘’<¯µ ·¬ªº¶±£\"»®§­¹²³ß×™¥" + "\n ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔ ÕÖØÙÚÛÜÝÞÿ"
                        + "\n àáâãäåæçèé êëìíîïðñòóô õöøùúûüýþÿ" + "\n !\"#$%&'()*+,-./:;<=>?@[\\^_z{|}~"
                        + "\n uvw wW gq9 2z 5s il17|!j oO08 `'\" ;:,. m nn rn {[()]}u");
                textArea.setFont(new Font((String) font, Font.ITALIC, 16));
    
                textArea.setForeground(myColorMap.get(foregroundcolor));
                JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
            }
        }
    }