除非我将所有内容都放在一个类中,否则JMenuBar不会出现

时间:2018-09-10 01:54:33

标签: java swing jframe jmenubar

我正在尝试使用MenuBar制作框架,但无法显示它。我尝试使用pack(),因为在类似的问题中建议使用该方法,但这使我的框架不存在,最终我只看到上方的窗口栏。

如果我将所有内容放在一个类中,它将起作用;所以我认为我的问题是有关Java结构的基本知识。

这是我的JMenuBar类:

package jpaintnet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class JMenuBar extends JFrame implements MenuListener, ActionListener {

    JMenuBar mb;
    JMenu addImage, addObject, save;
    JMenuItem circle, rectangle, triangle, line;

    public JMenuBar() {

        mb = new JMenuBar();

        addObject = new JMenu("Add Object");
        mb.add(addObject);

        addImage = new JMenu("Add a new image");
        mb.add(addImage);

        save = new JMenu("save");
        mb.add(save);


        circle = new JMenuItem("Create a circle");
        circle.addActionListener(this);
        addObject.add(circle);

        rectangle = new JMenuItem("Create a rectangle");
        rectangle.addActionListener(this);
        addObject.add(rectangle);

        triangle = new JMenuItem("Create a triangle");
        triangle.addActionListener(this);
        addObject.add(triangle);

        line = new JMenuItem("Create a line");
        line.addActionListener(this);
        addObject.add(line);
    }



    @Override
    public void actionPerformed(ActionEvent e) {
       //here goes everything!
    }

    @Override
    public void menuSelected(MenuEvent e) {
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void menuCanceled(MenuEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

我的JFrame类:

package jpaintnet;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.HeadlessException;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class Frame extends JFrame
{

    public Frame(String title) throws HeadlessException {

        JMenuBar mb = new JMenuBar();
        JPanel panel = new JPanel();

        this.setBounds(100,100,640,480);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(Color.white);

        this.getContentPane().add(BorderLayout.SOUTH, panel);
        this.getContentPane().add(BorderLayout.NORTH, mb);

        this.setVisible(true);
    }
}

最后,我的主:

package jpaintnet;

public class Main {

    public static void main(String[] args) {

        Frame f = new Frame("JPaintNet");
        f.setVisible(true);
    }
}

我已经评论了一种setVisible()方法,但是为了以防万一,我最终将其再次放在此处。

谢谢。

2 个答案:

答案 0 :(得分:1)

您犯了一些错误。

  1. 在您的Frame类中,删除以下导入

    import javax.swing.JMenuBar;
    

    因为导入了swing.JMenuBar,jvm将忽略您创建的JMenuBar

  2. 您的JMenuBar类应该从javax.swing.JMenuBar而不是JFrame扩展。

    public class JMenuBar extends javax.swing.JMenuBar implements MenuListener, ActionListener
    
  3. 摆脱mb = new JMenuBar();构造函数中的JMenuBar语句。因为当您创建new JMenuBar()时,jvm要做的第一件事就是调用它的构造函数。在构造函数旁边创建另一个new JMenuBar()意味着您将继续创建许多JMenuBar,最终将导致StackOverFlow错误。

  4. 在您的mb类中删除变量JMenuBar,并替换为this

    this.add(addObject);
    this.add(addImage);
    this.add(save);
    

然后您的窗口应该可以正常工作。

enter image description here


更新:

MadProgrammer has suggested一样,请记住使类名唯一,尤其是在涉及Java开发的库时。另外,在创建数据库时,请避免将Java关键字用作表名或列名。如果将来要使用诸如休眠之类的框架,它将为您省去很多麻烦。

答案 1 :(得分:0)

@Andrew,已按照下面的说明更新了您的代码,以供您参考。请参阅内嵌评论:

这是您的MyMenuBar,即JMenuBar类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

/*
 * 1. Renamed your class "JMenuBar" to "MyMenuBar" - do not use Java class names. Its bad practice
 * 2. Extend from JMenuBar class to create Menu Bar. Whereas JFrame requires to create a frame
 */
public class MyMenuBar extends JMenuBar implements MenuListener, ActionListener { 
  JMenu addImage, addObject, save;
  JMenuItem circle, rectangle, triangle, line;

  public MyMenuBar() {


      addObject = new JMenu("Add Object");
      add(addObject);

      addImage = new JMenu("Add a new image");
      add(addImage);

      save = new JMenu("save");
      add(save);


      circle = new JMenuItem("Create a circle");
      circle.addActionListener(this);
      addObject.add(circle);

      rectangle = new JMenuItem("Create a rectangle");
      rectangle.addActionListener(this);
      addObject.add(rectangle);

      triangle = new JMenuItem("Create a triangle");
      triangle.addActionListener(this);
      addObject.add(triangle);

      line = new JMenuItem("Create a line");
      line.addActionListener(this);
      addObject.add(line);
  }



  @Override
  public void actionPerformed(ActionEvent e) {
     //here goes everything!
  }

  @Override
  public void menuSelected(MenuEvent e) {
  }

  @Override
  public void menuDeselected(MenuEvent e) {
      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

  @Override
  public void menuCanceled(MenuEvent e) {
      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }
}

这是您的MyFrame,即框架类:

    import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame
  extends
  JFrame {

  public MyFrame( String title ) {
    MyMenuBar mb = new MyMenuBar();
    JPanel panel = new JPanel();

    this.setBounds( 100, 100, 640, 480 );
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    this.setBackground( Color.white );

    this.getContentPane().add( BorderLayout.SOUTH, panel );
    // this.getContentPane().add( BorderLayout.NORTH, mb ); //Do not use this
    // method to set menu bar
    this.setJMenuBar( mb ); // this is ideal method to set menu bar

    this.setTitle( title ); // Missing as you have supplied title but missed to set it to frame
    this.setVisible( true );
  }

  // added main method here from your Main class
  public static void main( String[] args ) {

    MyFrame f = new MyFrame( "JPaintNet" );
    f.setVisible( true );
  }
}

将创建如下窗口:

enter image description here

我想这就是你想要的一切。