给楼主写了一个例子,希望对楼主有点帮助:// MenuTest.javaimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class MenuTest
{
public static void main(String[] args)
{
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a sample menu bar.
*/
class MenuFrame extends JFrame
{
public MenuFrame()
{
setTitle("MenuTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); popup = new JPopupMenu();
JMenuItem item = new JMenuItem("Click me");
item.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello, world!");
}
});
popup.add(item);
getContentPane().addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
if (event.isPopupTrigger())
popup.show(event.getComponent(),
event.getX(), event.getY());
} public void mouseReleased(MouseEvent event)
{
if (event.isPopupTrigger())
popup.show(event.getComponent(),
event.getX(), event.getY());
}
});
} public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200; private JPopupMenu popup;
}/**
A sample action that prints the action name to System.out
*/
class TestAction extends AbstractAction
{
public TestAction(String name) { super(name); }
public void actionPerformed(ActionEvent event)
{
System.out.println(getValue(Action.NAME)
+ " selected.");
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class MenuTest
{
public static void main(String[] args)
{
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a sample menu bar.
*/
class MenuFrame extends JFrame
{
public MenuFrame()
{
setTitle("MenuTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); popup = new JPopupMenu();
JMenuItem item = new JMenuItem("Click me");
item.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello, world!");
}
});
popup.add(item);
getContentPane().addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
if (event.isPopupTrigger())
popup.show(event.getComponent(),
event.getX(), event.getY());
} public void mouseReleased(MouseEvent event)
{
if (event.isPopupTrigger())
popup.show(event.getComponent(),
event.getX(), event.getY());
}
});
} public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200; private JPopupMenu popup;
}/**
A sample action that prints the action name to System.out
*/
class TestAction extends AbstractAction
{
public TestAction(String name) { super(name); }
public void actionPerformed(ActionEvent event)
{
System.out.println(getValue(Action.NAME)
+ " selected.");
}
}
解决方案 »
- java操作数据库
- The source attachment doesnot contain the source for the file TrueTypeFont.class
- 菜鸟问题——整型常量的输出
- 很简单的问题,请大家帮忙看下~急用,谢了~
- 如何将一个String变量转换成一个Char数组?
- 麻烦懂GMF的朋友进来帮我确认下是不是GMF插件安装的问题!
- 语句String _str = (String)null;执行后为什么没有抛出异常,而是仍旧返回空值?毕竟是对空值做强制类型转换呀,请高手解答
- 在JTable中如何控制鼠标焦点啊?
- 关于System.out与编码格式的问题
- 300分求chm格式的java api帮助文档
- 哪有《Java核心(第二版)》的随书源码?万分感谢!!!
- 关于jbx的问题
想进一步问一下,如果是一课树,点右键,在菜单里面选择增加一个树叶,能不能帮写一下代码,谢谢!
我的MSN: [email protected]
其实我也是刚开始学习Java, 对Java GUI比较感兴趣,刚看完<<Core Java I>>,现在在看<<Thinking in Java>>, 希望能和你讨论java编程。