一個關(guān)于定時提醒的彈窗例子,還不是很成熟,僅供參考
創(chuàng)新互聯(lián)是一家專業(yè)提供馬山企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、做網(wǎng)站、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為馬山眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
//主窗口界面設(shè)計
public class TestButtons {
JFrame frame = new JFrame("提醒");
JButton jButton = new JButton("開始運行"); //按鈕
JCheckBox checkBox = new JCheckBox("早上提醒"); //復(fù)選按鈕
JCheckBox checkBox1 = new JCheckBox("晚上提醒"); //復(fù)選按鈕
JLabel label = new JLabel("Ready to run."); //靜態(tài)文本
//構(gòu)造函數(shù)
public TestButtons() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new java.awt.FlowLayout());
//為按鈕添加動作監(jiān)聽器
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//按鈕事件處理程序
label.setText("Thread keep running.");
int i=Timetorun()*1000;
try {
Thread.sleep(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "該干啥了。");
}
});
//早上提醒 復(fù)選框處理程序
checkBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
label.setText("Selected Check Box is " + cb.isSelected());
}
});
//晚上提醒 復(fù)選框處理程序
checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
label.setText("Selected Check Box is " + cb.isSelected());
}
});
//添加組件到容器
frame.getContentPane().add(jButton);
frame.getContentPane().add(checkBox);
frame.getContentPane().add(checkBox1);
frame.getContentPane().add(label);
frame.setSize(200, 250);
}
public void show() {
frame.show();
}
//主處理函數(shù)
public static void main(String[] args) {
TestButtons tb = new TestButtons();
tb.show();
}
//時間處理函數(shù)
public static int Timetorun(){
//獲取系統(tǒng)當(dāng)前時間
Calendar cal=Calendar.getInstance();
int nowhour=cal.get(Calendar.HOUR_OF_DAY);
int nowminite=cal.get(Calendar.MINUTE);
int nowweek=cal.get(Calendar.DAY_OF_WEEK);
System.out.println(nowhour+"");
System.out.println(nowminite+"");
System.out.println(nowweek+"");
int dehour=0;
int deminite=0;
//獲取延遲時間
if(nowweek==2 || nowweek==3 || nowweek==5){
if(nowminite=30 nowhour=20){
deminite=30-nowminite;
dehour=20-nowhour;
}
else if(nowminite30 nowhour=20){
deminite=90-nowminite;
dehour=19-nowhour;
}
else if (nowminite=30 nowhour20) {
deminite=30-nowminite;
dehour=44-nowhour;
}
else {
deminite=90-nowminite;
dehour=43-nowhour;
}}
else if(nowweek==4 || nowweek==6 || nowweek==7 || nowweek==1){
if(nowminite=30 nowhour=17){
deminite=30-nowminite;
dehour=17-nowhour;
}
else if(nowminite30 nowhour=17){
deminite=90-nowminite;
dehour=16-nowhour;
}
else if (nowminite=30 nowhour17) {
deminite=30-nowminite;
dehour=41-nowhour;
}
else {
deminite=90-nowminite;
dehour=40-nowhour;
}
}
//延遲時間為K分鐘
int k= dehour*60 + deminite;
return k;
}
}
tcp或者udp設(shè)置為全局的,不要定義在某一個窗體里面,當(dāng)udp讀取線程接受到數(shù)據(jù)后,就直接new這個窗體,把消息帶過去,比如new ChatFrame(String msg).show();就行了。
代碼缺一行:
。。。
authorTextArea.setPreferredSize(new Dimension(40, 80));
authorFrame.add(authorTextArea);
。。。
以上完了后,需要加一個
authorFrame.setVisible(true);
至于這個框的大小,你再調(diào)調(diào)哈,相互學(xué)習(xí)~,三年沒做過了~
首先,給你看一個簡單的代碼例子先:
import java.util.*;
public class Test {
public static void main(String[] args) {
Date myDate = new Date();
Timer timer = new Timer();
timer.schedule(new MyTask(), myDate);
}
static class MyTask extends java.util.TimerTask {
public void run() {
System.out.println("________");
}
}
}
這段代碼的作用是:在當(dāng)前時間打印出“________”。
這里用到了Timer的schedule方法,該方法的使用有如下兩種情況:
schedule(TimerTask task, Date time)設(shè)定指定任務(wù)task在指定時間time執(zhí)行;
schedule(TimerTask task, long delay, long period)方法設(shè)定指定任務(wù)task在指定延遲delay后進行固定延遲peroid的執(zhí)行。
scheduleAtFixedRate(TimerTask task, long delay, long period)方法設(shè)定指定任務(wù)task在指定延遲delay后進行固定頻率peroid的執(zhí)行。
這里要注意一點:如果是用后面兩個方法的話,則要通過timer的cancel()方法結(jié)束其運行,否則會一直循環(huán)執(zhí)行下去。
那么,回到你的題目,只要將Date time參數(shù)改一下、將上面的run()方法的方法體改一下就行了。
記得給我分哦~~^_^
名稱欄目:java固定彈出窗口代碼 java怎么固定窗口大小
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article46/ddohjhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、App開發(fā)、云服務(wù)器、網(wǎng)站設(shè)計公司、虛擬主機、品牌網(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)