简体   繁体   English

如何使jPanel半透明?

[英]How to make a jPanel semi transparent?

I want to add a jPanel which is semi transparent. 我想添加一个半透明的jPanel。 But other components which are placed inside the jPanel such as buttons and labels should be displayed with 100% opacity. 但是,放置在jPanel内部的其他组件(如按钮和标签)应以100%不透明度显示。 I am using netbeans to design the GUIs. 我正在使用netbeans来设计GUI。 Normally i drag and drop the swing components in the palette to design the GUI(I don't code them). 通常我将摆动组件拖放到调色板中以设计GUI(我不对它们进行编码)。 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. 由于我是java新手,请给我一个详细的答案。 Thanks in advance. 提前致谢。

You can use JPanel.setBackground(Color bg); 你可以使用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. 您可以使用Alpha值构造颜色以设置颜色的透明度。

panel.setBackground(new Color(213, 134, 145, 123)); panel.setBackground(new Color(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. 这是效果,希望它可以帮助你。

在此输入图像描述

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值)来更改透明度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM