Java文本和颜色(0,0,0,0)

age*_*ate 0 java text jframe distortion

我正在编写一个带有未装饰框架的程序,文本显示不正确.我很确定这行是导致问题但不知道原因:

    setBackground(new Color(0, 0, 0, 0));
Run Code Online (Sandbox Code Playgroud)

这是文本的样子和它应该是什么样子

坏文

好文

这是我的代码:它是我的常规文件的简短版本,所以它可能看起来令人困惑.另外,我只用java工作了一个半星期.

    import java.awt.Dimension;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import static java.awt.GraphicsDevice.WindowTranslucency.*;



    public class MyTunesMain {

        public static void main(String[] args) {

            //MyTunes myTunes = new MyTunes();
            ShortTest myTunes = new ShortTest();

        }
    }

    ///////////////////////////////////////////

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


    public class ShortTest extends JFrame {

        // id
        private static final long serialVersionUID = 1L;

        // basic inits
        private int width = 1000;
        private int height = 600;
        SoundThread music;
        Font searchFont = new Font("Calibri", Font.PLAIN, 18);
        Container content = getContentPane();

        // JFrame stuff
        JFrame jf = new JFrame();
        JPanel topPanel = new JPanel();
        JPanel mainPanel = new JPanel();
        private JLabel songPlayed;



        // //////////////////////////////////////////////
        public ShortTest() {

            // initialize window and technical properties
            super("ShortTest");

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //dimension
            int extendBy=30;
            setMaximumSize(new Dimension(width + extendBy, height + extendBy));
            setMinimumSize(new Dimension(width + extendBy, height + extendBy));
            setPreferredSize(new Dimension(width + extendBy, height extendBy));
            setUndecorated(true);
            setLocationRelativeTo(null);

            //setBackground(new Color(0, 0, 0));
            setBackground(new Color(0, 0, 0, 0));      // all hell breaks lose
            getContentPane().setBackground(Color.BLACK);
                    setLayout(null);


            // initialize jpanel for objects
            mainPanel.setBounds(6, 6, width, height);
            mainPanel.setLayout(null);
            mainPanel.setOpaque(true);
            mainPanel.setBackground(Color.gray);
            add (mainPanel);


            mainPanel.add(topPanel);
            topPanel.setBounds(0, 0, 1000, 50);
            topPanel.setLayout(null);

            // setup song label
            songPlayed = new JLabel("Little Wing");
            songPlayed.setFont(searchFont);
            FontMetrics fm = songPlayed.getFontMetrics(songPlayed.getFont());
            String text = songPlayed.getText();
            int textWidth = fm.stringWidth(text);
            songPlayed.setBounds(500 - textWidth / 2, 2, textWidth, 15);
            songPlayed.setHorizontalAlignment(SwingConstants.CENTER);


            // push onto top JPanel
            topPanel.add(songPlayed);


            setVisible(true);

            System.out.println("\n done with init...........");

        }

    }
Run Code Online (Sandbox Code Playgroud)

ppe*_*rka 5

问题的构造函数文档说明:

使用指定的红色,绿色,蓝色和alpha值(范围为0 - 255)创建sRGB颜色.

参数:

r - 红色成分

g - 绿色成分

b - 蓝色成分

a - alpha分量

最后一个是重要的 - 你将Alpha通道设置为0 - 这意味着颜色不是真正的颜色,而是透明度... RGBA颜色空间Wiki文章

  • 用于new Color(0,0,0,255);指定100%不透明黑色
  • 或者new Color(0,0,0);从文档中使用:"Alpha默认为255."