Java Mac OSX本机的外观和感觉是否尊重UIManager字体更改?
问题描述:
我有一个Java小程序,唯一可以正常工作的外观是本机mac。我想使字体有点大,并尝试使用标准的UIManager方法Java Mac OSX本机的外观和感觉是否尊重UIManager字体更改?
UIManager.put(“Label.font”,new Font(“Georgia”,Font.PLAIN,18));
这不产生变化。当然,它不会抛出异常。
有谁知道本机mac外观是否忽略了这些?
我知道有一些具体的方法可以在mac上制作不同大小的控件,但这些只能使它们变小。你不能使控制比常规更大。
答
这似乎与任何安装了大号& F.
编Mac OS X上的工作:如果你想启动后更改设置,请参阅How to Set the Look and FeelChanging the Look and Feel After Startup下。
public final class Laf {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));
f.add(new JLabel("Test"));
f.pack();
f.setVisible(true);
}
});
}
}
public final class LafApplet extends JApplet {
@Override
public void init() {
UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));
this.add(new JLabel("Test"));
}
}
答
的updateComponentTreeUI(...)方法(由trashgod提供的更改LAF后启动链接引用)将只在FontUIResource,而不是字体的工作。这只适用于启动后需要多次更改字体的情况。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
public class ChangeFont extends JFrame
{
private int size = 12;
private JComponent component;
public ChangeFont()
{
JTextArea textArea = new JTextArea();
textArea.append("updateComponentTreeUI will only work on a FontUIResource\n\n");
textArea.append("1) click the FontUIResource button as many times as you want\n");
textArea.append("2) after you click the Font button, neither button will work");
getContentPane().add(textArea, BorderLayout.NORTH);
JButton west = new JButton("FontUIResource");
west.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
update(new FontUIResource("monospaced", Font.PLAIN, size));
}
});
getContentPane().add(west, BorderLayout.WEST);
JButton east = new JButton("Font");
east.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
update(new Font("monospaced", Font.PLAIN, size));
}
});
getContentPane().add(east, BorderLayout.EAST);
component = new JTable(5, 5);
getContentPane().add(component, BorderLayout.SOUTH);
}
private void update(Font font)
{
UIManager.put("Table.font", font);
UIManager.put("TextArea.font", font);
SwingUtilities.updateComponentTreeUI(this);
size += 2;
pack();
}
public static void main(String[] args)
{
ChangeFont frame = new ChangeFont();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
+0
我不知道。好例子! – trashgod 2010-05-23 23:59:33
使用FontUIResource而不是Font可能会更安全。参见演示提供。 – camickr 2010-05-23 22:56:11
我正在做一个JApplet,并将其设置在init中,然后不更改它。它适用于金属外观和感觉,但我通过创建一个金属外观和感觉的孩子类来实现。由于两个人认为这个工程即使与mac原生的外观和感觉,我会再试一次。 谢谢 – 2010-05-24 00:22:43
它适用于上面'JApplet'中的所有L&F,受到@camickr提出的观点影响。 – trashgod 2010-05-24 01:32:40