Java Jframe won和x27的背景色;我一点也不改变
我一直试图让我的jframe的背景颜色变成蓝色,但它所做的只是显示默认的jframe颜色。如何使背景实际显示为蓝色? 换句话说,它不允许我得到一个蓝色的背景,但jframe将打开没有任何颜色Java Jframe won和x27的背景色;我一点也不改变,java,Java,我一直试图让我的jframe的背景颜色变成蓝色,但它所做的只是显示默认的jframe颜色。如何使背景实际显示为蓝色? 换句话说,它不允许我得到一个蓝色的背景,但jframe将打开没有任何颜色 package DisplayPackagev1; import java.awt.Canvas; import java.awt.Color; import java.awt.Component; import java.awt.Graphics2D; import java.awt.image.Buf
package DisplayPackagev1;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Sea_InvadersDisplay extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
JFrame frame = new JFrame();
frame.add(display);
frame.pack();
frame.setTitle("Sea Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
display.start();
}
private boolean running = false;
private Thread thread;
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {e.printStackTrace();}
}
public int FPS;
public Sea_InvadersDisplay(){
this.setSize(1300, 690);
this.setFocusable(true);
}
@Override
public void run() {
long timer = System.currentTimeMillis();
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 999999999 / TARGET_FPS;
int frames = 0;
this.createBufferStrategy(3);
BufferStrategy bs = this.getBufferStrategy();
while(running){
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
// double delta = updateLength / ((double) OPTIMAL_TIME); means that when I update it, it doesn't jump.
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
FPS = frames;
frames = 0;
System.out.println(FPS);
}
draw(bs);
try{
Thread.sleep(((lastLoopTime - System.nanoTime()) + OPTIMAL_TIME) / 999999999);
} catch(Exception e){};
}
}
public void draw(BufferStrategy bs){
do {
do {
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
}while(bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
// Buffer Strategy, way to use Buffer Damage so that there isn't any flickering. ALos takes up less resources
}
你必须打上述电话
例如:
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
JFrame frame = new JFrame();
frame.add(display);
frame.pack();
frame.setTitle("Sea Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().setBackground( Color.BLUE );
frame.setVisible(true);
display.start();
}
当您添加
Sea\u investersdisplay
时,将使用该组件填充该帧的所有区域。因此,即使您的JFrame
背景有任何背景颜色,由于叠加Sea\u显示,它也不可见
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
display.setBackground( Color.BLUE ); //set background on display instead
JFrame frame = new JFrame();
...
display.start();
}
相反,请尝试设置Sea\u屏幕的背景色
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
display.setBackground( Color.BLUE ); //set background on display instead
JFrame frame = new JFrame();
...
display.start();
}