gpt4 book ai didi

java - JMenu 和 JPopupMenu 的 Swing 鼠标单击事件

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:26:32 24 4
gpt4 key购买 nike

我希望在 Swing 中的菜单和弹出菜单外单击鼠标时具有 Eclipse 行为。基本上,在 Eclipse 中,当您在菜单外按下鼠标时,菜单会消失,鼠标按下事件将转发到您用鼠标单击的组件。

令人恼火的是,Swing 不这样做,我找不到解决方法。

现在,即使使用 Windows LAF,我也必须再次单击才能让组件注册鼠标单击。

为了响应,就在 table 上做吧

final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem viewProfile = new JMenuItem("View Profile");
viewProfile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
popupMenu.add(viewProfile);
table.setComponentPopupMenu(popupMenu);

编辑:这是一个测试代码。我认为向每个简单组件添加鼠标监听器以便它们可以在弹出菜单后注册鼠标单击并不方便。在下面的示例中,Table 确实实现了它,但 Button 没有。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestMouse {

public static void main(String[] args) {
new TestMouse();
}

private JLabel counter;

private int count;

private JPanel northPanel;

private JButton clickMe;

public TestMouse() {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

DefaultTableModel model = new DefaultTableModel(new Object[] { "A", "B", "B", "B", "B", "B", "B" }, 10);
JTable table = new JTable(model);

northPanel = new JPanel();

clickMe = new JButton("Button");
clickMe.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("clicked");
count++;
}
});
counter = new JLabel("0");

northPanel.add(counter);
northPanel.add(clickMe);

final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem viewProfile = new JMenuItem("View Profile");
viewProfile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
popupMenu.add(viewProfile);
table.setComponentPopupMenu(popupMenu);
table.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
count++;
counter.setText(String.valueOf(count));
}

});

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(northPanel, BorderLayout.NORTH);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

最佳答案

我似乎无法重现这个问题。

当我右键单击表格时,弹出菜单出现,当我再次单击表格(关闭弹出窗口)时,mouseClicked 事件被触发。

ClicktyClack

注意北位置的计数器...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestMouse {

public static void main(String[] args) {
new TestMouse();
}

private JLabel counter;
private int count;

public TestMouse() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

DefaultTableModel model = new DefaultTableModel(
new Object[]{"A", "B", "B", "B", "B", "B", "B"},
10
);
JTable table = new JTable(model);
counter = new JLabel("0");

final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem viewProfile = new JMenuItem("View Profile");
viewProfile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
popupMenu.add(viewProfile);
table.setComponentPopupMenu(popupMenu);
table.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
count++;
counter.setText(String.valueOf(count));
}

});

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(counter, BorderLayout.NORTH);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

实际runnable example that demonstrates your problem将涉及更少的猜测工作和更好的响应

关于java - JMenu 和 JPopupMenu 的 Swing 鼠标单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com