「java闪烁」java解决闪屏
本篇文章给大家谈谈java闪烁,以及java解决闪屏对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java程序 出现的按钮会闪烁 求解
Java程序:
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main extends JFrame implements Runnable {
JButton btn;
public Main() {
super("闪烁的按钮");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
btn = new JButton("我会闪烁");
this.add( btn);
Thread th = new Thread(this);
th.start();
}
public static void main(String[] args) {
new Main();
}
@Override
public void run() {
int r = 127;
int g = 127;
int b = 127;
int i, j, k;
Random rand = new Random();
while(true) {
i = rand.nextInt(255);
j = rand.nextInt(255);
k = rand.nextInt(255);
r = (r + i) % 255 + 1;
g = (g + j) % 255 + 1;
b = (b + k) % 255 + 1;
btn.setBackground(Color.getHSBColor(r, g, b));
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行该程序后,可以看到按钮的背景会持续闪烁。
java闪烁问题,用了双缓冲,可是仍然闪烁。求教如何改?
//改这里Thread.sleep(10);和x+=1;y+=1;
import java.awt.*;
import javax.swing.*;
public class ScGame extends JFrame{
int oldx,x,oldy,y;
Image buffer=null;
ScGame(){
setSize(800,600);
setVisible(true);
oldx=x=100;oldy=y=100;
}
public void paint(Graphics g)
{
g.drawImage(buffer,getInsets().left,getInsets().top,this);
}
void motion()
{ oldx=x;oldy=y;
x+=1;y+=1;
Color c1=new Color(255,0,0);
Graphics temp;
if(buffer==null)
buffer=createImage(800,600);
temp=buffer.getGraphics();
temp.setColor(c1);
temp.clearRect(oldx,oldy,100,100);
temp.fill3DRect(x, y, 100, 100,false);
temp.dispose();
}
public static void main(String args[])
{
ScGame scg=new ScGame();
try{
for(int i=0;i1000;i++){
Thread.sleep(10);
scg.motion();
scg.repaint();
}
}catch(Exception e){
e.printStackTrace();}
}
}
java 窗口程序 画面闪烁。
闪烁
是因为
调用的repaint方法,你试试update(getGraphics())方法试试,repaint已经是重绘了,或者你调用repaint(x,y,w,h)局部刷新
java编写的图形界面,为什么会闪烁
1、因为调用了repaint方法,或者调用repaint(x,y,w,h)局部刷新
2、可调用java 的awt/swing控件,下面贴一个示例代码:
public static void main(String args[]){
JFrame jf = new JFrame();
jf.getContentPane().setLayout(new FlowLayout());
jf.getContentPane().add(new JButton("测试"));
jf.setSize(320, 240);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
java闪烁的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java解决闪屏、java闪烁的信息别忘了在本站进行查找喔。
发布于:2022-12-10,除非注明,否则均为
原创文章,转载请注明出处。