我需要创建一个GUI,必须提供一个弹出菜单。我设法做到这一点,但它只出现,如果我设置布局null -在这种情况下,有问题,我的功能都不出现,只有弹出式菜单。我不允许使用Java。秋千或者别的什么
我如何管理让弹出菜单出现没有设置我的布局null?
我希望你能理解我的问题,在这里你可以检查我的代码:
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);
,它应该与非空布局工作良好。弹出式菜单被设计为在其他组件上显示,所以在这种情况下,您不需要将它添加到其他组件上。