java中弹出式菜单怎么用show方法显示 import java.awt.*; import javax.swing.*; public class S{ public s
就这样见但的显示出来在窗体上面现实.用java中的JPopupMenu弹出式菜单,用show()方法显示出来在窗体上....
就这样见但的显示出来在窗体上面现实.
用java中的JPopupMenu弹出式菜单,用show()方法显示出来在窗体上. 展开
用java中的JPopupMenu弹出式菜单,用show()方法显示出来在窗体上. 展开
1个回答
展开全部
代码如下:为窗体增加鼠标监听,实现public void mouseReleased(MouseEvent e) {
if ((e.getModifiers()&MouseEvent.BUTTON3_MASK)!=0&&
!e.isControlDown()&&!e.isShiftDown()) {
popup.show(jFrame, e.getX(),e.getY());
}
});
实现显示弹出菜单
为菜单增添时间监听,实现其功能。
完整代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.Popup;
public class TestPopup {
JPopupMenu popup;
JFrame jFrame;
public TestPopup(){
jFrame=new JFrame("测试");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
jFrame.setBounds(300,200,400,500);
popup=new JPopupMenu();
JMenuItem mItem=new JMenuItem("复制");
JMenuItem mItem2=new JMenuItem("粘贴");
JMenuItem mItem3=new JMenuItem("剪切");
mItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("复制:菜单事件处理");
}
});
mItem2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("粘贴:菜单事件处理");
}
});
mItem3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("剪切:菜单事件处理");
}
});
popup.add(mItem);
popup.add(mItem2);
popup.add(mItem3);
//jFrame.add(popup);
jFrame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers()&MouseEvent.BUTTON3_MASK)!=0&&
!e.isControlDown()&&!e.isShiftDown()) {
popup.show(jFrame, e.getX(),e.getY());
}
}
});
}
public static void main(String[] args) {
new TestPopup();
}
}
有什么疑问可以hi我
if ((e.getModifiers()&MouseEvent.BUTTON3_MASK)!=0&&
!e.isControlDown()&&!e.isShiftDown()) {
popup.show(jFrame, e.getX(),e.getY());
}
});
实现显示弹出菜单
为菜单增添时间监听,实现其功能。
完整代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.Popup;
public class TestPopup {
JPopupMenu popup;
JFrame jFrame;
public TestPopup(){
jFrame=new JFrame("测试");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
jFrame.setBounds(300,200,400,500);
popup=new JPopupMenu();
JMenuItem mItem=new JMenuItem("复制");
JMenuItem mItem2=new JMenuItem("粘贴");
JMenuItem mItem3=new JMenuItem("剪切");
mItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("复制:菜单事件处理");
}
});
mItem2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("粘贴:菜单事件处理");
}
});
mItem3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("剪切:菜单事件处理");
}
});
popup.add(mItem);
popup.add(mItem2);
popup.add(mItem3);
//jFrame.add(popup);
jFrame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers()&MouseEvent.BUTTON3_MASK)!=0&&
!e.isControlDown()&&!e.isShiftDown()) {
popup.show(jFrame, e.getX(),e.getY());
}
}
});
}
public static void main(String[] args) {
new TestPopup();
}
}
有什么疑问可以hi我
追问
import java.awt.*;
import javax.swing.*;
public class S{
public static void main(String args[]){
JFrame jf=new JFrame("我的第一个小程序");
JPopupMenu jp=new JPopupMenu();
JMenuItem jm=new JMenuItem("1");
JMenuItem jm1=new JMenuItem("2");
jp.add(jm);
jp.add(jm1);
jp.show(jf,20,20);
jf.setVisible(true);
}
}
追答
弹出式菜单,当鼠标右键时自己弹出,属鼠标事件。所以必须为PopupMenu 对象所在的容器注册鼠标监听事件,在容器的什么地方弹出需要获取鼠标的坐标,你要在窗体中显示,就为窗体注册鼠标监听事件。你的代码修改如下:
public class S{
public static void main(String args[]){
final JFrame jf=new JFrame("我的第一个小程序");
final JPopupMenu jp=new JPopupMenu();
JMenuItem jm=new JMenuItem("1");
JMenuItem jm1=new JMenuItem("2");
jp.add(jm);
jp.add(jm1);
jf.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers()&MouseEvent.BUTTON3_MASK)!=0&&
!e.isControlDown()&&!e.isShiftDown()) {
jp.show(jf, e.getX(),e.getY());
}
}
});
jf..setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setBounds(300,200,400,500);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询