【发布时间】:2021-02-15 15:22:01
【问题描述】:
我正在尝试在 Swing 中做一个呼叫模拟器,但由于某种原因,窗口不会移动。
我的目标是打开一个单独的窗口,如果按下按钮或按下另一个按钮,则返回 true 或 false。用户应该有 15 秒的时间做某事,否则返回 false 并执行操作。
当它返回时,它应该完成它的执行。这个函数被main的另一个函数部分调用。
但是,由于我缺乏经验和愚蠢,它不起作用。相反,它会打开一个不会自行关闭的窗口(空白)...
代码如下:
public static boolean callWindow(Telephone src, Telephone dst) {
Timer t1 = new Timer(15000, e-> {
DateTime date = new DateTime();
Call c = new Call(date, src.getTelephone(), dst.getTelephone(), 0);
dst.addLostCall(c);
});
//Janela para atender
JFrame window = new JFrame();
window.setTitle("Incoming Call");
JLabel telephones = new JLabel();
telephones.setText("From: " + src.getTelephone() + " To: " + dst.getTelephone() );
JButton answerCall = new JButton("Answer");
JButton denyCall = new JButton("Decline");
JLabel status = new JLabel();
answerCall.addActionListener( e -> status.setText("answer") );
denyCall.addActionListener( e -> status.setText("no answer") );
answerCall.setBackground(Color.GREEN);
denyCall.setBackground(Color.RED);
JPanel callInfo = new JPanel();
callInfo.add(telephones);
JPanel callOptions = new JPanel();
callOptions.add(answerCall);
callOptions.add(denyCall);
Container container = window.getContentPane();
container.add(callInfo, BorderLayout.NORTH);
container.add(callOptions);
window.setSize(350,120);
window.setVisible(true);
t1.start();
while (t1.isRunning()) {
if (status.getText().equals("answer")) return true;
if (status.getText().equals("no answer")) return false;
}
return false;
}
我很想知道这是如何工作的,以及如何消除这个可怕的代码。
【问题讨论】: