gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:09:39 24 4
gpt4 key购买 nike

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

如何在不将布局设置为空的情况下让弹出菜单出现?

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

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); 它应该可以在非空布局中正常工作。弹出菜单旨在显示在其他组件之上,因此在这种情况下您无需将其添加到其他组件。

关于Java AWT - 将弹出菜单添加到没有布局的面板/框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com