我正在尝试编写一个简单的程序来收听剪贴板copys并保存它们。 我写了以下代码:
package CopyPaste;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("Copy Paste");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
MyPanel pane = new MyPanel();
frame.add(pane);
frame.setVisible(true);
}
}
package CopyPaste;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.lang.Object;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ClipboardOwner{
private final static Font f = new Font("david", Font.BOLD,22);
private static TextArea text;
private JButton btnGet;
private Clipboard c;
public MyPanel(){
this.setLayout(new BorderLayout());
text = new TextArea();
text.setFont(f);
add(BorderLayout.CENTER,text);
add(BorderLayout.SOUTH,this.getSouthButton());
c.addFlavorListener(new FlavorListener() {
@Override
public void flavorsChanged(FlavorEvent e) {
proccessClipboard(c);
}
});
}
private static void proccessClipboard(Clipboard c){
String s = null;
c = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
s = (String) c.getContents(null).getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
text.setText(s);
}
//return the south panel button
private JPanel getSouthButton(){
JPanel south = new JPanel();
btnGet = new JButton("Get");
ButtonLis lis = new ButtonLis();
btnGet.addActionListener(lis);
south.add(btnGet);
return south;
}
private class ButtonLis implements ActionListener{
public void actionPerformed(ActionEvent e) {
String s = null;
if (e.getSource()==btnGet){
c = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
s = (String) c.getContents(null).getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
text.setText(s);
}
}
}
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
// TODO Auto-generated method stub
}
}
error:
Exception in thread "main" java.lang.NullPointerException
at CopyPaste.MyPanel.<init>(MyPanel.java:39)
at CopyPaste.Main.main(Main.java:13)
出于某种原因,日食不显示任何错误,但当我点击&#34;运行&#34;按钮我得到一些编译错误。 有人知道是什么原因引起的吗?
答案 0 :(得分:1)
这是一篇很老的帖子,但我会为其他搜索相同内容的人回答这个问题。正确的方法是在定义FlavourListener之前初始化剪贴板。所以&#34; MyPanel&#34;构造函数必须正确更新:
error
答案 1 :(得分:0)
您的错误发生在Panel类的第39行,因为错误消息中的这一行告诉您:
at CopyPaste.MyPanel.<init>(MyPanel.java:39)
这一行:
c.addFlavorListener(new FlavorListener() {
因此,当您尝试向其添加FlavorListener时,剪贴板变量c为null,并且在尝试使用它之前必须初始化它。