2010-05-23 35 views
1

我有一個Java小程序,唯一可以正常工作的外觀是本機mac。我想使字體有點大,並嘗試使用標準的UIManager方法Java Mac OSX本機的外觀和感覺是否尊重UIManager字體更改?

UIManager.put(「Label.font」,new Font(「Georgia」,Font.PLAIN,18));

這不產生變化。當然,它不會拋出異常。

有誰知道本機mac外觀是否忽略了這些?

我知道有一些具體的方法可以在mac上製作不同大小的控件,但這些只能使它們變小。你不能使控制比常規更大。

回答

1

這似乎與任何安裝了大號& 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")); 
    } 
} 
+1

使用FontUIResource而不是Font可能會更安全。參見演示提供。 – camickr 2010-05-23 22:56:11

+0

我正在做一個JApplet,並將其設置在init中,然後不更改它。它適用於金屬外觀和感覺,但我通過創建一個金屬外觀和感覺的孩子類來實現。由於兩個人認爲這個工程即使與mac原生的外觀和感覺,我會再試一次。 謝謝 – 2010-05-24 00:22:43

+0

它適用於上面'JApplet'中的所有L&F,受到@camickr提出的觀點影響。 – trashgod 2010-05-24 01:32:40

1

的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