简体   繁体   中英

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. the only problem is that the menubar is behind the jpanel, but if you hit the menu with your courser, you will see it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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