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全屏背景颜色不会改变?
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01