0

所以我正在做这个项目来显示某些字体。问题是原始代码有效但是当我尝试使用扫描仪时它没有

原始代码

 public static void main(String[]args)
{
font("Times New Roman");
}

public static void font(String x){
World canvas = new World();//Creates a new world called canvas
Graphics picGraphics = canvas.getGraphics();//creates picGraphics which gets used in the world
picGraphics.setColor(Color.MAGENTA);//Color is set to Magenta
Font font = new Font(x,Font.BOLD,80);
picGraphics.setFont(font);//sets the font
//drawString parameters tell the string that is written and the coordinates to place the word
picGraphics.drawString("Font Tester",105,255);
canvas.repaint();
}

现在使用扫描仪的代码

public static void font2(){
World canvas = new World();//Creates a new world called canvas
Graphics picGraphics = canvas.getGraphics();//creates picGraphics which gets used in the world
picGraphics.setColor(Color.MAGENTA);//Color is set to Magenta
Scanner keyboard=new Scanner(System.in);
String x = keyboard.next();
Font font = new Font(x,Font.BOLD,80);
picGraphics.setFont(font);//sets the font
//drawString parameters tell the string that is written and the coordinates to place the word
picGraphics.drawString("Font Tester",105,255);
canvas.repaint();
}

正如您在该代码中看到的那样,我有一个扫描仪,以便用户可以直接将他们想要的字体输入到程序中以显示。但是,每当我将新代码与 Time New Roman 之类的示例一起使用时,窗口中显示的字体甚至不是 Times new Roman,而是更像 Arial... t 给出任何不同的结果

帮助?

BTW:已经添加了很多预设代码,例如 World canvas = new World(); 和其他位将有助于显示弹出的空白窗口,我可以在其中显示字体

谢谢

4

1 回答 1

1

Scanner.next方法仅返回您输入的第一个单词,即Times,并且系统找不到具有该名称的字体。

您可以使用该nextLine方法读取单词之间带有空格的整行:

String x = keyboard.nextLine();
于 2013-04-26T14:05:14.690 回答