我有一个JFrame
,看起来像一个典型的聊天框。要连接服务器并与服务器断开连接,我实施了JPopupMenu
并在主窗口中添加了MouseListener
但是弹出菜单不会出现。
主窗口中的各种JComponents:
JTextArea
在JScrollPane
JTextField
输入消息Send
按钮JScrollPane
和ArrayList
以显示用户JTextArea
或字段上输入您的信息
我添加了所有组件,还添加了哪些监听器?
变量:
private static A_Chat_Client chatClient;
public static String userName = "Anonymous";
//------------------------------------------------------------------------------
public static JFrame mainFrame = new JFrame();
public static JTextArea textArea = new JTextArea(30,30);
public static JScrollPane pane = new JScrollPane(textArea);
public static JTextField message = new JTextField(10);
public static JButton send = new JButton("Send");
public static JPopupMenu popup = new JPopupMenu();
public static JMenuItem connect = new JMenuItem("Connect");
public static JMenuItem disconnect = new JMenuItem("Disconnect");
public static JMenuItem help = new JMenuItem("Help");
public static JList usersOnline = new JList();
public static JScrollPane userPane = new JScrollPane(usersOnline);
主要方法
public static void main(String[] args) {
buildMainWindow();
initialize();
addListeners();
popup.show(mainFrame, 0, 0); //forcefully popup
}
addListeners()
public static void addListeners(){
mainFrame.addMouseListener(new MouseHandler());
send.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
actionSend();
}
});
connect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
actionConnect();
}
});
disconnect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
actionDisconnect();
}
});
help.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
actionHelp();
}
});
}
类MouseHandler
private static class MouseHandler extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e){
if(e.isPopupTrigger()){
popup.show(mainFrame, e.getX(), e.getY());
}
}
}
答案 0 :(得分:2)
您可以使用AWTEventListener
Toolkit#addAWTEventListener
附加到主要事件队列
然后,您需要检查您正在接收的事件类型,并检查它是否是弹出事件。
public class TestGlobalPopup {
public static void main(String[] args) {
new TestGlobalPopup();
}
public TestGlobalPopup() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
if (me.isPopupTrigger()) {
Component component = me.getComponent();
JPopupMenu popup = new JPopupMenu();
popup.add(new JLabel("Clicked on " + component.getClass().getName()));
popup.show(component, me.getX(), me.getY());
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Label: "), gbc);
gbc.gridx++;
add(new JTextField(10), gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
add(new JScrollPane(new JTextArea(5, 20)), gbc);
}
}
}