简体   繁体   English

我需要在此类中添加构造函数的帮助,以便可以将菜单栏添加到主类中

[英]I need help adding a constructor to this class so i can add the menubar to my main class

this is the menubar class if this is put directly into the main class it works however in its own class it does not because there is no constructor and im having difficulty doing it i understand the importance of constructors but cant seem to get my head around how to add it to this class 这是菜单栏类,如果直接将其放入主类中,则可以工作,但是在其自己的类中,这不是因为没有构造函数,而且我很难做到这一点,我理解构造函数的重要性,但似乎无法理解将其添加到此类

public class MenuBar extends mainClass {

   JFrame frame = new JFrame("Assingment");
   JMenuBar menuBar;
   JMenu menu, submenu;
   JMenuItem menuItem;
   JPopupMenu popup;

   {
      menuBar = new JMenuBar();
      menu = new JMenu("File");
      menu.setMnemonic(KeyEvent.VK_A);
      menu.getAccessibleContext().setAccessibleDescription("File Menu");
      menuBar.add(menu);

      menuItem = new JMenuItem("Load");
      menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
      menu.add(menuItem);

      menuItem.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              JFileChooser fileChooser = new JFileChooser();
                  switch (fileChooser.showOpenDialog(frame)) {
                    case JFileChooser.APPROVE_OPTION:
                        break; 
                  }
               }
      });


      menuItem = new JMenuItem("Save", KeyEvent.VK_T);
      menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
      menuItem.getAccessibleContext().setAccessibleDescription("Save");
      menu.add(menuItem);

      menuItem = new JMenuItem("Exit",KeyEvent.VK_T);
      menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
      menuItem.getAccessibleContext().setAccessibleDescription("Exit");
      menuItem.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
               System.exit(0);
          }
      });

      menu.add(menuItem);
      menu = new JMenu("Help");
      menu.setMnemonic(KeyEvent.VK_N);
      menu.getAccessibleContext().setAccessibleDescription("Help Menu");
      menuBar.add(menu);

      frame.setJMenuBar(menuBar);
      frame.setVisible(true);
    }
} 

First class-- 头等舱

 @SuppressWarnings("serial")
    public class GraphicsPanel extends JPanel {

private final static Color BACKGROUND_COL = Color.DARK_GRAY;


private BufferedImage image;

public void drawLine(Color color, int x1, int y1, int x2, int y2) {

    Graphics g = image.getGraphics();

    g.setColor(color);

    g.drawLine(x1, y1, x2, y2);
}


public void clear() {

    Graphics g = image.getGraphics();

    g.setColor(BACKGROUND_COL);

    g.fillRect(0, 0, image.getWidth(),  image.getHeight());
}

@Override
public void paint(Graphics g) {

    // render the image on the panel.
    g.drawImage(image, 0, 0, null);
}





GraphicsPanel() {

    setPreferredSize(new Dimension(500, 300));

    image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);

    // Set max size of the panel, so that is matches the max size of the image.
    setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));

    clear();
}



  }

Mainclass-- 主类-

public class mainClass extends GraphicsPanel  {

public static void main(String[] args)
{
    JFrame frame = new JFrame ("Assingment");
   mainClass GraphPan = new mainClass();
   MenuBar mnuBar=new MenuBar();
   frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
   frame.setContentPane(GraphPan);
   frame.pack();
   frame.setVisible(true);
}


}
public class TestGui extends JFrame {
    public TestGui() {
        super();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(true);
        this.getContentPane().setLayout(new BorderLayout());
        createMenu(this.getContentPane());
        createPanel(this.getContentPane());
        this.pack();
        this.setPreferredSize(this.getSize());
        this.setLocationRelativeTo(null);

    }

private void createMenu(Container pane) {


    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Datei");
    menuBar.add(menu);
    JMenuItem menuItemClose = new JMenuItem("Schließen");
    JMenuItem menuItemEdit = new JMenuItem("Bearbeiten");
    JMenuItem menuItemUndo = new JMenuItem("Rückgängig");

    menuItemEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
    menuItemEdit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });
    menuItemClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
    menuItemClose.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    JMenuItem menuItemAdd = new JMenuItem("Einfügen");
    menuItemAdd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    menuItemAdd.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }

    });

    menuItemUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
    menuItemUndo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }

    });
    menu.add(menuItemAdd);
    menu.add(menuItemEdit);
    menu.add(menuItemUndo);
    menu.add(menuItemClose);
    menuBar.setMinimumSize(new Dimension(100, 21));


    pane.add(menuBar, BorderLayout.PAGE_START);
}
private void createPanel(Container contentPane) {
    JPanel g = new JPanel();
    g.setPreferredSize(new Dimension(500, 300));

    image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);

    // Set max size of the panel, so that is matches the max size of the
    // image.
    g.setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));

    clear();
    add(g);
}

private final static Color BACKGROUND_COL = Color.DARK_GRAY;

private BufferedImage image;

public void drawLine(Color color, int x1, int y1, int x2, int y2) {

    Graphics g = image.getGraphics();

    g.setColor(color);

    g.drawLine(x1, y1, x2, y2);
}

public void clear() {

    Graphics g = image.getGraphics();

    g.setColor(BACKGROUND_COL);

    g.fillRect(0, 0, image.getWidth(), image.getHeight());
}

@Override
public void paint(Graphics g) {

    // render the image on the panel.
    g.drawImage(image, 0, 0, null);
}

}

this code will create a window with a menubar in the top left corner and a dark grey jpanel. 此代码将创建一个窗口,在左上角具有一个菜单栏,并带有一个深灰色的jpanel。 the only problem is that the menubar is behind the jpanel, but if you hit the menu with your courser, you will see it 唯一的问题是菜单栏位于 jpanel的后面 ,但是如果您用选课器打菜单,您将看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM