我正在编写一个带有未装饰框架的程序,并且文本无法正确显示。我很确定这条线导致了问题,但不知道为什么:
setBackground(new Color(0, 0, 0, 0));
这是文本的外观和应该是什么样的
这是我的代码:它是我的常规文件的简短版本,因此它可能看起来令人困惑。此外,我只使用 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...........");
}
}