JFrame不会变得透明

时间:2016-11-26 10:30:54

标签: java swing jframe

最近我一直试图获得透明的JFrame而没有成功。

我想让所有组件都可见而不是框架,我看到人们将背景设置为透明并将不透明设置为false。

我这样做但没有成功。

public class KeyDialog extends JFrame {
    public static MapPanel mapPanel = new MapPanel();

    public KeyDialog() {
        GridBagConstraints customGridBagLayout = new GridBagConstraints();
        setLayout(new GridBagLayout());
        setUndecorated(true);
        // Title - Row 1
        JTextField row1 = new JTextField();
        row1.setSize(new Dimension(this.getWidth(), HEIGHT));
        row1.setFont(new Font("Arial", Font.PLAIN, 30));
        PromptSupport.setPrompt("Title Goes Here", row1);
        customGridBagLayout.fill = GridBagConstraints.HORIZONTAL;
        customGridBagLayout.gridy = 0;
        add(row1, customGridBagLayout);

        // Key - Row 2
        JPanel row2 = new JPanel();
        row2.setOpaque(false);
        // With Key
        JPanel withKey = titleBoxKey(true);
        withKey.setBackground(new Color(0, 0, 0, 0));
        row2.add(withKey);
        // Against Key
        JPanel againstKey = titleBoxKey(false);
        againstKey.setBackground(new Color(0, 0, 0, 0));
        row2.add(againstKey);

        customGridBagLayout.gridy = 1;
        add(row2, customGridBagLayout);

        pack();
    }

    public static JPanel titleBoxKey(boolean with) {
        JPanel keyPanel = new JPanel();
        keyPanel.setLayout(new GridBagLayout());
        GridBagConstraints customGridBagLayout = new GridBagConstraints();
        customGridBagLayout.insets = new Insets(7, 7, 7, 7); // Padding
        Color republicanColor = null;
        Color democraticColor = null;
        if (with) {
            republicanColor = mapPanel.getRepublicanWithColor();
            democraticColor = mapPanel.getRepublicanAgainstColor();
        } else if (!with) {
            republicanColor = mapPanel.getDemocraticAgainstColor();
            democraticColor = mapPanel.getDemocraticAgainstColor();
        }

        // Row 1 - Rectangles and Text Field
        // Democratic Rectangle
        JLabel republicanRect = mapPanel.drawRect(republicanColor);
        customGridBagLayout.fill = GridBagConstraints.HORIZONTAL;
        customGridBagLayout.weightx = 0.5;
        customGridBagLayout.gridx = 0;
        customGridBagLayout.gridy = 0;
        keyPanel.add(republicanRect, customGridBagLayout);

        // Republican Rectangle
        JLabel democraticRect = mapPanel.drawRect(democraticColor);
        customGridBagLayout.fill = GridBagConstraints.HORIZONTAL;
        customGridBagLayout.weightx = 0.5;
        customGridBagLayout.gridx = 1;
        customGridBagLayout.gridy = 0;
        keyPanel.add(democraticRect, customGridBagLayout);

        // With/Against Label
        JLabel infoLabel;
        if (with) {
            infoLabel = new JLabel("With");
        } else {
            infoLabel = new JLabel("Against");
        }
        customGridBagLayout.weightx = 0.5;
        customGridBagLayout.gridx = 2;
        customGridBagLayout.gridy = 0;
        keyPanel.add(infoLabel, customGridBagLayout);

        // Row 2 - Bottom Text
        // Republican Label
        JLabel republicanLabel = new JLabel("Republican");
        customGridBagLayout.weightx = 0.5;
        customGridBagLayout.gridx = 0;
        customGridBagLayout.gridy = 1;
        keyPanel.add(republicanLabel, customGridBagLayout);

        // Democrat Label
        JLabel democraticLabel = new JLabel("Democratic");
        customGridBagLayout.weightx = 0.5;
        customGridBagLayout.gridx = 1;
        customGridBagLayout.gridy = 1;
        keyPanel.add(democraticLabel, customGridBagLayout);

        return keyPanel;
    }
}

3 个答案:

答案 0 :(得分:0)

你做了一个很好的尝试,但试试这个。

revision

只需添加该代码即可:)

答案 1 :(得分:0)

由于@JontyMorris

的帮助,我将背景设置为frame.setBackground(new Color(0, 0, 0, 0));

然后我将框架上的所有内容设置为不透明panel.setOpaque(false); 并删除了我设置的背景,因此剥离的版本看起来像这样......

// Frame
JFrame frame = new JFrame();
frame.setBackground(new Color(0, 0, 0, 0)); // Setting frame to be transparent

// Components
JPanel panel = new JPanel();
//add stuff to panel...
panel.setOpaque(false);

答案 2 :(得分:0)

实际上它比设置背景颜色要多一些。您必须将未修饰的属性更改为true。这是您可以遵循的快速教程。很好解释。

https://www.youtube.com/watch?v=zecGJfNHPWo