帮我看看这几行代码 java新手 我使用mouselistener 哪里有问题?
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.e...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import javax.swing.*;
public class practice
{static DefaultListModel model=new DefaultListModel();
static JList list=new JList(model);
public static void main(String[] args)
{
JFrame jf=new JFrame("测试,鼠标双击事件");
JPanel jp=new JPanel();
jf.setContentPane(jp);
jp.add(list);
model.addElement("life");
model.addElement("good");
model.addElement("sTRING");
MouseListener mouselisten=new MyMouse();
list.addMouseListener(mouselisten);jf.setVisible(true);
}
}
class MyMouse extends MouseAdapter
{
public void MouseClicked(MouseEvent e)
{
if (e.getSource()==practice.list)
{
int i=e.getClickCount();
if(i==2)
{
System.out.println("刚才双击了");
}
}
}
} 展开
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import javax.swing.*;
public class practice
{static DefaultListModel model=new DefaultListModel();
static JList list=new JList(model);
public static void main(String[] args)
{
JFrame jf=new JFrame("测试,鼠标双击事件");
JPanel jp=new JPanel();
jf.setContentPane(jp);
jp.add(list);
model.addElement("life");
model.addElement("good");
model.addElement("sTRING");
MouseListener mouselisten=new MyMouse();
list.addMouseListener(mouselisten);jf.setVisible(true);
}
}
class MyMouse extends MouseAdapter
{
public void MouseClicked(MouseEvent e)
{
if (e.getSource()==practice.list)
{
int i=e.getClickCount();
if(i==2)
{
System.out.println("刚才双击了");
}
}
}
} 展开
1个回答
展开全部
把类改为:
class MyMouse extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (e.getSource() == Practice.list) {
int i = e.getClickCount();
if (i == 2) {
System.out.println("刚才双击了");
}
}
}
}
为了让窗口一运行就显示,在jf.setVisible(true);前加上jf.setSize(800, 600);
class MyMouse extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (e.getSource() == Practice.list) {
int i = e.getClickCount();
if (i == 2) {
System.out.println("刚才双击了");
}
}
}
}
为了让窗口一运行就显示,在jf.setVisible(true);前加上jf.setSize(800, 600);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询