摆动窗口在 x 时间后不返回值 [英] Swing window doesn't return value after x time

查看:26
本文介绍了摆动窗口在 x 时间后不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Swing 中做一个呼叫模拟器,但由于某种原因,窗口不会移动.

I'm trying to do a call simulator in Swing and for some reason the window won't budge.

我的目标是打开一个单独的窗口,如果按下按钮或按下另一个按钮,则返回 true 或 false.用户应该有 15 秒的时间做某事,否则返回 false 并执行一个操作.

My goal is to open a seperate window that returns true or false if a button is pressed or another has been pressed. The user should have 15 seconds to do something, otherwise it returns false and performs an action.

当它返回时,它应该完成它的执行.这个函数被main的另一个函数部分调用.

When it returns, it should finish it's execution. This function is called by another function part of main.

然而,由于我缺乏经验和白痴,它不起作用.相反,它会打开一个窗口(空白),在它应该...

However, due to my lack of experience and idiocy, it doesn't work. Instead, it opens a window (blank) that never closes by itself when it should...

代码如下:

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;

    }

我很想知道它是如何工作的,以及如何对这个可怕的代码进行 depaghettify.

I would appreciate to know how this could work and how to despaghettify this horrible code.

推荐答案

我使用了一个 JDialog 来演示如何在 15 秒后关闭窗口.

I used a JDialog to demonstrate how to close a window after 15 seconds.

这是我创建的 GUI JFrame.

Here's the GUI JFrame I created.

这是我创建的 JDialog

JDialog 将在 15 秒后处理.isAnswered 方法将在呼叫被接听或呼叫被拒绝、通过左键单击 X 关闭 JDialog 或 15 秒时返回 true用完了.

The JDialog will dispose after 15 seconds. The isAnswered method will return true if the call was answered or false if the call was declined, the JDialog was closed by left-clicking on the X, or when the 15 seconds runs out.

这是完整的可运行代码.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JDialogTimedGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JDialogTimedGUI());
    }
    
    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame("JDialog Timed GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMainPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(
                75, 100, 75, 100));
        panel.setPreferredSize(new Dimension(400, 200));

        JButton button = new JButton("Open JDialog");
        button.addActionListener(new ButtonListener());
        panel.add(button);

        return panel;
    }
    
    public class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            IncomingCall ic = new IncomingCall(frame, "Incoming Call", 
                    "555-1212", "555-2323");
            System.out.println("Incoming call: " + ic.isAnswered());
        }

    }
    
    public class IncomingCall extends JDialog {
        
        private static final long serialVersionUID = 1L;
        
        private boolean answered;
        
        private Timer timer;

        public IncomingCall(JFrame frame, String title, 
                String sourcePhone, String destinationPhone) {
            super(frame, true);
            this.answered = false;
            
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            setTitle(title);

            add(createMainPanel(frame, sourcePhone, destinationPhone));
            pack();
            setLocationRelativeTo(frame);
            
            timer = new Timer(15000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {
                    setVisible(false);
                    dispose();
                    timer.stop();
                }
            });
            timer.start();
            
            setVisible(true);
        }
        
        private JPanel createMainPanel(JFrame frame, 
                String sourcePhone, String destinationPhone) {
            JPanel panel = new JPanel(new BorderLayout(5, 5));
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            
            String s = "From: " + sourcePhone + "    To: " + destinationPhone;
            JLabel telephones = new JLabel(s);
            panel.add(telephones, BorderLayout.BEFORE_FIRST_LINE);
            
            JPanel callOptions = new JPanel();
            
            CallButtonListener listener = new CallButtonListener(this);
            
            JButton answerCall = new JButton("Answer");
            answerCall.addActionListener(listener);
            answerCall.setBackground(Color.GREEN);
            callOptions.add(answerCall);
            
            JButton denyCall = new JButton("Decline");
            denyCall.addActionListener(listener);
            denyCall.setBackground(Color.RED);
            callOptions.add(denyCall);
            
            panel.add(callOptions, BorderLayout.CENTER);
            
            return panel;
        }

        public void setAnswered(boolean answered) {
            this.answered = answered;
        }

        public boolean isAnswered() {
            return answered;
        }
        
    }
    
    public class CallButtonListener implements ActionListener {
        
        private IncomingCall incomingCall;

        public CallButtonListener(IncomingCall incomingCall) {
            this.incomingCall = incomingCall;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            if (button.getText().equals("Answer")) {
                incomingCall.setAnswered(true);
            } else {
                incomingCall.setAnswered(false);
            }
            incomingCall.dispose();
        }
        
    }

}

这篇关于摆动窗口在 x 时间后不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆