Java AWT - 将弹出菜单添加到没有布局的面板/框架

Java AWT - Add a popupMenu to a Panel / Frame without Layout

我需要创建一个必须提供 PopupMenu 的 GUI。我设法做到了这一点,但只有当我将 Layout 设置为 null 时它才会出现 - 在这种情况下存在问题,我的 none 功能仅出现在弹出菜单中。 我不允许使用 Java.swing 或其他东西。

如何在不将 Layout 设置为 null 的情况下让 popupMenu 出现?

希望你能理解我的问题,你可以在这里查看我的代码:

public class ServerStart {

    public static void main(String[] args) {
        ServerInterface serverInterface = new ServerInterface();
        serverInterface.setVisible(true);
    }
}

class ServerInterface extends Frame implements Runnable {

    private int portNr;
    private MenuBar menuBar;
    private String fileName;
    private TextField portText;
    private PopupMenu popupMenu;
    private Thread interfaceThread;
    private ServerThread serverThread;
    private Panel mainPanel, displayPanel;
    private Menu menuServer, menuFile, menuDatabase, menuProgram;
    private MenuItem start, stop, create, choose, connect, beenden, popupstart,
            popupstop, popupclose;
    private Label title, port, textClient, textStatus, clientNr, serverStatus;
    public static int client = 0;
    ServerSocket serverSocket;
    ServerInterface serverInterface;

    public ServerInterface() {
        super("ADRELI 4 GUI - SERVER");
        this.setBounds(325, 50, 475, 355);
       // this.setLayout(null);
        this.setLayout(new BorderLayout());
        this.setResizable(false);
        this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                DialogWindow dialogWindow = new DialogWindow(
                        new ServerInterface(), "Programm beenden",
                        "Wollen Sie das Programm wirklich beenden?",
                        "OK", "Abbrechen");
                if (dialogWindow.getAnswer()) {
                    System.exit(0);
                }
            }
        }
        );

        mainPanel = new Panel();
        mainPanel.setBackground(Color.darkGray);
        mainPanel.setLayout(null);
        this.add(mainPanel);

        title = new Label("SERVER");
        title.setBackground(Color.white);
        title.setBounds(20, 20, 425, 100);
        title.setAlignment(Label.CENTER);
        title.setFont(new Font("Arial", Font.BOLD, 25));
        mainPanel.add(title);

        menuBar = new MenuBar();
        menuServer = new Menu("Server...");
        start = new MenuItem("Start");
        stop = new MenuItem("Stop");
        stop.setEnabled(false);
        menuBar.add(menuServer);
        menuServer.add(start);
        menuServer.add(stop);
        this.setMenuBar(menuBar);

        menuFile = new Menu("Datei...");
        create = new MenuItem("Anlegen");
        choose = new MenuItem("Auswaehlen");
        menuFile.add(create);
        menuFile.add(choose);
        menuBar.add(menuFile);

        menuDatabase = new Menu("Datenbank...");
        connect = new MenuItem("Verbinden");
        menuDatabase.add(connect);
        menuBar.add(menuDatabase);

        menuProgram = new Menu("Programm...");
        beenden = new MenuItem("Beenden");
        menuProgram.add(beenden);
        menuBar.add(menuProgram);

        popupMenu = new PopupMenu();
        popupstart = new MenuItem("Start");
        popupstop = new MenuItem("Stop");
        popupstop.setEnabled(false);
        popupclose = new MenuItem("Beenden");
        popupMenu.add(popupstart);
        popupMenu.add(popupstop);
        popupMenu.addSeparator();
        popupMenu.add(popupclose);
        this.add(popupMenu);

        displayPanel = new Panel();
        displayPanel.setBounds(20, 140, 425, 150);
        displayPanel.setLayout(null);
        displayPanel.setBackground(Color.lightGray);

        textStatus = new Label("Serverstatus:");
        textStatus.setBounds(20, 20, 300, 30);
        textStatus.setBackground(Color.white);
        textStatus.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(textStatus);

        textClient = new Label("Aktive Clients:");
        textClient.setBounds(20, 60, 300, 30);
        textClient.setBackground(Color.white);
        textClient.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(textClient);

        port = new Label("Portnummer:");
        port.setBounds(20, 100, 300, 30);
        port.setBackground(Color.white);
        port.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(port);

        serverStatus = new Label();
        serverStatus.setBackground(Color.red);
        serverStatus.setBounds(355, 20, 50, 30);
        displayPanel.add(serverStatus);

        clientNr = new Label(" 0 ");
        clientNr.setBackground(Color.white);
        clientNr.setBounds(355, 60, 50, 30);
        clientNr.setAlignment(Label.CENTER);
        clientNr.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(clientNr);

        portText = new TextField("56789");
        portText.setBackground(Color.white);
        portText.setBounds(355, 100, 50, 30);
        portText.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(portText);

        start.addActionListener(new ItemClicked());
        stop.addActionListener(new ItemClicked());
        choose.addActionListener(new ItemClicked());
        create.addActionListener(new ItemClicked());
        connect.addActionListener(new ItemClicked());
        beenden.addActionListener(new ItemClicked());
        popupstart.addActionListener(new ItemClicked());
        popupstop.addActionListener(new ItemClicked());
        popupclose.addActionListener(new ItemClicked());
        mainPanel.add(displayPanel);
    }

    @Override
    public void processMouseEvent(MouseEvent me) {
        if (me.isPopupTrigger()) {
            popupMenu.show(me.getComponent(), me.getX(), me.getY());
        }
        super.processMouseEvent(me);
    }

删除 this.add(popupMenu); 它应该可以在非空布局下正常工作。弹出菜单旨在显示在其他组件之上,因此在这种情况下您不要将它添加到其他组件。