把 paste()方法覆盖掉import java.awt.BorderLayout;import javax.swing.*;public class T { public static void main(String[] args) { JTextField tf = new JTextField() { public void paste() { } };
JTextArea ta = new JTextArea() { public void paste() { } };
JFrame f = new JFrame(); f.getContentPane().add(tf, BorderLayout.NORTH); f.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER); f.setSize(300, 400); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
楼上,Ctrl+V是不需要实现的,系统自动会带这个功能。
如果你是想要在文本组件中实现右键功能时带有Ctrl+V
网上倒是有很多例子!
public static void main(String[] args) {
JTextField tf = new JTextField() {
public void paste() {
}
};
JTextArea ta = new JTextArea() {
public void paste() {
}
};
JFrame f = new JFrame();
f.getContentPane().add(tf, BorderLayout.NORTH);
f.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
f.setSize(300, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}