單獨顯示Dialog略奇怪,所以乎衫我用JFrame做例子,原理是一樣的

創(chuàng)新互聯(lián)是一家專業(yè)提供綿竹企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為綿竹眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
建立一個線程,睡3秒,醒寬頃罩來后就把JFrame窗體關(guān)閉。
給JFrame加事件監(jiān)聽,包括所有你想監(jiān)聽的事件,我這里為了簡便,只監(jiān)聽了鼠標單擊。一旦事件發(fā)生,則吵醒那個線程,這樣它就睡不足3秒,并且直接進入下一個迭代,繼續(xù)慎鬧睡3秒。
直到有一次單擊后3秒內(nèi)沒有操作,這時候線程不會觸發(fā)InterruptedException 異常,就會執(zhí)行關(guān)閉窗體的操作
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class DummyTest {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final Thread thread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(3000);
frame.dispose();
return;
} catch (InterruptedException e) {}
}
}
});
frame.setSize(400, 400);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
thread.interrupt();
}
});
frame.setVisible(true);
thread.start();
}
}
構(gòu)造函數(shù)中加一句setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
修改后完整的代碼
編譯通過~
import java.awt.*;
import java.awt.event.*;
public class TestGraphics extends Frame{
public void paint(Graphics g)
{
int x,y,i=0;
Font font=new Font("褲態(tài)Serif",Font.ITALIC|Font.BOLD,40);
g.setFont(font);
g.drawOval(60,50,80,80);
g.setColor(Color.red);
g.fillOval(150, 50, 80, 80);
g.setColor(Color.pink);
g.drawOval(240, 50, 80, 80);
g.setColor(Color.green);
g.drawString("i love java"晌純槐, 80, 260);
g.setColor(Color.blue);
for(i=0;i20;i++)
{
x=(int)(Math.random()*300)+30;
y=(int)(Math.random()*200)+130;
g.fillOval(x, y, 10, 10);
}
g.setColor(Color.orange);
y=100;
for(i=0;i40;i++)
{
y+=5;
g.drawRect(30, 30, 320, y);
}
}
public static void main(String[] args)
{
TestGraphics f=new TestGraphics();
f.setTitle("我的第宴友一個Graphics");
f.setSize(400,345);
f.setLocation(0,0);
f.setVisible(true);
f.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
});
}
}
網(wǎng)頁題目:java自動關(guān)閉窗口代碼 java關(guān)閉窗口語句
文章出自:http://chinadenli.net/article14/dsppege.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、微信小程序、網(wǎng)站排名、動態(tài)網(wǎng)站、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)