简体   繁体   English

如何将Jmenubar添加到mouseclick程序?

[英]How to add Jmenubar to mouseclick program?

I'm writing a code that display a rectangle on the mouse click and store it in arraylist and I wanted to add a menu to give the option to the user to save the shape in a file and load it. 我正在编写一个代码,该代码在单击鼠标时显示一个矩形并将其存储在arraylist中,我想添加一个菜单,以向用户提供将形状保存到文件中并加载它的选项。

So I combined and old menu class that I've been working on with the mouse click class and when I run, I only get an empty JFrame box with no menu and no rect appear when I click anywhere. 因此,我结合了我一直在使用鼠标单击类处理的旧菜单类,并且在运行时,我只得到一个空的JFrame框,没有菜单,单击任何地方都不会显示矩形。 and there's an illegalArgumentExpression in the console. 控制台中有一个非法的ArgumentExpression。

The code worked perfectly until I added the third class, so which part is wrong? 在添加第三类之前,代码运行良好,那么哪一部分是错误的?

First Class: 头等舱:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MouseClick{
    private static int x,y;
    private static DrawingObjects object = new DrawingObjects();
    private static MenuDemo menu = new MenuDemo();

    public static void main(String[] args){

        JFrame frame = new JFrame("MouseClick");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.add(object);
        frame.add(menu);
        object.addMouseListener(new AL());
    }
    static class AL extends MouseAdapter{
        public void mouseClicked (MouseEvent e){
            x = e.getX();
            y = e.getY();
            object.drawing(x, y);
        }
    }
}

Second Class: 二等舱:

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawingObjects extends JPanel{
    private ArrayList<Point> points = new ArrayList<>();


    public void drawing(int x, int y){
        points.add(new Point(x, y));
        repaint();
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(Point p : points){
            g.fillRect(p.x, p.y, 20, 20);

        }
    }
    }

Third Class: 第三类:

import java.awt.*;
import javax.swing.*;

public class MenuDemo extends JFrame {
    JMenu mnuFile, mnuExit, mnuitemSaveAs;
    JMenuItem mnuitemNew, mnuitemSave, mnuitemDoc, mnuitemTxt, mnuitemDat;
    public MenuDemo(){
        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        mnuFile = new JMenu("File");
        mnuExit = new JMenu("Exit");

        mnuitemNew = new JMenuItem("New");
        mnuitemSave = new JMenuItem("Save");
        mnuitemSaveAs = new JMenu ("Save As");

        mnuitemDoc = new JMenuItem(".doc");
        mnuitemTxt = new JMenuItem(".txt");
        mnuitemDat= new JMenuItem(".dat");

// menu bar 
        JMenuBar jmb = new JMenuBar();
        setJMenuBar(jmb);

        mnuFile.add(mnuitemNew);
        mnuFile.add(mnuitemSave);
        mnuFile.add(mnuitemSaveAs);
        mnuitemSaveAs.add(mnuitemDoc);
        mnuitemSaveAs.add(mnuitemTxt);
        mnuitemSaveAs.add(mnuitemDat);

        jmb.add(mnuFile);
        jmb.add(mnuExit);

    }
    }

Your MenuDemo class extends JFrame. 您的MenuDemo类扩展了JFrame。 You cannot add a JFrame to another JFrame. 您不能将JFrame添加到另一个JFrame。 The main method in your MouseClick class must call frame.setJMenuBar . frame.setJMenuBar类中的main方法必须调用frame.setJMenuBar

I suggest you alter your MenuDemo class to store its JMenuBar in a field, just like all the menus and menu items, so other classes (like MouseClick) are able to access it. 建议您更改MenuDemo类以将其JMenuBar存储在字段中,就像所有菜单和菜单项一样,以便其他类(例如MouseClick)也可以访问它。

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

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