2个回答
2013-10-30
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ExamplePop {
JFrame frame;
JTextAreaMenu textmenu;
private JScrollPane pane;
public ExamplePop(){
init();
}
private void init(){
frame =new JFrame("测试右键菜单");
textmenu=new JTextAreaMenu();
pane =new JScrollPane(textmenu);
frame.add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
public static void main(String[] args){
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
new ExamplePop();
}
class JTextAreaMenu extends JTextArea implements MouseListener{
private JPopupMenu pop = null; // 弹出菜单
private JMenuItem copy = null, paste = null, cut = null; // 三个功能菜单
public JTextAreaMenu() {
super();
init();
}
private void init() {
this.addMouseListener(this);
pop = new JPopupMenu();
pop.add(copy = new JMenuItem("复制"));
pop.add(paste = new JMenuItem("粘贴"));
pop.add(cut = new JMenuItem("剪切"));
this.add(pop);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
pop.show(this, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
}
}
}
import java.awt.*;
import java.awt.event.*;
public class ExamplePop {
JFrame frame;
JTextAreaMenu textmenu;
private JScrollPane pane;
public ExamplePop(){
init();
}
private void init(){
frame =new JFrame("测试右键菜单");
textmenu=new JTextAreaMenu();
pane =new JScrollPane(textmenu);
frame.add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
public static void main(String[] args){
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
new ExamplePop();
}
class JTextAreaMenu extends JTextArea implements MouseListener{
private JPopupMenu pop = null; // 弹出菜单
private JMenuItem copy = null, paste = null, cut = null; // 三个功能菜单
public JTextAreaMenu() {
super();
init();
}
private void init() {
this.addMouseListener(this);
pop = new JPopupMenu();
pop.add(copy = new JMenuItem("复制"));
pop.add(paste = new JMenuItem("粘贴"));
pop.add(cut = new JMenuItem("剪切"));
this.add(pop);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
pop.show(this, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-30
展开全部
如果你用swt组件的话,默认就会有右键菜单了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询