gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:20:30 30 4
gpt4 key购买 nike

我正在编写一个带有未修饰框架的程序,并且文本无法在其上正确显示。我很确定这条线导致了问题,但不知道为什么:

    setBackground(new Color(0, 0, 0, 0));

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

bad text

good text

这是我的代码:它是我的常规文件的简短版本,因此可能看起来很困惑。另外,我只使用了一个半星期的 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...........");

}

}

最佳答案

documentation for the constructor in question解释:

Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

Parameters:

r - the red component

g - the green component

b - the blue component

a - the alpha component

最后一点很重要 - 您将 Alpha channel 设置为 0 - 这意味着颜色不是真正的颜色,而是透明度... RGBA color space Wiki article

解决方案

  • 使用 new Color(0,0,0,255); 指定 100% 不透明黑色
  • 或使用 new Color(0,0,0);,如文档中所示:“Alpha 默认为 255。”

关于Java 文本和颜色(0, 0, 0, 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19147864/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com