【问题标题】:java.awt.Frame.setBackground(Color arg0) not displaying PINK colorjava.awt.Frame.setBackground(Color arg0)不显示粉红色
【发布时间】:2018-05-25 19:43:39
【问题描述】:

我正在尝试运行以下程序,这些程序应该显示背景颜色 PINK 和字符串“这是一个测试”。白色

1) Test.java

package Practice;

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

public class Test extends JFrame{

    public static void main(String[] args){

        DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
        Test test = new Test();
        test.run(dm);
    }

    public void run(DisplayMode dm){

        setBackground(Color.PINK);
        setForeground(Color.WHITE);
        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 a test.", 200, 200);
    }
}

2) Screen.java

package Practice;

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

public class Screen {

    private GraphicsDevice videoCard;

    public Screen(){

        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        videoCard = env.getDefaultScreenDevice();
    }

    public void setFullScreen(DisplayMode dm, JFrame window){

        window.setUndecorated(true);
        window.setResizable(false);
        videoCard.setFullScreenWindow(window);

        if(dm != null && videoCard.isDisplayChangeSupported()){
            try{
                videoCard.setDisplayMode(dm);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public Window getFullScreenWindow(){

        return videoCard.getFullScreenWindow();
    }

    public void restoreScreen(){

        Window w = videoCard.getFullScreenWindow();

        if(w != null){

            w.dispose();
        }

        videoCard.setFullScreenWindow(null);
    }

}

预期结果:

全屏显示背景颜色 PINK 和字符串“这是一个测试”。白色

实际结果:

全屏显示背景颜色黑色和字符串“这是一个测试”。白色。

我在 Windows 机器上的 Eclipse 中运行它。

【问题讨论】:

  • 不要设置框架的背景和前景。绘制方法中的图形对象不处理框架背景或前景。您必须为图形对象设置颜色 (g.setColor (Color.PINK);)。此外,从不 覆盖paint 方法(改用paintComponent),并且不要在您的框架类中这样做。您可以在扩展 JPanel 的类中完成所有绘画工作(在 paintComponent 方法中),然后将该面板添加到框架中。
  • @Ansharja 好吧,我目前只有这两个课程。设置图形对象后(g.setColor(Color.PINK);)。我也遇到了同样的问题。
  • 您是否在 绘制字符串之前设置图形颜色?
  • 我在之前和之后都试过了,但在之前的情况下:我的字体被改变了,而不是背景颜色。

标签: java swing jframe awt


【解决方案1】:

正如我在第一条评论中所说,有很多事情你不应该做:

  • 您不应该直接使用JFrame 进行一些自定义绘画。相反,请使用JPanel。在这种情况下也不需要扩展JFrame,你没有添加任何功能。

  • 要更改框架的背景颜色,最好设置框架内容窗格的背景颜色。此外,要更改用于绘制字符串的颜色,您应该调用Graphics.setColor ()

  • 不要覆盖paint 方法。相反,覆盖 JComponent's paintComponent 方法。此外,方法中的第一行应该调用父绘制方法,以允许组件在执行任何其他操作之前正常绘制自身。

最后,不清楚你想在 Screen 类中做什么,但调用 Thread.sleep 实际上是一种不好的做法,对我来说所有这些代码都没有多大意义。

启用全屏模式的最佳方法是在您的框架上调用setExtendedState (JFrame.MAXIMIZED_BOTH)

这是我所说的一个工作示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class Test
{
    public static void main (String [] a) {
        SwingUtilities.invokeLater (new Runnable () {
            @Override public void run () {
                createAndShowGUI ();
            }
        });
    }
    private static void createAndShowGUI () {
        JFrame frame = new JFrame ("App");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setContentPane (new DrawPanel (Color.PINK, Color.WHITE));
        frame.pack ();
        // frame.setExtendedState (JFrame.MAXIMIZED_BOTH); // You can use this instruction to have full screen mode.
        frame.setLocationRelativeTo (null);
        frame.setVisible (true);
    }
}
class DrawPanel extends JPanel
{
    Color foregroundColor;

    public DrawPanel (Color backgroundColor, Color foregroundColor) {
        setBackground (backgroundColor);
        this.foregroundColor = foregroundColor;
    }
    @Override public Dimension getPreferredSize () {
        return new Dimension (400, 400);
    }
    @Override protected void paintComponent (Graphics g) {
        super.paintComponent (g);
        g.setColor (foregroundColor);
        g.setFont (new Font ("Arial", Font.PLAIN, 24));
        g.drawString ("This is a test.", 200, 200);
    }
}

截图:

【讨论】:

    猜你喜欢
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2014-02-26
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2022-07-29
    相关资源
    最近更新 更多