Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

swing - Why the same font looks different in Java app run from Netbeans and run from Jar?

In my Java Swing app I have some JTextAreas, I realize the text looks different when it's run from Netbeans and when the app is run from a Jar file, why ? How to make them look the same ?

JTextArea Client_Side_TextArea=new JTextArea(),Network_TextArea=new JTextArea();

setLayout(new BorderLayout());

Client_Side_TextArea.setFont(new Font("Monospaced",0,15));
Client_Side_TextArea.setForeground(new Color(0,28,218));
Client_Side_TextArea.setPreferredSize(new Dimension(290,300));
Client_Side_TextArea.append("           Client Side
================================
");
add("West",Client_Side_TextArea);

Network_TextArea.setFont(new Font("Monospaced",0,15));
Network_TextArea.setBackground(new Color(226,226,226));
Network_TextArea.setForeground(new Color(0,28,218));
Network_TextArea.setPreferredSize(new Dimension(270,300));
Network_TextArea.append("            Network Connection
 =========================================
");
add("Center",Network_TextArea);

In the following image, the upper part is from the app run with Netbeans, the lower part is how it looks when run from a Jar file :

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the expected behavior: as discussed in the Font API, each supported platform may map a different physical font to a particulate logical font such as Font.MONOSPACED. Each look & feel may further refine the choice of font for a particular purpose. Unless the platforms, versions and settings are identical, the fonts may vary. A complete example and more on the mapping may be found here.

In addition, for the reasons cited here, don't use setPreferredSize(). If you choose to override getPreferredSize(), be certain not to fall into this trap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...