0

装飾されていないフレームでプログラムを書いていますが、テキストが正しく表示されません。この行が問題を引き起こしていると確信していますが、その理由はわかりません:

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

テキストは次のようになります。

悪いテキスト

良いテキスト

これが私のコードです。これは私の通常のファイルの短いバージョンなので、混乱するかもしれません。さらに、私はJavaを1週間半しか使用していません。

    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...........");

        }

    }
4

1 に答える 1

5

問題のコンストラクターのドキュメントでは、次のように説明されています。

指定された赤、緑、青、およびアルファ値の範囲 (0 ~ 255) で sRGB カラーを作成します。

パラメーター:

r - 赤の成分

g - 緑の成分

b - 青の成分

a - アルファ成分

最後が重要です - アルファ チャネルを 0 に設定します - これは、色が実際には色ではなく透明であることを意味します... RGBA 色空間 Wiki 記事

解決

  • new Color(0,0,0,255);100% 不透明な黒を指定するために使用します
  • またはnew Color(0,0,0);ドキュメントのように使用します:「アルファはデフォルトで255に設定されています。」
于 2013-10-02T22:34:44.883 に答える