java 在textarea加入右键菜单
importjava.awt.*;importjava.awt.event.*;publicclassMypopupextendsFrame{PopupMenup;Tex...
import java.awt.*;
import java.awt.event.*;
public class Mypopup extends Frame {
PopupMenu p;
TextArea textmenu;
private ScrollPane pane ;
MenuItem mi1, mi2, mi3, mi4;
TextArea t;
public Mypopup() {
super("Mypopup Menu");
}
public void init() {
setLayout(new FlowLayout());
t = new TextArea();
p = new PopupMenu();
pane = new ScrollPane();
p.add(mi1 = new MenuItem("View Source"));
mi1.addActionListener(new miActionListener());
p.addSeparator();
p.add(mi2 = new MenuItem("Print"));
mi2.addActionListener(new miActionListener());
p.add(mi3 = new MenuItem("Refersh"));
mi3.addActionListener(new miActionListener());
p.addSeparator();
p.add(mi4 = new MenuItem("Properties"));
mi4.addActionListener(new miActionListener());
t.add(p);
pane.add(t);
add(pane);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
pack();
setVisible(true);
// show();
}
public void processMouseEvent(MouseEvent e) {
if (e.isPopupTrigger()) {
p.show(e.getComponent(), e.getX(), e.getY());
}
}
class miActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText(e.getActionCommand());
}
}
public static void main(String[] args) {
Mypopup pu = new Mypopup();
pu.init();
pu.setSize(500, 300);
}
}
不要系统菜单,要自己添加的菜单 展开
import java.awt.event.*;
public class Mypopup extends Frame {
PopupMenu p;
TextArea textmenu;
private ScrollPane pane ;
MenuItem mi1, mi2, mi3, mi4;
TextArea t;
public Mypopup() {
super("Mypopup Menu");
}
public void init() {
setLayout(new FlowLayout());
t = new TextArea();
p = new PopupMenu();
pane = new ScrollPane();
p.add(mi1 = new MenuItem("View Source"));
mi1.addActionListener(new miActionListener());
p.addSeparator();
p.add(mi2 = new MenuItem("Print"));
mi2.addActionListener(new miActionListener());
p.add(mi3 = new MenuItem("Refersh"));
mi3.addActionListener(new miActionListener());
p.addSeparator();
p.add(mi4 = new MenuItem("Properties"));
mi4.addActionListener(new miActionListener());
t.add(p);
pane.add(t);
add(pane);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
pack();
setVisible(true);
// show();
}
public void processMouseEvent(MouseEvent e) {
if (e.isPopupTrigger()) {
p.show(e.getComponent(), e.getX(), e.getY());
}
}
class miActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText(e.getActionCommand());
}
}
public static void main(String[] args) {
Mypopup pu = new Mypopup();
pu.init();
pu.setSize(500, 300);
}
}
不要系统菜单,要自己添加的菜单 展开
展开全部
你可以直接给textarea添加一个右键监听,当检测到右键事件后,显示一个弹出菜单,弹出菜单就是自己的顶一定义的功能了。
public class PopupMenu extends JFrame {
private JPopupMenu menu;
private JTextArea text;
public PopupMenu() {
JMenuItem m;
MenuItemListener mil = new MenuItemListener();
menu = new JPopupMenu();
text = new JTextArea(15, 30);
m = new JMenuItem("Item1");
m.addActionListener(mil);
menu.add(m);
m = new JMenuItem("Item2");
m.addActionListener(mil);
menu.add(m);
m = new JMenuItem("Item3");
m.addActionListener(mil);
menu.add(m);
text.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.show(text, e.getX(), e.getY());
}
}
});
getContentPane().add(text);
}
private class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.setText(((JMenuItem) e.getSource()).getText());
}
}
public static void main(String[] args) {
PopupMenu frame = new PopupMenu();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
public class PopupMenu extends JFrame {
private JPopupMenu menu;
private JTextArea text;
public PopupMenu() {
JMenuItem m;
MenuItemListener mil = new MenuItemListener();
menu = new JPopupMenu();
text = new JTextArea(15, 30);
m = new JMenuItem("Item1");
m.addActionListener(mil);
menu.add(m);
m = new JMenuItem("Item2");
m.addActionListener(mil);
menu.add(m);
m = new JMenuItem("Item3");
m.addActionListener(mil);
menu.add(m);
text.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.show(text, e.getX(), e.getY());
}
}
});
getContentPane().add(text);
}
private class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.setText(((JMenuItem) e.getSource()).getText());
}
}
public static void main(String[] args) {
PopupMenu frame = new PopupMenu();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询