Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从事件中动态生成文本?_Java_Dynamic - Fatal编程技术网

Java 如何从事件中动态生成文本?

Java 如何从事件中动态生成文本?,java,dynamic,Java,Dynamic,你好,我做了一个代码,可以下载一个应用程序,一切正常 我的问题是我想在框架上显示“请等待,直到jar下载完成…50%完成”我希望50%的内容随着正在下载的文件左边的%而改变 我已经准备好了,但改变的部分是不起作用的部分 这是我的密码: while(( length = inputstream.read(buffer)) > -1) { down += length; bufferedoutputstream.write(buffer

你好,我做了一个代码,可以下载一个应用程序,一切正常

我的问题是我想在框架上显示“请等待,直到jar下载完成…50%完成”我希望50%的内容随着正在下载的文件左边的%而改变

我已经准备好了,但改变的部分是不起作用的部分

这是我的密码:

        while(( length = inputstream.read(buffer)) > -1)
    {
        down += length;

        bufferedoutputstream.write(buffer, 0 , length);
        String text = clientDL.label1.getText();
        text +=  getPerc() + "%";
    }
下面是我的getPerc()方法:


谢谢。

您这里的主要问题是Java不能处理类似指针的逻辑,因此使用getter获取变量,然后为该变量分配新值不会有任何作用

你要找的是

clientDL.label1.setText("Whatever text you want to put in the label");

(对于记录,您可能希望为该标签定义一个getter,而不是直接访问label1,这是一种糟糕的做法)

您希望使用回车符

System.out.print(“10%\r”)


编辑:你没有使用标准输出,我的错。参见silverlord的回答

我认为应该大致是这样的:

 String text = clientDL.label1.getText();
 while(( length = inputstream.read(buffer)) > -1){
    down += length;

    bufferedoutputstream.write(buffer, 0 , length);

    SwingUtilities.invokeLater(new Runnable(){
       public void run(){
          clientDL.label1.setText(text +  getPerc() + "%");
       }
    });
}
(我假设您下载的文件是在一个非AWT事件队列的线程中)

我没有足够的声誉发表评论,我无法为您提供解决方案向您的问题添加评论。无论如何,我尝试过类似的场景
我认为您的clientDL.label1.getText()有问题;就像Silverlord建议的解决方案一样。请参考Silverlord的回答
公共类检查器{
整数长度=0;
InputStreamReader inputstream;
FileInputStream fis=null;
public void display()引发IOException
{
fis=新文件输入流(“C:\\Users\\298610\\Documents\\test.txt”);
inputstream=新的InputStreamReader(fis);
int i=0;
而((length=inputstream.read())>-1)
{
如果(i回答

“请等待jar完成下载…50%完成”我想要 要更改的50%将与正在下载的文件的左侧百分比一起更改。

while((长度=inputstream.read(缓冲区))>-1)
{
向下+=长度;
写(缓冲区,0,长度);
String text=clientDL.label1.getText();
int perc=getPerc();
如果(perc)您想将(每)%完成更改为(每)%完成50%后的剩余值吗?
 String text = clientDL.label1.getText();
 while(( length = inputstream.read(buffer)) > -1){
    down += length;

    bufferedoutputstream.write(buffer, 0 , length);

    SwingUtilities.invokeLater(new Runnable(){
       public void run(){
          clientDL.label1.setText(text +  getPerc() + "%");
       }
    });
}
I dont have enough reputation to comment i am unable to provide you solution adding comment to your question. Anyway i tried similar scenario
I think there is a problem with your clientDL.label1.getText(); just like what solutions suggested by Silverlord. Please refer Silverlord answer


public class Checker {
int length = 0;
InputStreamReader inputstream;
FileInputStream fis = null;

    public void display() throws IOException
    {
         fis = new FileInputStream("C:\\Users\\298610\\Documents\\test.txt");
        inputstream = new InputStreamReader(fis);
        int i=0;
        while(( length = inputstream.read()) > -1)
            {
               if(i<getPerc().length)
               {
                String text = null;
                text = getPerc()[i] + "%";
                System.out.println("Hi man "+text);
               }
                i++;
            }
    }

     private  int[] getPerc() {

            return  new int[]{10,20,30,40,50,60,70,80,90,100};
        }

     public static void main(String a[]) throws IOException
     {
         Checker w =new Checker();
         w.display();
     }
}
while(( length = inputstream.read(buffer)) > -1)
               {
            down += length;
                    bufferedoutputstream.write(buffer, 0 , length);
            String text = clientDL.label1.getText();
             int perc = getPerc();

     if(perc <= 50)        
    {
    text +=  getPerc() + "% done";
    }else
    {
     text ="Please wait until the jar is downloading..."
     text = text + (100 - perc) + " % remaining"
    } 
       }