2016-06-10 93 views
0

我有一个问题,可能有一个简单的修复,但我似乎无法得到它的工作。制作程序等待,直到点击一个按钮

我需要我的程序暂停或等待,直到用户在继续之前选择跳过按钮或正反馈按钮。 我认为这将需要基本的线程,但我不知道如何实现它。任何帮助将appreacited。

gui显示在一个单独的类(GUI)中,其余的是另一个类。 代码有点乱,因为它在12小时内被编码为Hackfest。

编辑:解决它我自己删除按钮监听器,并使变量静态。

public void onStatus(Status status) { 
//this is a listener from the Twitter4J class. Every time new Tweet comes in it updates. 
      for (int i = 0; i <= posWords.length; i++) { 

       if (status.getText().toLowerCase().contains(gui.sCrit.getText()) 
         && (status.getText().toLowerCase().contains(posWords[i].toLowerCase()))) { 
//If the tweet matches this criteria do the following: 
        String tweet; 
        Status tempStoreP; 
        System.out.println("Flagged positive because of " +posWords[i].toLowerCase()+" " + i); 
        tempStoreP = status; 

        tweet = tempStoreP.getUser().getName() + ":" + tempStoreP.getText() + " | Flagged as positive \n\r"; 
        gui.cTweet.setText(tweet); 
        //Write to log 
        try { 


         wPLog.append("~" + tweet); 


        } catch (IOException e) { 
        } 
        //Add action listeneer to gTweet. 
       //here is my problem. I want it to wait until a user has clicked one of these buttons before moving on and getting the next Tweet. It has to pause the thread until a button is clicked then it can only move on to getting another Tweet. 
//the problem is that the button listener uses local variables to work. 
         gui.gTweet.addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           if (gui.pTweet.getText().equalsIgnoreCase("")) { 
            gui.pTweet.setText("Please type a response"); 
           } else if (gui.pTweet.getText().equalsIgnoreCase("Please type a response")) { 
            gui.pTweet.setText("Please type a response"); 

           } else { 
            try { 
             Status sendPTweet = twitter 
               .updateStatus("@" + tempStoreP.getUser().getScreenName() + " " 
                 + gui.pTweet.getText()); 

            } catch (TwitterException e1) { 
            } 
           } 

           gui.gTweet.removeActionListener(this); 
          } 
         }); 

        //add Aaction listert to sTweet 

         gui.sTweet.addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
           try { 

            wPLog.append("Skipped \n\r"); 

           } catch (IOException e1) { 
           } 
           gui.sTweet.removeActionListener(this); 
          } 
         }); 


       } 

谢谢你的帮助。在附注中,如果任何人都可以告诉我为什么当点击该按钮时,它会循环并用相同的消息来垃圾邮件,这会有所帮助。谢谢。

+3

如果代码是一团糟,那么清理它然后发布。为什么要打扰任何人,即使你不喜欢? – zubergu

+1

是否有按钮启动将用户移动到下一个屏幕而不是选项的操作? – npinti

+1

*“代码有点乱,因为它是在12小时内为Hackfest编码的。”*这是没有任何借口,在将它倾倒在这里之前不要改进它。为了尽快提供更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。顺便说一下,这种类型的问题通常使用模态对话框来处理,但我不能基于可用的有限信息来判断这是否符合要求。 –

回答

0

在我理解你的问题,你可以使用多线程是这样的:

try{ 
    while(!isSkipClicked){ 
    //your multithreading code 
    } 
    //code to move... 
} 

或 您可以使用Dispose方法,如果你有2 JFrame

或 您可以使用多个JPanel像而跳过按钮点击隐藏pnl1和使用方法

pnl1.setvisible(false); pbl2.setVisible(true);

+1

你应该提到他不得不使用多线程,因为他会暂停SWING线程。 – ScriptKiddy

+0

好的我刚才提到 –

1

我想到了多线程的事情表明pnl2,我认为这是不容易的不够。 如果我是你,我会禁用除了点击按钮之外的所有控件。

例子:

JButton button = new JButton("Test); 
button.setEnabled(false); 

用户将无法直到您使用button.setEnabled(true);点击按钮,如果你只是禁用所有控件,但该按钮,应该是罚款。

0

我认为这将需要基本线程,

根本不符合事实。

你说你想让你的程序“等待”。对于Swing应用程序来说,这个词实际上并不重要。 Swing应用程序是事件驱动的。也就是说,你的程序大多由事件处理程序组成,这些事件处理程序由Swing的顶级循环(也就是Event Dispatch Thread或EDT)调用,以响应各种事件,如鼠标点击,键盘事件,定时器等。

在事件驱动的程序中,“等到X”主要是指禁用某些东西,然后在X处理程序中重新启用它。

当然,Swing程序有时会创建其他线程来处理来自GUI框架不知道的来源的输入,并执行“背景”计算。在这种情况下,“等到X”可能意味着发信号通知某个线程暂停,然后发信号通知它在X处理程序中继续工作。

相关问题