Java Swing 如何实现记事本中“编辑”菜单下的 剪切,复制,粘贴,删除,全选 功能

时间:2024-01-03 16:56:44

这篇文字将要学习以下知识点:

1.如何给JButton按钮添加鼠标点击事件监听器

#1.addMouseListener(MouseListener l)  给JButton添加一个鼠标点击监听器l

2.文本区控件JTextArea 中的方法(剪切,复制,粘贴,删除,全选  功能的实现)

#1.cut()  先在文本区控件中选中一段文字,然后调用此方法就可以将文字#剪切#到剪贴板(效果和windows中的剪切一模一样)。

#2.copy() 先在文本区控件中选中一段文字,然后调用此方法就可以将文字#复制#到剪贴板(效果和windows中的剪切一模一样)。

#3.paste() 随便复制一段文字(例如从网页中),然后选中文本区,再调用此方法,就可以将刚才复制的文字复制到文本区中

#4.replaceSelection(String content) 先在文本区中选定一段文字,然后调用此方法就可以将选中的文字替换为content

#5.selectAll() 选中全部文字,和windows中的ctrl+a效果一样

1.如何给JButton按钮添加鼠标点击事件监听器

先弄一个这种样子的对话框出来

Java Swing 如何实现记事本中“编辑”菜单下的 剪切,复制,粘贴,删除,全选 功能

然后查看源码中的initialize()方法大概是这个样子的:

 private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //创建一个显示文本为New button的按钮
JButton NewButton = new JButton("New button");
//设置按钮的位置和长宽属性
NewButton.setBounds(28, 138, 117, 129);
frame.getContentPane().add(NewButton);
}

接下来给按钮NewButton添加一个鼠标点击事件监听器:在NewButton 上右键-Add event handler -mouse-mouseClicked 。完成之后NewButton的鼠标点击事件监听器就添加成功了

Java Swing 如何实现记事本中“编辑”菜单下的 剪切,复制,粘贴,删除,全选 功能

然后查看源码,你会发现initialize()方法变成了大概这个样子:

 private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //创建一个显示文本为New button的按钮
JButton NewButton = new JButton("New button"); //给NewButton添加鼠标点击监听器
NewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//当NewButton被点击时,mouseClicked()方法中的代码会被执行
}
}); //设置按钮的位置和长宽属性
NewButton.setBounds(28, 138, 117, 29);
frame.getContentPane().add(NewButton);
}

其中这一部分就是“鼠标点击监听器”。这里你也许会产生一个问题,使用new关键字的时候都是比如这样的:A a = new A();,为啥这里new MouseAdapter()以后还跟了一个大括号,大括号里面还有一个方法呢?这种叫做匿名内部类,也就是一个没有名字的类。彻底搞懂上面那一段代码需要先了解以下知识点:

1.抽象类

2.接口

3.匿名内部类

在《Java疯狂讲义》中有对应的章节,等你学完上面的3个板块以后我在出个上面代码解释的详细版本给你。这里你只要把它理解为一个“鼠标点击监听器”就好了。

 new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//当NewButton被点击时,mouseClicked()方法中的代码会被执行
}
}

JButton通过它的addMouseListener()方法给自己添加了上面那个监听器。这个监听器的作用是:当鼠标点击这个按钮时:mouseClicked()方法中的代码会被执行。这就给了你机会,让你可以写自己的代码来响应点击事件。将mouseClicked()方法修改为:

 public void mouseClicked(MouseEvent e) {
//当NewButton被点击时,mouseClicked()方法中的代码会被执行
System.out.println("按钮被点击了!");
}

当你点击按钮时,控制台就会输出:按钮被点击了!

2.文本区控件JTextArea 中的方法(剪切,复制,粘贴,删除,全选  等功能的实现) 

下面这个程序中包含一个可输入的文本区+5个按钮。每个按钮都被添加了鼠标点击事件。

 package swing;

 import java.awt.EventQueue;

 import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; public class MyEdit { private JFrame frame;
private JTextArea textArea; /**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyEdit window = new MyEdit();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the application.
*/
public MyEdit() {
initialize();
} /**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //创建一个显示文本为New button的按钮
JButton cutButton = new JButton("剪切"); //给NewButton添加鼠标点击监听器
cutButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//当NewButton被点击时,mouseClicked()方法中的代码会被执行 }
}); //设置按钮的位置和长宽属性
cutButton.setBounds(6, 121, 117, 29);
frame.getContentPane().add(cutButton); textArea = new JTextArea();
textArea.setBounds(6, 6, 425, 81);
frame.getContentPane().add(textArea); //复制按钮
JButton copyButton = new JButton("复制");
copyButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textArea.copy();
}
});
copyButton.setBounds(6, 163, 117, 29);
frame.getContentPane().add(copyButton); //粘贴按钮
JButton pasteButton = new JButton("粘贴");
pasteButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textArea.paste();
}
});
pasteButton.setBounds(6, 204, 117, 29);
frame.getContentPane().add(pasteButton); //删除按钮
JButton deleteButton = new JButton("删除");
deleteButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//将选中的文字删除掉。请ctrl+单击replaceSelection()方法,查看方法使用详情
textArea.replaceSelection(null);
}
});
deleteButton.setBounds(154, 121, 117, 29);
frame.getContentPane().add(deleteButton); //全选按钮
JButton selectAllButton = new JButton("全选");
selectAllButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textArea.selectAll();
}
});
selectAllButton.setBounds(154, 163, 117, 29);
frame.getContentPane().add(selectAllButton);
}
}

剪切按钮:选中文本中输入的一段问题,然后单击此按钮,选中的文字就被复制到了剪贴板

复制按钮:和剪切按钮类似

粘贴按钮:随便在网页上复制一段文字,然后选中文本区,然后按粘贴按钮,在网页上复制的文字就被粘贴到文本区中了

删除按钮:选中文本区中的一段文字,然后点击此按钮,选中的文字就被删除了

全选按钮:点击此按钮,文本区中的所有文字就会被选中

那个记事本程序中的剪切,复制,粘贴,删除,全选功能就是上面那样实现的。把上面的代码看懂以后,就又懂了20%咯。

作业:

1.设计一个程序,要求 : 包含2个按钮A B,一开始点击B按钮什么反应都没有,然后点击A按钮之后再来点击B按钮,让控制台输出:“嘻嘻嘻嘻”。

2.设计一个程序,要求:

1.包含3个按钮A B C

2.A按钮点击后能给B按钮设置一个鼠标点击监听器,让B按钮被点击后输出“AAAA”

3.C按钮点击后能给B按钮设置一个鼠标点击监听器,让B按钮被点击后输出“CCCC”