开发者

How to get JFrame as function argument in java?

开发者 https://www.devze.com 2023-04-07 03:34 出处:网络
I wanted to make a different function to make menubar for java application. In menubar function frame.pack() in action listeners and frame.setJMenuBar statements are required.

I wanted to make a different function to make menubar for java application.

In menubar function frame.pack() in action listeners and frame.setJMenuBar statements are required.

So how can we pass frame as object to subclass as argument? I get errors in

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.updateComponent开发者_StackOverflow中文版TreeUI(frame);
        frame.pack();
      }
    });
}
public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      CreateAndRunGUI();
    }
  });
}

Can makemenubar function be used without using frame as an argument?


You'll have to replace

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

with

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

otherwise no methods of class JFrame will be accessible.

0

精彩评论

暂无评论...
验证码 换一张
取 消