Java setBackground无效

时间:2016-01-21 00:06:30

标签: java swing graphics jframe setbackground

我在Java中制作游戏,目前设置的背景不起作用,但是setforeground工作。这是我的主要类的代码:

import java.awt.*;
import javax.swing.*;

public class Sam extends JFrame{

public static void main(String[] args) {

    DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
    Sam s = new Sam();
    s.run(dm);
}
public void run(DisplayMode dm){
    this.setBackground(Color.PINK);
    this.setForeground(Color.BLACK);
    setFont(new Font("Arial", Font.PLAIN, 24));

    Screen s = new Screen();
    try{
        s.setFullScreen(dm, this);
        try{
            Thread.sleep(5000);
        }catch(Exception ex){ex.printStackTrace();}
    }finally{
        s.restoreScreen();
    }
}
public void paint(Graphics g){
    g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
}

}

这是我的Screen类的代码:

import java.awt.*;
import javax.swing.*;
public class Screen {

private GraphicsDevice vc;

public Screen(){
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window){
    window.setUndecorated(true);
    window.setResizable(false);
    vc.setFullScreenWindow(window);

    if(dm != null && vc.isDisplayChangeSupported()){
        try{
            vc.setDisplayMode(dm);
        }catch(Exception ex){ex.printStackTrace();}
    }
}
public Window getFullScreenWindow(){
    return vc.getFullScreenWindow();
}
public void restoreScreen(){
    Window w = vc.getFullScreenWindow();
    if(w != null){
        w.dispose();
    }
    vc.setFullScreenWindow(null);
}
}

1 个答案:

答案 0 :(得分:2)

JFrame是一个复合组件,它实际上由许多其他组件组成

enter image description here

(来自How to Use Root Panes

因此,当您致电setBackground时,您只会更改基础框架的背景颜色,而不会更改其所在的contentPane

相反,您应该使用getContent().setBackground(...)

“但为什么setForegound有效”我在这里问你?

那是因为你覆盖了paint JFrame foregroundcontentPane预先配置了框架的JFrame属性(而不是paint

现在,说了这么多......

  1. 出于这些原因,您应该避免从JPanel这样的顶级容器扩展而来......
  2. 您也不应该覆盖paintComponent。您应该从一个组件开始,从paint扩展并覆盖它的import java.awt.Color; import java.awt.DisplayMode; import java.awt.EventQueue; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Sam extends JFrame { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } DisplayMode dm = new DisplayMode(1920, 1080, 32, 60); Sam s = new Sam(); s.run(dm); } }); } public Sam() { add(new MainView()); } public void run(DisplayMode dm) { Screen s = new Screen(); s.setFullScreen(dm, this); Timer timer = new Timer(5000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { s.restoreScreen(); } }); timer.start(); } public class MainView extends JPanel { public MainView() { this.setBackground(Color.PINK); this.setForeground(Color.BLACK); setFont(new Font("Arial", Font.PLAIN, 24)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); String text = "THIS IS GUNNA BE AWESOME"; FontMetrics fm = g.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g.drawString(text, x, y); } } public class Screen { private GraphicsDevice vc; public Screen() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); vc = env.getDefaultScreenDevice(); } public void setFullScreen(DisplayMode dm, JFrame window) { window.setUndecorated(true); window.setResizable(false); vc.setFullScreenWindow(window); if (dm != null && vc.isDisplayChangeSupported()) { try { vc.setDisplayMode(dm); } catch (Exception ex) { ex.printStackTrace(); } } } public Window getFullScreenWindow() { return vc.getFullScreenWindow(); } public void restoreScreen() { Window w = vc.getFullScreenWindow(); if (w != null) { w.dispose(); } vc.setFullScreenWindow(null); } } } 方法。
  3. 看看:

    当您覆盖顶级容器的{{1}}时出错的情况,并且它们不是双缓冲的,因此更新它们会导致闪烁

    实施例...

    {{1}}