【问题标题】:Taking user input for foreground color and applying it to JTextArea? Why is String foregroundcolor not working?将用户输入的前景色应用到 JTextArea?为什么字符串前景色不起作用?
【发布时间】:2016-01-31 22:25:14
【问题描述】:
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 前景应用到用户输入,这似乎不能正常工作,你想吗?

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

}

【问题讨论】:

    标签: java swing colors user-input


    【解决方案1】:

    您误解了Color.getColor(...) 方法。它需要一个字符串,但它是颜色 int 的字符串表示形式,而不是颜色字符串的字符串表示形式。根据颜色 API:

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

    考虑使用Map&lt;String, Color&gt; 来帮助您设置颜色。

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

    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。
    2. 现在什么都不应该使用小程序了。

    干净一点:

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

    【讨论】:

    • 非常感谢!非常有帮助,感谢您允许我尝试的不同选项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2016-05-22
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多