如何暂停程序直到按下按钮? [英] how to pause program until a button press?

查看:68
本文介绍了如何暂停程序直到按下按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从jframe扩展的类中使用它,并且它有一个按钮(我在程序中使用它)

我想在程序中运行jframe时整个程序暂停

直到我按下按钮.

我该怎么办

在c ++ getch()中执行此操作.

我想要一个这样的功能.

解决方案

通过睡眠暂停执行,尽管我怀疑这是您要使用的机制.因此,正如其他人所建议的那样,我相信您需要实现wait-notify逻辑.这是一个非常人为的例子:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class PanelWithButton extends JPanel
{
    // Field members
    private AtomicBoolean paused;
    private JTextArea textArea;
    private JButton button;
    private Thread threadObject;

    /**
     * Constructor
     */
    public PanelWithButton()
    {
        paused = new AtomicBoolean(false);
        textArea = new JTextArea(5, 30);
        button = new JButton();

        initComponents();
    }

    /**
     * Initializes components
     */
    public void initComponents()
    {
        // Construct components
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        add( new JScrollPane(textArea));
        button.setPreferredSize(new Dimension(100, 100));
        button.setText("Pause");
        button.addActionListener(new ButtonListener());
        add(button);

        // Runnable that continually writes to text area
        Runnable runnable = new Runnable()
        {
            @Override
            public void run() 
            {
                while(true)
                {
                    for(int i = 0; i < Integer.MAX_VALUE; i++)
                    {
                        if(paused.get())
                        {
                            synchronized(threadObject)
                            {
                                // Pause
                                try 
                                {
                                    threadObject.wait();
                                } 
                                catch (InterruptedException e) 
                                {
                                }
                            }
                        }

                        // Write to text area
                        textArea.append(Integer.toString(i) + ", ");


                        // Sleep
                        try 
                        {
                            Thread.sleep(500);
                        } 
                        catch (InterruptedException e) 
                        {
                        }
                    }
                }
            }
        };
        threadObject = new Thread(runnable);
        threadObject.start();
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(400, 200);
    }

    /**
     * Button action listener
     * @author meherts
     *
     */
    class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent evt) 
        {
            if(!paused.get())
            {
                button.setText("Start");
                paused.set(true);
            }
            else
            {
                button.setText("Pause");
                paused.set(false);

                // Resume
                synchronized(threadObject)
                {
                    threadObject.notify();
                }
            }
        }
    }
}

这是您的主要课程:

 import javax.swing.JFrame;
    import javax.swing.SwingUtilities;


    public class MainClass 
    {
        /**
         * Main method of this application
         */
        public static void main(final String[] arg)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new PanelWithButton());
                    frame.pack();
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                }
            });
        }
    }

如您所见,该示例应用程序将连续写入文本区域,直到您单击显示为暂停"的按钮,然后才能继续操作,您需要单击相同的按钮,该按钮现在将显示为开始". /p>

i use from a class that extended from jframe and it has a button(i use from it in my program)

i want when run jframe in my program the whole of my program pause

until i press the button.

how can i do it

in c++ getch() do this.

i want a function like that.

解决方案

Pausing Execution with Sleep, although I doubt that is the mechanism that you'll want to use. So, as others have suggested, I believe you'll need to implement wait-notify logic. Here's an extremely contrived example:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class PanelWithButton extends JPanel
{
    // Field members
    private AtomicBoolean paused;
    private JTextArea textArea;
    private JButton button;
    private Thread threadObject;

    /**
     * Constructor
     */
    public PanelWithButton()
    {
        paused = new AtomicBoolean(false);
        textArea = new JTextArea(5, 30);
        button = new JButton();

        initComponents();
    }

    /**
     * Initializes components
     */
    public void initComponents()
    {
        // Construct components
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        add( new JScrollPane(textArea));
        button.setPreferredSize(new Dimension(100, 100));
        button.setText("Pause");
        button.addActionListener(new ButtonListener());
        add(button);

        // Runnable that continually writes to text area
        Runnable runnable = new Runnable()
        {
            @Override
            public void run() 
            {
                while(true)
                {
                    for(int i = 0; i < Integer.MAX_VALUE; i++)
                    {
                        if(paused.get())
                        {
                            synchronized(threadObject)
                            {
                                // Pause
                                try 
                                {
                                    threadObject.wait();
                                } 
                                catch (InterruptedException e) 
                                {
                                }
                            }
                        }

                        // Write to text area
                        textArea.append(Integer.toString(i) + ", ");


                        // Sleep
                        try 
                        {
                            Thread.sleep(500);
                        } 
                        catch (InterruptedException e) 
                        {
                        }
                    }
                }
            }
        };
        threadObject = new Thread(runnable);
        threadObject.start();
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(400, 200);
    }

    /**
     * Button action listener
     * @author meherts
     *
     */
    class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent evt) 
        {
            if(!paused.get())
            {
                button.setText("Start");
                paused.set(true);
            }
            else
            {
                button.setText("Pause");
                paused.set(false);

                // Resume
                synchronized(threadObject)
                {
                    threadObject.notify();
                }
            }
        }
    }
}

And here's your main class:

 import javax.swing.JFrame;
    import javax.swing.SwingUtilities;


    public class MainClass 
    {
        /**
         * Main method of this application
         */
        public static void main(final String[] arg)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new PanelWithButton());
                    frame.pack();
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                }
            });
        }
    }

As you can see, this example application will continually write to the text area until you click the button that reads 'Pause', whereupon to resume you'll need to click that same button which will now read 'Start'.

这篇关于如何暂停程序直到按下按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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