2017-06-12 77 views
1

我想通過JRadioButtonMenuItem更新我的外觀。而我在Stackoverflow中搜索,但我發現是1班的一大堆代碼。對於我來說,作爲初學者,它更容易在一個特殊的班級分開功能。 這是我的框架類。如何在課堂上更新外觀和感覺?

public class CalenderFrame extends JFrame { 

public CalenderFrame() throws HeadlessException { 
    createFrame(); 
} 
public void createFrame() { 
    setJMenuBar(CalenderMenuBar.getInstance().createMenu()); 

    setTitle("Calender"); 
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    setPreferredSize(new Dimension(400, 300)); 
    pack(); 
    setLocationRelativeTo(null); 
    setVisible(true); 
} 
} 

這就是我的MenueBar類。我只是簡單介紹一下這個問題的特定代碼。這個班是一個單身人士。

public JMenuBar createMenu() { 
JMenu lookAndFeelMenu = new JMenu("Look & Feel"); 
     JRadioButtonMenuItem lAndFWindowsItem = new JRadioButtonMenuItem("Windows",true); 
     lAndFWindowsItem.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == lAndFWindowsItem) { 
        lAndFAction(1); 
       } 
      } 
     }); 
     JRadioButtonMenuItem lAndFMetalItem = new JRadioButtonMenuItem("Metal",false); 
     lAndFMetalItem.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == lAndFMetalItem) { 
        lAndFAction(2); 
       } 
      } 
     }); 
     JRadioButtonMenuItem lAndFMotifItem = new JRadioButtonMenuItem("Motif", false); 
     lAndFMotifItem.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == lAndFMotifItem) { 
        lAndFAction(3); 
       } 
      } 
     }); 
     ButtonGroup group = new ButtonGroup(); 
     group.add(lAndFWindowsItem); 
     group.add(lAndFMetalItem); 
     group.add(lAndFMotifItem); 

     lookAndFeelMenu.add(lAndFWindowsItem); 
     lookAndFeelMenu.add(lAndFMetalItem); 
     lookAndFeelMenu.add(lAndFMotifItem); 
    } 

public void lAndFAction(int counter) { 
     try { 
     String plaf = ""; 
     if (counter == 1) { 
      plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 
     } else if (counter == 2) { 
      plaf = "javax.swing.plaf.metal.MetalLookAndFeel"; 
     } else if (counter == 3) { 
      plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; 
     } 

     UIManager.setLookAndFeel(plaf); 
     //SwingUtilities.updateComponentTreeUI(this); 


    } catch (UnsupportedLookAndFeelException ue) { 
     System.err.println(ue.toString()); 
    } catch (ClassNotFoundException ce) { 
     System.err.println(ce.toString()); 
    } catch (InstantiationException ie) { 
     System.err.println(ie.toString()); 
    } catch (IllegalAccessException iae) { 
     System.err.println(iae.toString()); 
    } 
    } 
} 

我希望你們能幫助我。

回答

2

我不確定你的問題實際上是什麼。但是,更改LaF後必須更新組件。按照Look and Feel Documentation

改變外觀和感覺啓動

你甚至可以在程序的GUI 後更改爲L & f控制setLookAndFeel是可見的。要使現有組件反映新的L & F, 將調用SwingUtilities的updateComponentTreeUI方法,每個 頂級容器。然後,您可能希望調整每個頂級容器的大小以反映其包含組件的新大小。對於 例如:

UIManager.setLookAndFeel(lnfName); 
SwingUtilities.updateComponentTreeUI(frame); 
frame.pack(); 

因此,你需要在框架保持的組件在UI的參考。一個想法是做這樣的事情:

public class CalendarMenuBar { 
    // Add this field to tour factory 
    private static JFrame frameThatWillBeUpdated; 

    // ... (Your code goes here) 

    // update this method to receive the reference of the frame which will 
    // need to be refreshed (update the GUI) 
    public JMenuBar createMenu(JFrame frame) { 
     // sets the reference for the frame 
     frameThatWillBeUpdated = frame; 

     // ... (the rest of your code for this method) 
    } 

    // ... 

    // Update this method to refresh the frame 
    public void lAndFAction(int counter) { 
     try{ 
      // ... (your code) 

      // Set the LaF 
      UIManager.setLookAndFeel(plaf); 
      // Update the component tree (frame and its children) 
      SwingUtilities.updateComponentTreeUI(frameThatWillBeUpdated); 
      // repack to resize 
      frame.pack(); 

     } catch(Exception ex){ 
      // Your catches 
     } 
    } 
} 

,這裏是當創建你的框架(您CalenderFrame類中),你如何使用它:

public void createFrame() { 
    // use this frame as reference 
    setJMenuBar(CalenderMenuBar.getInstance().createMenu(this)); 

    // ... (your code goes here) 
} 
+0

泰這麼多它的工作原理 –

+0

@JeredBlockDev如果這個答案解決你的問題,考慮[Accepting it](https://stackoverflow.com/help/someone-answers) –