Java字体使用项目错误

Java字体使用项目错误,java,fonts,Java,Fonts,所以我做这个项目是为了显示某些字体。 问题是原始代码可以工作 但当我尝试使用扫描仪时,它不会 原始代码 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.g

所以我做这个项目是为了显示某些字体。 问题是原始代码可以工作 但当我尝试使用扫描仪时,它不会

原始代码

 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();
}
正如你所看到的那样,我有一个扫描仪,用户可以直接将他们想要的字体输入到要显示的程序中。然而,每当我在诸如TimeNewRoman这样的示例中使用新代码时,窗口中显示的字体甚至不是TimesNewRoman,而是更像Arial。。。 我也尝试过将扫描仪放在主方法中,也不会给出任何不同的结果

帮忙

顺便说一句:已经添加了很多预设代码,例如World canvas=new World();还有其他一些功能将有助于显示弹出的空白窗口,在那里我可以显示字体


谢谢扫描仪。下一步
方法仅返回输入的第一个单词,即
,系统无法找到具有该名称的字体

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

String x = keyboard.nextLine();