圖片顯示,螢屏上有兩張圖片,一張顯示正常圖片,一張顯示提示圖片。
回圈更換圖片,假定一次播放時間為80ms,
1.正常圖片與提示圖片同時顯示
2.正常圖片顯示80ms
3.提示圖片顯示前20ms,后60ms不顯示
下列代碼以顏色代替圖片,我該如何控制提示圖片,提示圖片顯示不穩定
import javax.swing.*;
import java.awt.*;
/**
* @className: JFrameTest
* @description: 正常圖片與提示圖片同時顯示,一次圖片更換時間 80ms,
* 正常圖片顯示80ms,提示圖片顯示前20ms,剩余60ms不顯示,
*
* @version: 1.0
**/
public class JFrameTest extends JFrame{
private JPanel normalPane; //正常圖片
private JLabel colorPane; //提示圖片
public JFrameTest(){
normalPane = new JPanel();
normalPane.setBackground(Color.WHITE);
normalPane.setPreferredSize(new Dimension(100,100));
normalPane.setMaximumSize(new Dimension(100,100));
colorPane = new JLabel();
colorPane.setBackground(Color.WHITE);
colorPane.setPreferredSize(new Dimension(100,100));
colorPane.setMaximumSize(new Dimension(100,100));
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(400,400));
contentPane.setBackground(Color.BLACK);
contentPane.setLayout(new BorderLayout());
setContentPane(contentPane);
contentPane.add(normalPane,BorderLayout.NORTH);
contentPane.add(colorPane,BorderLayout.SOUTH);
setSize(new Dimension(400,400));
setVisible(true);
}
public synchronized void setColorPaneVisible(boolean visible){
colorPane.setVisible(visible);
}
public void setNormalPaneVisible(boolean visible){
normalPane.setVisible(visible);
}
static class Task implements Runnable{
private JFrameTest jFrameTest;
private int totalTime;
private int colorWaitTime;
public Task(JFrameTest jFrameTest,int totalTime,int colorWaitTime){
this.jFrameTest = jFrameTest;
this.totalTime = totalTime;
this.colorWaitTime = colorWaitTime;
}
@Override
public void run() {
while (true){
try{
jFrameTest.setNormalPaneVisible(true); //模擬更換圖片
jFrameTest.setColorPaneVisible(true);
Thread.sleep(colorWaitTime);
jFrameTest.setColorPaneVisible(false);
Thread.sleep(totalTime-colorWaitTime);
}catch (Exception e){e.printStackTrace();}
}
}
}
public static void main(String[] args) {
JFrameTest jFrameTest = new JFrameTest();
int totalTime = 80; //總體輪詢時間
int colorWaitTime = 20; //提示圖片顯示時間
new Thread(new Task(jFrameTest,totalTime,colorWaitTime)).start();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268675.html
標籤:Java相關
