【发布时间】: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