的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);
}
}
使用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