日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 怎么暂停程序_java – 如何暂停程序直到按下按钮?

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 怎么暂停程序_java – 如何暂停程序直到按下按钮? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Pausing Execution with Sleep,雖然我懷疑這是你想要使用的機制.因此,正如其他人所建議的那樣,我相信您需要實現等待通知邏輯.這是一個非常人為的例子:

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

}

});

}

}

如您所見,此示例應用程序將不斷寫入文本區域,直到您單擊“暫停”按鈕,然后恢復您需要單擊同一按鈕,現在將顯示為“開始”.

總結

以上是生活随笔為你收集整理的java 怎么暂停程序_java – 如何暂停程序直到按下按钮?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。