Java 如何使 jPanel 半透明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24559167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:00:38  来源:igfitidea点击:

How to make a jPanel semi transparent?

javaswinguser-interfacejpanel

提问by user3641302

I want to add a jPanel which is semi transparent. But other components which are placed inside the jPanel such as buttons and labels should be displayed with 100% opacity. I am using netbeans to design the GUIs. Normally i drag and drop the swing components in the palette to design the GUI(I don't code them). I can't see any property in properties window to achieve this. Please help me. As I am quite new to java please give me a detailed answer. Thanks in advance.

我想添加一个半透明的 jPanel。但是放置在 jPanel 内的其他组件(例如按钮和标签)应以 100% 不透明度显示。我正在使用 netbeans 来设计 GUI。通常我在调色板中拖放摆动组件来设计 GUI(我不编码它们)。我在属性窗口中看不到任何属性来实现这一点。请帮我。由于我对java很陌生,请给我一个详细的答案。提前致谢。

采纳答案by Eugene

You can use JPanel.setBackground(Color bg);to make the panel semi-transparent. What matter is the color's property. You can construct a color with alpha values to set the transparent degree of the color.

您可以使用 JPanel.setBackground(Color bg); 使面板半透明。重要的是颜色的属性。您可以使用 alpha 值构造颜色以设置颜色的透明程度。

panel.setBackground(new Color(213, 134, 145, 123));

panel.setBackground(新颜色(213, 134, 145, 123));

The last parameter is actual the alpha value and you can adjust it to see the effect.

最后一个参数是实际的 alpha 值,您可以调整它以查看效果。

Here is the code:

这是代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class PanelTest {
    public static void main(String[] args) {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                PanelTest test = new PanelTest();
                test.createUI();
            }
        };
        SwingUtilities.invokeLater(runnable);
    }

    public void createUI(){
        JFrame frame = new JFrame("Panel Test");

        JPanel panel = new JPanel();

        panel.setBackground(new Color(213, 134, 145, 123));
        JButton button = new JButton("I am a button");

        JLabel label = new JLabel("I am a label");
        label.setFont(new Font("Arial", Font.BOLD, 15));

        JTextField textField = new JTextField();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(button);
        panel.add(Box.createVerticalStrut(20));
        panel.add(label);
        panel.add(Box.createVerticalStrut(20));
        panel.add(textField);

        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

        BottomPanel buttomPanel = new BottomPanel();
        buttomPanel.add(panel);
        frame.add(buttomPanel,BorderLayout.CENTER);

        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @SuppressWarnings("serial")
    class BottomPanel extends JPanel{
        @Override
        protected void paintComponent(Graphics g) {
            for (int y = 0; y < 200; y = y + 20) {
                g.drawString("I am the string on the bottom", 5, y);
            }
        }
    }
}

Here is the effect and hope it can help you.

下面是效果,希望能帮到你。

enter image description here

在此处输入图片说明

回答by Farnoush Rezaei Jafari

You can simply create your jPanel using drag and drop, as you always do and then for changing the panel's color and making it transparent or semi-transparent you can use this code:

您可以像往常一样简单地使用拖放创建 jPanel,然后更改面板的颜色并使其透明或半透明,您可以使用以下代码:

panel.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.5f));

You can change the color by changing first three parameters of Color constructor, which represent RGB and you can change transparency by changing fourth parameter, which is the alpha value of color.

您可以通过更改 Color 构造函数的前三个参数(代表 RGB)来更改颜色,您可以通过更改第四个参数(即颜色的 alpha 值)来更改透明度。