java高手,这个Java程序中的复制与粘贴功能怎样修改才能实现

2024-12-16 00:40:15
推荐回答(1个)
回答1:

import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class lianxi1 extends JFrame {
JPanel n, n5, n6;
JButton n1, n2, n7, n8;
TextField n3;
TextArea n4;
String str;

public lianxi1() {
super("我的练习");
this.setSize(600, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n = new JPanel();
this.add(n);

n5 = new JPanel();
n3 = new TextField(20);
n1 = new JButton("发送");
n2 = new JButton("删除");
n7 = new JButton("复制");
n8 = new JButton("粘贴");

n5.add(n3);
n5.add(n1);
n5.add(n2);
n5.add(n7);
n5.add(n8);
n.add(n5);

n6 = new JPanel();
n4 = new TextArea();
n6.add(n4);
n.add(n6);
n2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n4.setText("");
n4.repaint();
}
});
n2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n3.setText("");
n3.repaint();
}
});

n1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == n1) {
if (!"".equals(n3.getText())) {
n4.setText(n3.getText());
}
}
}

});
n7.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
str = n3.getText();
JOptionPane.showMessageDialog(null, "已复制成功!!!");
}
});
n8.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
n3.setText(null);
n4.setText(str);
}
});

}

public static void main(String[] args) {
new lianxi1();
}
}