【问题标题】:Swing window doesn't return value after x time摆动窗口在 x 时间后不返回值
【发布时间】: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;

    }

我很想知道这是如何工作的,以及如何消除这个可怕的代码。

【问题讨论】:

    标签: java swing timer


    【解决方案1】:

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

    这是我创建的 GUI JFrame

    这是我创建的JDialog

    JDialog 将在 15 秒后释放。如果呼叫被接听,isAnswered 方法将返回 true,如果呼叫被拒绝,JDialog 已通过左键单击 X 或 15 秒结束时关闭,则返回 false。

    这是完整的可运行代码。

    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();
            }
            
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多