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
功能錯誤?
**你什麼錯誤** – SLaks
找不到符號符號:方法setJMenuBar(javax.swing.JMenuBar中的)位置:可變幀 –
更換'對象frame'用'JFrame的frame'在MakeMenuBar的'參數列表'。 – Howard