我已经从 eclipse 向我的 JAR 文件添加了一个字体,并试图在 JTextFields 中使用该字体。运行应用程序时没有绘制 setText,只是在 JTextField 中绘制了一个 -。我的代码基于Exporting font to jar in eclipse。
下面是我的代码:
//lets load the font
Font font = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("Coalition_v2.ttf"));
font.deriveFont(Font.PLAIN, 14);
txtBrain = new JTextField();
txtBrain.setFont(font);
txtBrain.setText("BRAIN");
txtBrain.setHorizontalAlignment(SwingConstants.CENTER);
txtBrain.setEditable(false);
txtBrain.setColumns(10);
txtBrain.setBounds(521, 727, 90, 40);
contentPane.add(txtBrain);
我知道字体在正确的位置,就好像我将 .getResourceAsStream 更改为 /Coalition_v2.ttf 然后我从控制台收到以下错误:
java.io.IOException: Problem reading font data.
at java.awt.Font.createFont(Font.java:946)
字体文件包含在 /src/
按要求输出 Jar -tvf:
0 Sun Mar 31 09:51:56 BST 2013 ngd/
0 Sun Mar 31 09:51:56 BST 2013 ngd/co/
0 Sun Mar 31 09:51:56 BST 2013 ngd/co/uk/
0 Sun Mar 31 09:51:56 BST 2013 ngd/co/uk/TabSync/
806 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About$1.class
556 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About$2.class
4762 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About.class
1802 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/Help.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$1.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$10.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$11.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$12.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$13.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$14.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$15.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$16.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$17.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$18.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$19.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$2.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$20.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$21.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$22.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$23.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$24.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$25.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$26.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$27.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$28.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$29.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$3.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$30.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$31.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$32.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$33.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$34.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$35.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$36.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$37.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$38.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$39.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$4.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$40.class
4765 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$41.class
723 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$42.class
1185 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$43.class
697 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$44.class
1220 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$45.class
995 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$46.class
781 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$47.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$5.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$6.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$7.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$8.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$9.class
2097 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$Field1Listener.class
47217 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main.class
1260 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/FileFiltererpdf.class
1260 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/FileFiltererjpg.class
991552 Wed Jul 27 19:13:00 BST 2011 ngd/co/uk/TabSync/Coalition_v2.ttf
991552 Wed Jul 27 19:13:00 BST 2011 Coalition_v2.ttf
409717 Thu Mar 21 14:15:32 GMT 2013 nullpdf.pdf
163009 Thu Mar 21 14:29:08 GMT 2013 Javasplash.jpg
代码从 Main.class 运行。
编辑:
代码现在看起来像这样:
Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/Coalition_v2.ttf")).deriveFont(Font.PLAIN, 14);
System.out.println(font)
排序。感谢您的帮助,也感谢 Andrew 引导您朝着正确的方向前进。