当前位置: 技术问答>java相关
Jpopupmenu的问题
来源: 互联网 发布时间:2015-05-22
本文导语: 我写的jpopupmenu测试程序执行时,点击右键就出错 TestPopup$PopListener.faint(TestPopup.java:36) TestPopup$PopListener.mouseReleased(TestPopup.java:30) 那为大虾看下啊 import java.awt.*; import java.awt.event.*; import javax.swing.*; class TestPopup extends...
我写的jpopupmenu测试程序执行时,点击右键就出错
TestPopup$PopListener.faint(TestPopup.java:36) TestPopup$PopListener.mouseReleased(TestPopup.java:30)
那为大虾看下啊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestPopup extends JFrame
{
TextArea ta;
Container pane;
JPopupMenu pop;
JMenuItem mi;
public TestPopup()
{
TextArea ta=new TextArea();
Container pane=getContentPane();
pane.add(ta);
JMenuItem mi=new JMenuItem("hello");
JPopupMenu pop=new JPopupMenu();
pop.add(mi);
MouseListener pl=new PopListener();
ta.addMouseListener(pl);
}
class PopListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
faint(e);
}
public void mouseReleased(MouseEvent e)
{
faint(e);
}
private void faint(MouseEvent e)
{
if (e.isPopupTrigger())
{
pop.show(e.getComponent(),e.getX(),e.getY());
}
}
}
public static void main(String[] args)
{
TestPopup testpop=new TestPopup();
testpop.setVisible(true);
}
}
TestPopup$PopListener.faint(TestPopup.java:36) TestPopup$PopListener.mouseReleased(TestPopup.java:30)
那为大虾看下啊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestPopup extends JFrame
{
TextArea ta;
Container pane;
JPopupMenu pop;
JMenuItem mi;
public TestPopup()
{
TextArea ta=new TextArea();
Container pane=getContentPane();
pane.add(ta);
JMenuItem mi=new JMenuItem("hello");
JPopupMenu pop=new JPopupMenu();
pop.add(mi);
MouseListener pl=new PopListener();
ta.addMouseListener(pl);
}
class PopListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
faint(e);
}
public void mouseReleased(MouseEvent e)
{
faint(e);
}
private void faint(MouseEvent e)
{
if (e.isPopupTrigger())
{
pop.show(e.getComponent(),e.getX(),e.getY());
}
}
}
public static void main(String[] args)
{
TestPopup testpop=new TestPopup();
testpop.setVisible(true);
}
}
|
不是这个问题。
问题在于类的某些成员变量没有正确地指向对象,可以把TestPopup()的构造函数做如下修改:
public TestPopup()
{
ta=new TextArea();//去掉前面的类型
pane=getContentPane();
pane.add(ta);
mi=new JMenuItem("hello");
pop=new JPopupMenu();
pop.add(mi);
MouseListener pl=new PopListener();
ta.addMouseListener(pl);
}
问题在于类的某些成员变量没有正确地指向对象,可以把TestPopup()的构造函数做如下修改:
public TestPopup()
{
ta=new TextArea();//去掉前面的类型
pane=getContentPane();
pane.add(ta);
mi=new JMenuItem("hello");
pop=new JPopupMenu();
pop.add(mi);
MouseListener pl=new PopListener();
ta.addMouseListener(pl);
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。