简体   繁体   English

实施此操作的正确方法是什么?

[英]What is the correct way to implement this do while?

I start my GUI like this, which seems correct. 我这样启动我的GUI,这似乎是正确的。

  javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame gui = new JFrame();
            gui.setExtendedState(JFrame.MAXIMIZED_BOTH);
            gui.setVisible(true);
        }
    });

At a certain point in the application, the playTurn() method gets fired. 在应用程序中的某个点,将触发playTurn()方法。 The for loops all turns in the list. for循环会在列表中轮流显示。

 for (String turn : controller.getTurns()) {
                playTurn(turn);
            }

I now load the correct panel with my CardLayout which worked fine. 现在,我用CardLayout加载了正确的面板,效果很好。 Then I had to write the playTurn() method. 然后,我不得不编写playTurn()方法。 So playTurn() gets called. 因此playTurn()被调用。 It should do certain things according to some variables. 它应该根据一些变量来做某些事情。 But it should not return until some buttons are disabled. 但是在禁用某些按钮之前,它不应该返回。 This is what I can't achieve, the program just stops working. 这是我无法实现的,程序只是停止工作。 I can guess it's in the direction of threads etc.. but can't seem to figure it out. 我可以猜测它是朝线程等方向发展的。但是似乎无法弄清楚。 Thanks in advance. 提前致谢。

public void playTurn(String turn) {
    if (controller.givePlayers().contains(turn)) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                while (!turnFinished) {
                    if (!button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled()) {
                        turnFinished = true;
                    }
                }
            }
        });
    } else {
        deletePlayer(turn);
    }
}

Sorry for bad formatting. 抱歉,格式化错误。 Couldn't find where. 找不到位置。

EDIT: The GUI stops being responsive. 编辑:GUI停止响应。 Can't close program either. 也无法关闭程序。 I tried using a SwingWorker for the while which does not block the GUI but still playTurn() returns. 我尝试使用SwingWorker一段时间,它不会阻塞GUI,但playTurn()返回。

I have even tried creating a new thread where I call the method. 我什至尝试创建一个新线程来调用该方法。 The doesn't get blocked anymore but the method still returns. 不再被阻止,但方法仍然返回。

Thread one = new Thread() {
                    public void run() {
                        playTurn(turn);
                    }
                };

FIXED: Placing the runnable up higher in the stack; 修正:将可运行物品放到更高的位置;

Your playTurn method runs the code on the EDT , cause of this line javax.swing.SwingUtilities.invokeLater(new Runnable() { , which makes your application GUI unresponsive as GUI-changing code must generally be run on the EDT . Since your buttons won't change from your GUI, once the loop starts, it might just loop forever. 您的playTurn方法在EDT上运行代码,此行的原因是javax.swing.SwingUtilities.invokeLater(new Runnable() { ,这会使您的应用程序GUI无响应,因为更改GUI的代码通常必须在EDT上运行。不会从您的GUI更改,一旦循环开始,它可能永远循环下去。

By running the code in another Thread , you won't freeze your GUI. 通过在另一个Thread运行代码,您将不会冻结GUI。 I'm guessing, since you don't provide much informations on the rest of your code, that you might have to change the way you handle things once your loop is done. 我猜,由于您没有在其余代码中提供太多信息,因此一旦循环完成,您可能必须更改处理事情的方式。

Edit from comments : Since you don't want playTurn to return, don't use a thread within it and make sure playTurn is not running on the EDT . 从注释中进行编辑:由于您不希望playTurn返回,因此请勿在其中使用线程,并确保playTurn不在EDT上运行。 Your playTurn method will return after creating and making a new Thread run the code. 创建并让新Thread运行代码后,您的playTurn方法将返回。

You might want to try dong it like this : 您可能想要尝试像这样:

Runnable code = new Runnable() {
  @Override
  public void run() {
    for (String turn : controller.getTurns()) {
      playTurn(turn);
    }
  }
};

if (!SwingUtilities.isEventDispatchThread()) {
  code.run();
} else {
  new Thread(code).start();
}

To make sure you don't run the code on the EDT . 为了确保您不在EDT上运行代码。 That way, playTurn doesn't return until the loop condition is met, the GUI stays responsive. 这样,直到满足循环条件时, playTurn才返回, GUI保持响应。

public void playTurn(String turn) {
  if (controller.givePlayers().contains(turn)) {
    while (!turnFinished) {
      if (!button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled()) {
        turnFinished = true;
      }
    }
  } else {
    deletePlayer(turn);
  }
}

Doing this might have you change a few things more. 这样做可能会让您做出更多更改。

The idea is to make the call to a new Thread where you don't want it/need it to wait for the code being run in a new Thread to end to continue. 这个想法是在不希望/不需要它的地方调用新Thread ,以等待在新Thread运行的代码结束以继续。

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

相关问题 什么是实现compareObjects()的正确方法 - What is the correct way to implement compareObjects() 正确的方法是什么? - What is the correct way to do this? 正确的方法来实现Java中以前的Mixin? - Correct way to implement what used to be a Mixin in Java? 用Java实现创建者类和类本身设计的正确方法是什么? - What is the correct way to implement a Creator Class and the Class itself design in Java? 进行while循环的正确方法是什么? 我的方式不起作用 - What is the correct way to make a while loop?? my way is not working 使用 gameLoop 时实现多线程的正确方法是什么 - What is the proper way to implement multi threading while using a gameLoop 实现连接池的正确方法 - Correct way to implement a connection pool 正确的方法是什么? 初始化/未找到符号错误 - What is the correct way to do this? Initialization/Symbol not found errors 在Struts 2中使用嵌套域对象进行CRUD的正确方法是什么? - What's the correct way to do CRUD with nested domain objects in Struts 2? 枚举字段为Null时ArrayList的元素。 正确的方法是什么? - Elements of ArrayList in Enum field Null. What is the correct way to do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM