java右键菜单_Java弹出菜单,为JTextArea添加了右键弹出式菜单,实现了复制、粘贴、剪切 | 学步园...

这篇博客展示了如何在Java中为JTextArea添加右键弹出菜单,实现文本的复制、粘贴和剪切功能。通过创建JPopupMenu和JMenuItem,结合MouseListener和ActionListener,当用户点击右键时,菜单会显示并允许用户执行相应的操作。同时,代码检查了剪贴板是否有文本数据以及文本组件中是否有选定的内容,以启用或禁用相应的菜单项。
摘要由CSDN通过智能技术生成

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.DataFlavor;

import java.awt.datatransfer.Transferable;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.InputEvent;

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.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.KeyStroke;

import javax.swing.UIManager;

/**

* Java右键菜单实现文本组件内容的的复制、粘贴、剪切功能

*

* @author 五斗米

* @blog http://blog.csdn.net/mq612

*/

public class Test extends JFrame {

private static final long serialVersionUID = -5942087991012920147L;

private JScrollPane pane = null;

private TextAreaMenu text = null;

public Test() {

super("右键菜单");

try { // 使用Windows的界面风格

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

} catch (Exception e) {

e.printStackTrace();

}

text = new TextAreaMenu();

pane = new JScrollPane(text);

this.getContentPane().add(pane);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(300, 200);

this.setVisible(true);

}

public static void main(String args[]) {

new Test();

}

/**

* 带有功能菜单的JTextArea

*

* @author 五斗米

* @blog http://blog.csdn.net/mq612

*/

class TextAreaMenu extends JTextArea implements MouseListener {

private static final long serialVersionUID = -2308615404205560110L;

private JPopupMenu pop = null; // 弹出菜单

private JMenuItem copy = null, paste = null, cut = null; // 三个功能菜单

public TextAreaMenu() {

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("剪切"));

copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));

paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));

cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));

copy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

action(e);

}

});

paste.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

action(e);

}

});

cut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

action(e);

}

});

this.add(pop);

}

/**

* 菜单动作

* @param e

*/

public void action(ActionEvent e) {

String str = e.getActionCommand();

if (str.equals(copy.getText())) { // 复制

this.copy();

} else if (str.equals(paste.getText())) { // 粘贴

this.paste();

} else if (str.equals(cut.getText())) { // 剪切

this.cut();

}

}

public JPopupMenu getPop() {

return pop;

}

public void setPop(JPopupMenu pop) {

this.pop = pop;

}

/**

* 剪切板中是否有文本数据可供粘贴

*

* @return true为有文本数据

*/

public boolean isClipboardString() {

boolean b = false;

Clipboard clipboard = this.getToolkit().getSystemClipboard();

Transferable content = clipboard.getContents(this);

try {

if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {

b = true;

}

} catch (Exception e) {

}

return b;

}

/**

* 文本组件中是否具备复制的条件

*

* @return true为具备

*/

public boolean isCanCopy() {

boolean b = false;

int start = this.getSelectionStart();

int end = this.getSelectionEnd();

if (start != end)

b = true;

return b;

}

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) {

copy.setEnabled(isCanCopy());

paste.setEnabled(isClipboardString());

cut.setEnabled(isCanCopy());

pop.show(this, e.getX(), e.getY());

}

}

public void mouseReleased(MouseEvent e) {

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你提供一些思路,但是要注意的是,由于我是语言模型,并不能实际编写代码。你需要自己动手实现。 首先,你需要选择一个 Java GUI 库,比如 Swing 或 JavaFX。这里以 Swing 为例。 1. 创建一个 JFrame 对象,设置窗口的标题和大小。 2. 创建一个 JTextArea 对象,用于显示和编辑文本内容。 3. 在 JTextArea添加一个 JPopupMenu,用于实现复制粘贴剪切等操作。 4. 在 JPopupMenu 上添加 JMenuItem,分别对应复制粘贴剪切操作。 5. 为 JMenuItem 添加 ActionListener,实现对应功能。 下面是具体代码实现的示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NotePad extends JFrame implements ActionListener { private JTextArea textArea; public NotePad() { super("记事本"); setSize(500, 500); textArea = new JTextArea(); add(new JScrollPane(textArea), BorderLayout.CENTER); JPopupMenu popupMenu = new JPopupMenu(); JMenuItem copyItem = new JMenuItem("复制"); JMenuItem pasteItem = new JMenuItem("粘贴"); JMenuItem cutItem = new JMenuItem("剪切"); copyItem.addActionListener(this); pasteItem.addActionListener(this); cutItem.addActionListener(this); popupMenu.add(copyItem); popupMenu.add(pasteItem); popupMenu.add(cutItem); textArea.setComponentPopupMenu(popupMenu); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("复制")) { textArea.copy(); } else if (e.getActionCommand().equals("粘贴")) { textArea.paste(); } else if (e.getActionCommand().equals("剪切")) { textArea.cut(); } } public static void main(String[] args) { NotePad notepad = new NotePad(); notepad.setVisible(true); } } ``` 这样,一个简单的记事本程序就完成了。你可以在此基础上进一步添加其他功能,比如打开、保存文件,设置字体和颜色等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值