0

在我的应用程序中,我使用 jmenuitem 执行一些操作以通过套接字编程连接特定端口。现在,我想在单击“连接”菜单项时,将其名称和操作更改为“断开连接”。我的“连接”菜单项代码是这样的:

connect = new JMenuItem("Connect");
    connect.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            loginForm login = new loginForm();
            login.setVisible(true);

        }
    });
 jpopupMenu.insert(connect, 0); 

和“断开”代码是这样的:

disconnect = new JMenuItem("Disonnect");
    disconnect.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                client.disconnect();
            } catch (IOException ex) {
                Logger.getLogger(systemTray.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
jpopupMenu.insert(disconnect, 0);

但是此代码会导致重复项添加到菜单中。我找不到有关如何检测菜单项是否存在或替换菜单项的任何帮助!

4

3 回答 3

2

您的问题标题说明了一切(即Action,但您正在使用ActionListener/JMenuItem)。使用Action(我们可以使用它代替JMenuItem),我们可以简单地覆盖该NAME值,该值出现在菜单项名称中。这是一个例子。

注意menu.add(Action)而不是menu.add(JMenuItem)

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

public class TestMenu {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");

        Action menuAction = new AbstractAction("Connect") {
            private boolean connected = false;
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!connected) {
                    JOptionPane.showMessageDialog(null, "Connected");
                    putValue(NAME, "Disonnect");
                    connected = true;
                } else {
                    JOptionPane.showMessageDialog(null, "Disconnected");
                    putValue(NAME, "Connect");
                    connected = false;
                }     
            }  
        };

        menuBar.add(menu);
        menu.add(menuAction);
        f.setJMenuBar(menuBar);
        f.setVisible(true);
    }
}
于 2014-11-05T13:08:52.550 回答
1

我想你可以做这样的事情,这将允许你对两个操作使用单个菜单项,并根据客户端是否连接来更改其操作/文本。

private JMenuItem connect = new JMenuItem("Connect");

// ...

connect.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (client.isConnected()) {
            disconnect();
        } else {
            connect();
        }
    }
});

// ...

private void connect() {
    loginForm login = new loginForm();
    login.setVisible(true);
    // TODO: You should change the label only if connection actually succeeds
    connect.setText("Disconnect");
}

private void disconnect() {
    try {
        client.disconnect();
        connect.setText("Connect");
    } catch (IOException ex) {
        Logger.getLogger(systemTray.class.getName()).log(Level.SEVERE, null, ex);
    }
}
于 2014-11-05T12:58:46.847 回答
1

也许这也会对您有所帮助。
我把它们放在一个 jpopupmenu 上

public class Test {

    JPopupMenu popmenu;
    public boolean isConnected = false;

    public Test() {
        JFrame frame = new JFrame();
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        popmenu = new JPopupMenu();
        final JMenuItem connect = new JMenuItem("connect");
        connect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if (!isConnected) {
                            System.out.println("connect action");

                            connect();

                            isConnected = true;
                        } else {
                            System.out.println("disconnect action");

                            disconnect();

                            isConnected = false;
                        }
                    }
                });

            }
        });

        popmenu.insert(connect, 0);

        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popmenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        frame.setVisible(true);
    }

    private void connect() {
        loginForm login = new loginForm();
        login.setVisible(true);
        // TODO: You should change the label only if connection actually
        // succeeds
        connect.setText("Disconnect");
    }

    private void disconnect() {
        try {
            client.disconnect();
            connect.setText("Connect");
        } catch (IOException ex) {
            Logger.getLogger(systemTray.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    public static void main(String[] args) {
        new Test();
    }
}
于 2014-11-05T14:14:51.407 回答