我正在开发一个JDialog,我希望调用visible方法后它弹出并等待我点击按钮,然后返回我点击的按钮
这是我现在所使用的代码
package window;
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.CountDownLatch;
public class JD_ask extends JDialog {
private final JLabel text_Label = new JLabel();
private int choice = 0;
private CountDownLatch latch;
public JD_ask(Window window) {
super(window);
Container container = getContentPane();
JButton ok_Button = new JButton("好的");
JButton cancel_Button = new JButton("取消");
setSize(250, 200);
setLayout(null);
text_Label.setBounds(20, 20, 250, 50);
ok_Button.setBounds(40, 130, 80, 30);
cancel_Button.setBounds(150, 130, 80, 30);
container.add(cancel_Button);
container.add(ok_Button);
container.add(text_Label);
cancel_Button.addActionListener(e -> {
setVisible(false);
choice = 0;
latch.countDown();
});
ok_Button.addActionListener(e -> {
setVisible(false);
choice = 1;
latch.countDown();
});
}
public int visible(String title) {
latch = new CountDownLatch(1);
text_Label.setText(title);
setVisible(true);
try {
latch.await();
} catch (InterruptedException ignored) {}
return choice;
}
}
但是,当我调用visible方法后,虽然确实有窗口弹出,但没有显示按钮和文字
现在基本可以确定问题出在这行代码
latch.await();
我也尝试过将它改为
while (isVisible()){
Thread.onSpinWait();
}
但没有改变
我应该怎么修改