gpt4 book ai didi

java - java中的剪贴板监听器

转载 作者:行者123 更新时间:2023-12-01 12:50:40 28 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序来监听剪贴板副本并保存它们。我编写了以下代码:

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)

出于某种原因,Eclipse 没有显示任何错误,但是当我点击“运行”按钮时,我收到一些编译错误。有人知道是什么原因造成的吗?

最佳答案

这是一篇旧帖子,但我将为搜索相同内容的其他人回答它。正确的方法是在定义 FlavorListener 之前初始化剪贴板。因此“MyPanel”构造函数必须正确更新:

public MyPanel(){
this.setLayout(new BorderLayout());

text = new TextArea();
text.setFont(f);
add(BorderLayout.CENTER,text);
add(BorderLayout.SOUTH,this.getSouthButton());

c = Toolkit.getDefaultToolkit().getSystemClipboard();
c.addFlavorListener(new FlavorListener() {

@Override
public void flavorsChanged(FlavorEvent e) {
proccessClipboard(c);

}
});
}

关于java - java中的剪贴板监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233736/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com