我遇到了问题,我自己找不到答案。 我在java中进行简单的聊天,然后用名称填充了一个JList。双击某个名称时,该名称需要作为参数传递给另一个帧(作为收件人名称)。但是,双击不起作用
我有一个主类InstantMessageFrame,其中一个名为friends的JList被初始化并填充了一个字符串数组。
private JList<String> friends;
String names[] = { "Ana", "Banana", "Cikla", "Doris", "Ema", "Mirna","Matea","Veronika","Vera","Marta","Mirta","Davor","Marko","Matko","Kloki" };
JList<String> friends = new JList<String>(names);
另外,我在JList中添加了一个监听器
DisplayMessageDialog dmd = new DisplayMessageDialog();
friends.addMouseListener(dmd);
这是我的DisplayMessageDialog类,它检查是否有双击。如果双击,则应显示新框架。第一个“if”语句中的所有行都不执行(一个使用e.getClickCount())
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DisplayMessageDialog extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
JList theList = (JList) e.getSource();
if (e.getClickCount() == 2)
{
int index = theList.locationToIndex(e.getPoint());
if (index >= 0)
{
Object o = theList.getModel().getElementAt(index);
InstantMessageDialog imd = new InstantMessageDialog(null, o.toString());
imd.setVisible(true);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
}
这应该是这样的:
http://i.stack.imgur.com/Bex5U.png
双击时,应出现一个新框架(代码为“InstantMessageDialog”对象)
http://i.stack.imgur.com/yjvW6.png
看起来应该是这样的。
答案 0 :(得分:3)
双击名称时......
在设计GUI时,最好为鼠标和键盘提供支持。
结帐List Action。它为Action
添加了JList
支持。您需要做的就是提供Action
。然后可以通过以下方式调用Action
:
但是,双击不起作用
使用Enter键测试操作以验证其是否有效。如果是,则尝试双击。如果双击无效,请将mouseClicked
中的ListAction
方法更改为mousePressed
。
mousePressed
事件更可靠,因为只有在为同一像素生成mousePressed / mouseReleased事件时才会生成mouseClicked
事件。因此,如果鼠标移动了1个像素,则不会生成mouseClicked事件。
答案 1 :(得分:2)
所以解决你的上下文代码片段,这个例子似乎工作得很好,所以我只能猜测你的问题就在你的代码中你没有向我们展示的地方......
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JList<String> friends;
public TestPane() {
String names[] = {"Ana", "Banana", "Cikla", "Doris", "Ema", "Mirna", "Matea", "Veronika", "Vera", "Marta", "Mirta", "Davor", "Marko", "Matko", "Kloki"};
JList<String> friends = new JList<String>(names);
setLayout(new BorderLayout());
add(new JScrollPane(friends));
DisplayMessageDialog dmd = new DisplayMessageDialog();
friends.addMouseListener(dmd);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class DisplayMessageDialog extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
JList theList = (JList) e.getSource();
// if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
if (e.getClickCount() == 2) {
int index = theList.locationToIndex(e.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
//InstantMessageDialog imd = new InstantMessageDialog(null, o.toString());
//imd.setVisible(true);
JOptionPane.showMessageDialog(theList, "Double-clicked on: " + o.toString());
System.out.println("Double-clicked on: " + o.toString());
}
}
}
}
}
我可能会补充一下,你“双击”检查可能应该更像是......
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
假设您真的只想回复鼠标左键点击