如何减慢java中特定方法的执行速度

发布于 2025-01-04 01:24:35 字数 1121 浏览 0 评论 0原文

我的目标是让我编写的方法(更新到 swing 模块)在循环中以 500 毫秒的延迟运行。例如,我的循环大致应如下所示:

public final void doBubbleSort(String numbers[], JButton numButton[]){
for (int k = 0; k < numbers.length - 1; k++)  {  
  String str1 = "";
  boolean isSorted = true;  

  for (int i = 1; i < numbers.length - k; i++){  
     if (Integer.parseInt(numbers[i]) < Integer.parseInt(numbers[i - 1])  ){
        String tempVariable = numbers[i];  
        numbers[i] = numbers[i - 1];  
        numbers[i - 1] = tempVariable;  
        isSorted = false; 
        str1 = numButton[i].getText();
        numButton[i].setBackground(Color.RED);           
        numButton[i-1].setBackground(Color.RED);

        //Pause here for 500 ms

        numButton[i].setText(numButton[i-1].getText());
        numButton[i-1].setText(str1);
        numButton[i].setBackground(null);           
        numButton[i-1].setBackground(null);

     }   
  }  

  if (isSorted)  
     break;  
}  

}

编辑:为了更好地阐明我的目标:我的目标是通过将颜色更改为红色来突出显示将在冒泡排序中交换的两个数字,等待 0.5 秒,然后交换它们并将它们的颜色返回为 null(此后我修改了代码,将颜色更改为 null,而不是像以前那样更改为 Color.WHITE)。抱歉造成混乱。

It's my goal to make a method I have written (that updates to a swing module) run with a 500ms delay in the loop. For example, here's roughly what my loop should look like:

public final void doBubbleSort(String numbers[], JButton numButton[]){
for (int k = 0; k < numbers.length - 1; k++)  {  
  String str1 = "";
  boolean isSorted = true;  

  for (int i = 1; i < numbers.length - k; i++){  
     if (Integer.parseInt(numbers[i]) < Integer.parseInt(numbers[i - 1])  ){
        String tempVariable = numbers[i];  
        numbers[i] = numbers[i - 1];  
        numbers[i - 1] = tempVariable;  
        isSorted = false; 
        str1 = numButton[i].getText();
        numButton[i].setBackground(Color.RED);           
        numButton[i-1].setBackground(Color.RED);

        //Pause here for 500 ms

        numButton[i].setText(numButton[i-1].getText());
        numButton[i-1].setText(str1);
        numButton[i].setBackground(null);           
        numButton[i-1].setBackground(null);

     }   
  }  

  if (isSorted)  
     break;  
}  

}

Edit: To better clarify my goals: My aim is to highlight the two numbers about to be swapped in a bubble sort by changing their color to red, waiting .5s then swapping them and returning their color to null(I have revised the code since to change the color to null, not to Color.WHITE as it was before). Sorry for the confusion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2025-01-11 01:24:35

使用 javax.swing.Timer 类以指定的时间间隔执行事件。该机制将确保 Swing 组件在事件分派线程中被修改。

Use the javax.swing.Timer class to execute events at a specified interval. This mechanism will ensure that the Swing components are modified in the event-dispatching thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文