JAVA中JPopupMenu的一点问题 大神请进
一个很简单的问题,就几行代码,如下:JPopupMenukuaijie=newJPopupMenu();fuzhi1=newJMenuItem("复制");kuaijie...
一个很简单的问题,就几行代码,如下:
JPopupMenu kuaijie=new JPopupMenu();
fuzhi1=new JMenuItem("复制");
kuaijie.add(fuzhi1);
textarea.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e){checkForTriggerEvent(e);}
public void mouseReleased(MouseEvent e){checkForTriggerEvent(e);}
private void checkForTriggerEvent(MouseEvent e)
{
if(e.isPopupTrigger())
{
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
});
死都不能显示右键快捷菜单,求大神指教 展开
JPopupMenu kuaijie=new JPopupMenu();
fuzhi1=new JMenuItem("复制");
kuaijie.add(fuzhi1);
textarea.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e){checkForTriggerEvent(e);}
public void mouseReleased(MouseEvent e){checkForTriggerEvent(e);}
private void checkForTriggerEvent(MouseEvent e)
{
if(e.isPopupTrigger())
{
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
});
死都不能显示右键快捷菜单,求大神指教 展开
展开全部
lz 你好
JPopupMenu应该是在点击右键的时候 实现吧
给lz一个完整的例子:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class _PopupMenu extends JFrame{
private JPopupMenu popupMenu;
private JMenuItem copy = new JMenuItem("复制");
public _PopupMenu(){
super("右键子菜单");
popupMenu = new JPopupMenu();
popupMenu.add(copy);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
//如果点击右键 显示子菜单
if(e.getButton() == MouseEvent.BUTTON3){
//获取相对于窗口的逻辑位置
Point p = e.getPoint();
popupMenu.show(e.getComponent(), p.x, p.y);
}
//否则不显示子菜单
else{
popupMenu.setVisible(false);
}
}
});
setSize(300,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setVisible(true);
}
public static void main (String[] args) {
new _PopupMenu();
}
}
运行效果:
希望能帮助你哈
追问
请问下面的自动换行怎么不对了
lineWrap=new CheckboxMenuItem("自动换行");
else if(e.getSource()==lineWrap)
{
textarea.setLineWrap(!textarea.getLineWrap());
}
展开全部
你添加右键菜单的方法错了:
textarea.setComponentPopupMenu(kuaijie);这样添加
我写了一个例子你看看:
package org.somken;
import java.awt.PopupMenu;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
public class JPopupMenuFrame extends JFrame{
public JPopupMenuFrame() {
// TODO Auto-generated constructor stub
super("右键菜单");
this.setSize(330, 200);
this.setLayout(null);
this.setLocation(100, 50);
JTextArea textarea=new JTextArea(100,10);
textarea.setBounds(10, 10, 100, 100);
JPopupMenu kuaijie=new JPopupMenu();
JMenuItem fuzhi1=new JMenuItem("复制");
kuaijie.add(fuzhi1);
//添加右键菜单
textarea.setComponentPopupMenu(kuaijie);
JPanel pane=new JPanel();
pane.add(textarea);
setContentPane(pane);
setVisible(true);
}
public static void main(String[] args) {
new JPopupMenuFrame();
}
}
追问
请问下面的自动换行怎么不对了
lineWrap=new CheckboxMenuItem("自动换行");
else if(e.getSource()==lineWrap)
{
textarea.setLineWrap(!textarea.getLineWrap());
}
追答
你可以设置一下滚动条 只出现垂直的滚动,把水平滚动关闭 就可以自动换行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
jpopmenu没有注册吧!在哪用就在哪add(kuaijir);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询