我需要创建一个必须提供PopupMenu的GUI。我设法做到这一点,但只有在我将布局设置为null时才会出现 - 在这种情况下会出现问题,我的所有功能都只出现在popupmenu中。 我不被允许使用Java.swing或其他东西。
如何在不设置布局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);
}
答案 0 :(得分:1)
删除this.add(popupMenu);
,它应该可以正常使用非null布局。弹出菜单旨在显示在其他组件上,因此在这种情况下您不能将其添加到另一个组件。