2011-09-25 93 views
0

我想做一個不同的函數來爲Java應用程序製作菜單欄。如何在java中獲取JFrame作爲函數參數?

在菜單欄功能frame.pack()在動作監聽器和frame.setJMenuBar語句是必需的。

那麼我們如何將框架作爲對象傳遞給子類作爲參數呢?不使用框架作爲參數 我獲得可以用於

imports.. 

public class sjava { 
    public static void CreateAndRunGUI() { 
    final JFrame frame = new JFrame("MyFrame"); 

    code.. 

    MakeMenuBar(frame); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    public static void MakeMenuBar(Object frame) { 
    JMenuBar menubar = new JMenuBar(); 
    menubar.setBackground(new Color(180,160,130)); 
    menubar.setOpaque(true); 
    menubar.setPreferredSize(new Dimension(800,20)); 
    frame.setJMenuBar(menubar); 
    JMenu menu1 = new JMenu("menu1"); 

    code.. 

    mitem1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent a) { 
     code.. 
     SwingUtilities.updateComponentTreeUI(frame); 
     frame.pack(); 
     } 
    }); 
    code.. 
    mitem2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent a) { 
     code.. 
     SwingUtilities.updateComponentTreeUI(frame); 
     frame.pack(); 
     } 
    }); 
} 
public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     CreateAndRunGUI(); 
    } 
    }); 
} 

makemenubar功能錯誤?

+0

**你什麼錯誤** – SLaks

+0

找不到符號符號:方法setJMenuBar(javax.swing.JMenuBar中的)位置:可變幀 –

+0

更換'對象frame'用'JFrame的frame'在MakeMenuBar的'參數列表'。 – Howard

回答

1

你有

public static void MakeMenuBar(JFrame frame) {...} 

否則更換

public static void MakeMenuBar(Object frame) {...} 

沒有JFrame類的方法將可以訪問。

相關問題