按键时,自动将字符串数组逐个设置到剪贴板 [英] Auto set array of string to clipboard one by one when pressing key

查看:82
本文介绍了按键时,自动将字符串数组逐个设置到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我复制文本(由制表符分隔)时,我的程序会分割复制的文本并存储在字符串数组中。当我按下按钮时,它将逐个显示文本数组并显示并将文本设置为剪贴板。我能够显示文本但无法在剪贴板中设置。任何人都可以更正我的代码。我的代码如下。提前谢谢。

 import java.awt。*; 
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.event。*;
import javax.swing。*;

class MyProj {
public static void main(String [] args)throws Exception {

Clipboard clipboard = Toolkit.getDefaultToolkit()。getSystemClipboard();
String result =(String)clipboard.getData(DataFlavor.stringFlavor);

StringSelection stringSelection = new StringSelection(aa [i]);
clipboard.setContents(stringSelection,null);

Buttonpanel panela = new Buttonpanel();
panela.getstring(result);

JFrame Frame = new JFrame(hiiiiii);
Buttonpanel面板= new Buttonpanel();

Frame.add(面板);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setBounds(700,300,300,200);
Frame.setVisible(true);
}
}

class Buttonpanel扩展JPanel实现ActionListener {

JButton按钮;
JLabel标签;

static String [] aa;
String bb =;
static int i = 0;

Buttonpanel()
{
button = new JButton(Push);
add(button);
button.addActionListener(this);
label = new JLabel(hiiii);
add(label);
}

void getstring(String bb)
{
aa = bb.split();
}

public void actionPerformed(ActionEvent e)
{
label.setText(aa [i]);
System.out.println(+ i); //循环在这里工作
i ++;
}
}





我的尝试:



i能够显示文本但无法在剪贴板中设置。任何人都可以更正我的代码。我的代码如下。提前谢谢。

解决方案

那是因为你只在主方法中将一个字符串设置到剪贴板中:

 StringSelection stringSelection =  new  StringSelection(aa [i]); 
clipboard.setContents(stringSelection,null);



您需要等到收集到数组中的所有文本,然后将其转换为可以发送到剪贴板的表单。 / BLOCKQUOTE>

When i copy a texts (separated by tab), my program split the copied texts and store in the array of string. when i press the button it will display the array of text one by one and display along with set the text to clipboard. i am able to display text but not able to set in the clipboard. Can any one correct my code. my code is given below. thanks in advance.

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import javax.swing.*;

class MyProj {
public static void main(String [] args) throws Exception {

	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	String result = (String) clipboard.getData(DataFlavor.stringFlavor);
	
	StringSelection stringSelection = new StringSelection (aa[i]);
	clipboard.setContents (stringSelection, null);
	
    Buttonpanel panela = new Buttonpanel();
    panela.getstring(result);

    JFrame Frame = new JFrame("hiiiiii");
    Buttonpanel panel = new Buttonpanel();
    
    Frame.add(panel);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setBounds(700,300,300,200);
    Frame.setVisible(true);
	}
}

class Buttonpanel extends JPanel implements ActionListener {

JButton button;
JLabel label;

static String[] aa ;
String bb = "";
static int i = 0;

Buttonpanel()
{
    button = new JButton("Push");
    add(button);
    button.addActionListener(this);
    label = new JLabel("hiiii");
    add(label);
}

void getstring(String bb)
{
	aa = bb.split("	");	
}

public void actionPerformed(ActionEvent e)
{
    label.setText(aa[i]);
   System.out.println(""+ i );  //loop working here
	i ++;
}
}



What I have tried:

i am able to display text but not able to set in the clipboard. Can any one correct my code. my code is given below. thanks in advance.

解决方案

That's because you only set one string into the clipboard in your main method thus:

StringSelection stringSelection = new StringSelection (aa[i]);
clipboard.setContents (stringSelection, null);


You need to wait until you have collected all the text in the array, and then convert it into a form that you can send to the clipboard.


这篇关于按键时,自动将字符串数组逐个设置到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆