在我的应用程序中,我使用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);
但此代码会导致重复项目添加到菜单。 我找不到有关如何检测菜单项是否存在或替换菜单项的任何帮助!
答案 0 :(得分: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);
}
}
答案 1 :(得分: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);
}
}
答案 2 :(得分: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();
}
}