gpt4 book ai didi

Java 字体使用项目错误

转载 作者:行者123 更新时间:2023-12-01 14:38:31 25 4
gpt4 key购买 nike

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

原始代码

 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...我还尝试将扫描仪放在主方法中,这也没有给出任何不同的结果

帮忙?

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

谢谢

最佳答案

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

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

String x = keyboard.nextLine();

关于Java 字体使用项目错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16238305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com