Java full screen background color wont change?(Java全屏背景颜色不会改变?)
问题描述
我有一些代码可以在 java 中创建一个全屏图标并将背景颜色设置为粉红色,将前景色设置为红色.但是,每次我运行它时,它都不会将背景颜色更改为红色,而只是让它透明.我把代码放在下面.
I have some code that creates a full screen icon in java and sets the background color to pink and the foreground color to red. However every time i run it, it never changes the background color to red but just keeps it see through. I put the code below.
主java:
import java.awt.*;
import javax.swing.*;
@SuppressWarnings({ "serial" })
public class bob extends JFrame{
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800,600,16, DisplayMode.REFRESH_RATE_UNKNOWN);
bob b = new bob();
b.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.RED);
setFont(new Font("Arial", Font.PLAIN, 24));
screen s = new screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(Exception ex){}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("This is gonna be awesome", 200, 200);
}
}
这里是屏幕类:
import java.awt.*;
import javax.swing.*;
public class screen2 {
private GraphicsDevice vc;
public screen2(){
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){}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
有人有什么想法吗?
推荐答案
public void paint(Graphics g){
g.drawString("This is gonna be awesome", 200, 200);
}
背景的绘制是在 paint()
方法中完成的.您覆盖了该方法并且没有调用 super.paint(g)
所以背景永远不会被绘制.
The painting of the background is done in the paint()
method. Your overrode the method and didn't invoke super.paint(g)
so the background never gets painted.
但是,这不是进行自定义绘画的方法.您不应该覆盖 JFrame 的 paint() 方法.如果您想进行自定义绘画,请覆盖 JPanel
的 paintComponent()
方法,然后将面板添加到框架中.
However, this is NOT the way to do custom painting. You should NOT override the paint() method of a JFrame. If you want to do custom painting then override the paintComponent()
method of a JPanel
and then add the panel to the frame.
阅读关于 自定义绘画的 Swing 教程部分了解更多信息.
Read the section from the Swing tutorial on Custom Painting for more information.
添加 super.paint(g) 后,框架的子组件将被绘制.这意味着内容窗格被绘制并且内容窗格被绘制在框架上,因此您不会看到框架的背景,因此您还需要添加:
Once you add the super.paint(g), child components of the frame will be painted. This means the content pane gets painted and the content pane is painted over the frame so you won't see the background of the frame, so you also need to add:
//setBackground(Color.PINK);
getContentPane().setBackground(Color.PINK);
这篇关于Java全屏背景颜色不会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java全屏背景颜色不会改变?


基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01