51,412
社区成员
发帖
与我相关
我的任务
分享
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();
}
}