點我執行範例程式

製做 java look and feel 選單,首先要先取得系統支援的 look and feel 項目,可由 UIManager.getInstalledLookAndFeels() 來取得目前可用的 look and feel。接著製做 JRadioButtonMenuItem,並內嵌一個 actionPerformed 來變更外觀。變更新的 look and feel 後,一定要執行 SwingUtilities.updateComponentTreeUI 來變更當前視窗內的所有元件。

  1. UIManager.getInstalledLookAndFeels()
    返回表示當前可用的 LookAndFeel 實作的 LookAndFeelInfo 陣列。
  2. UIManager.setLookAndFeel(newLookAndFeel)
    將當前外觀設置為 newLookAndFeel。
  3. SwingUtilities.updateComponentTreeUI()
    簡單的外觀更改:將階層樹結構中的每個節點轉到 updateUI() -- 也就是說,通過當前外觀初始化其 UI 屬性。
ButtonGroup lookAndFeelGroup = new ButtonGroup(); LookAndFeelInfo lookAndFeelInfo[] = UIManager.getInstalledLookAndFeels(); for(int i=0; i<lookAndFeelInfo.length; i++) { final String lookAndFeel = lookAndFeelInfo[i].getClassName(); JRadioButtonMenuItem rbmiLookAndFeel = new JRadioButtonMenuItem(lookAndFeelInfo[i].getName()); rbmiLookAndFeel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { UIManager.setLookAndFeel(lookAndFeel); SwingUtilities.updateComponentTreeUI(JOptionPane.getRootFrame()); } catch (Exception e) { System.out.println("lookandfeel set failed"); } } }); if (lookAndFeelInfo[i].getName() == UIManager.getLookAndFeel().getName()) rbmiLookAndFeel.setSelected(true); mnView.add(rbmiLookAndFeel); lookAndFeelGroup.add(rbmiLookAndFeel); }

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()