简体   繁体   English

Java Swing如何使这个计数器工作?

[英]Java Swing how can I make this counter work?

Everytime my counter reaches 4 I want it to play a beep sound and go back to '1' and again count up to 4 play the beep sound and so on. 每当我的计数器达到4时,我希望它发出哔哔声并返回“1”并再次计数4次播放哔声等等。

I probably shouldn't place this in a label, because the counter doesnt run at all! 我可能不应该将它放在标签中,因为计数器根本不运行! I don't get any errors but the label says; 标签上写道,我没有任何错误; counter is 4 and doesnt count or anything. 计数器是4并且不计数或任何东西。

Can you help me make this counter work properly? 你能帮我让这个柜台正常工作吗? I also used printline but that gave some errors too. 我也使用过printline,但也有一些错误。

My code for the counter is this: 我的柜台代码是这样的:

button1.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent arg0) {

    label1.setVisible(true);
    int counter = 1;

    while(counter < 5 )
    {
      label1.setText("counter  is " + counter);
      counter = counter + 1 ;
    }

    counter = 1;
    tk.beep();
  }
});

Spawn a new thread to count, wait, and update the GUI. 生成一个新线程来计算,等待和更新GUI。

You're doing all of this work in the Event Dispatch Thread, which is the only thread which updates the GUI. 您正在Event Dispatch Thread中完成所有这些工作,这是唯一更新GUI的线程。 So when you set the text of the label, it doesn't get updated on the screen until the method returns and the Event Dispatch Thread handles the repaint operation. 因此,当您设置标签的文本时,它不会在屏幕上更新,直到方法返回并且事件调度线程处理重绘操作。

You need to spawn a new thread to do this, rather than just running it in a loop which executes immediately. 您需要生成一个新线程来执行此操作,而不是仅在立即执行的循环中运行它。 Just have the actionPerformed method spawn a new Thread which handles this instead. 只需让actionPerformed方法生成一个新的Thread来处理它。 Loop, count, and update in this thread, waiting with Thread.sleep between updates. 在此线程中循环,计数和更新,在更新之间等待Thread.sleep To update the label text, create a new Runnable that will update the label to the next value and put it on the Event Dispatch Thread with SwingUtilities.invokeLater . 要更新标签文本,请创建一个新的Runnable ,它将标签更新为下一个值,并将其放在带有SwingUtilities.invokeLater的Event Dispatch Thread上。 Keep this thread running in the background until you need it. 保持此线程在后台运行,直到您需要它为止。 I would recommend checking a shutdown status boolean every loop through, and quitting when it's set to false. 我建议每次循环检查关闭状态boolean,并在设置为false时退出。 This way, you can shut down the thread cleanly at any time. 这样,您可以随时干净地关闭线程。 Or, if you want it to countdown and beep only once, you can have the thread just end after one iteration of counting. 或者,如果你想让它倒计时并且只发出一次哔哔声,那么你可以在一次迭代计数之后让线程结束。

There are lots of questions on Stack Overflow that detail each of these steps, so I won't repeat this information here. Stack Overflow上有很多关于每个步骤的问题,所以我不会在这里重复这些信息。

After changing the value you need to repaint. 更改值后需要重新绘制。 Also, I assume you actually want to count the seconds in which case you need to use a Timer to kick off the action of changing the label and possibly playing a sound. 此外,我假设您实际上想要计算秒数,在这种情况下您需要使用Timer来启动更改标签和可能播放声音的操作。

Maybe this is does what you intened, every 4th button press it resets and beeps 也许这就是你所设想的,每按一下按钮就会重置并发出哔哔声

loginButton.addActionListener(new java.awt.event.ActionListener() {

    int counter = 1;

    public void actionPerformed(ActionEvent arg0) {

        label1.setVisible(true);

        if (counter < 5) {
            label1.setText("counter  is " + counter);
            label1.repaint();
            ++counter;
        } else {
            counter = 1;
            tk.beep();
        }
    }
});

If I understand correctly what you want , the below code should accomplish your goal. 如果我正确理解了您的需求,下面的代码应该可以实现您的目标。

button1.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent arg0) {

    label1.setVisible(true);

    Runnable runnable = new Runnable() {
           int counter =1 ;

           public void run() {
               while(true) {

                    while (counter<5)  {
                        SwingUtilties.invokeLater(new Runnable() {
                               public void run() {
                                   label1.setText("counter  is " + counter);
                               }
                        }); 

                        counter = counter + 1 ;
                        try {
                           Thread.sleep(1000);
                        }catch(InterruptedException ex) {
                            System.err.println(ex.toString());

                        }    
                    } 
                counter = 1;
                tk.beep();

                }

           }
    };

     new Thread(runnable).start();

});

Your loop stops after the first 4 and never called again. 您的循环在前4个循环后停止,并且从未再次调用。 since the text is changing to fast, you can only see the last result 由于文本正在变为快速,因此您只能看到最后的结果

int counter = 0;
while (//when do you want it to stop?)
{
    // print what you want (using (counter % 4) + 1)
    if ((counter % 4) == 0)
    {
         tk.beep();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM