简体   繁体   English

如何让JComponents背景透明?

[英]How to exactly make JComponents background transparent?

There are two things that I am trying to figure out. 我想弄清楚有两件事。 First thing, I want to figure out how to make a Jcomponent background transparent. 首先,我想弄清楚如何使Jcomponent背景透明。 When placing a JPanel into another JPanel that has a color set as the background, the JPanel that is set within the other JPanel has a white background that I can't seem to get rid of. 将JPanel放入另一个颜色设置为背景的JPanel时,在另一个JPanel中设置的JPanel有一个白色背景,我似乎无法摆脱它。 I tried using the firstpanel.setOpache function and repaint but it doesn't do anything. 我尝试使用firstpanel.setOpache函数并重新绘制,但它没有做任何事情。

And second, I noticed that putting a JPanel within another JPanel thats within another JPanel compresses it size. 其次,我注意到将JPanel放在另一个JPanel中的另一个JPanel压缩它的大小。 The images below will show what I am trying to describe. 下面的图片将显示我想要描述的内容。 I want to know what to do to avoid compressing the JPanel size and still able to put it within two levels of other JPanels . 我想知道如何避免压缩JPanel大小,并且仍然可以将其放在其他JPanel的两个级别中。 The code below is what I am practicing with. 下面的代码是我正在练习的。

import javax.swing.*;
import java.awt.*;


public class LearningFrame extends JFrame {

private JLabel userLabel;

private LearningFrame(){
    JPanel backGroundPanel = new JPanel();
    JPanel mainPanel = new JPanel();
    JPanel firstPanel = new JPanel();
    JPanel secondPanel = new JPanel();

    userLabel = new JLabel("User");
    userLabel.setFont(new Font("Arial",1 ,24));

    backGroundPanel.setBackground(new Color(247,211,53));
   // backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS));

    setContentPane(backGroundPanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1200,800);
    setLocationRelativeTo(null);

    //backGroundPanel.setLayout(null);
    mainPanel.setLayout(new GridLayout(1,2));

    firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));

   // firstPanel.setBackground(new Color(211,43,185));
    secondPanel.setBackground(new Color(34,233,44));


    JButton button = new JButton("Click");
    JButton button2 = new JButton("Click");
    JButton button3 = new JButton("Click");
    JButton button4 = new JButton("Click");


    firstPanel.add(button);
    firstPanel.add(button2);
    firstPanel.add(userLabel);
    secondPanel.add(button3);
    secondPanel.add(button4);

    mainPanel.add(firstPanel);
    mainPanel.add(secondPanel);

    backGroundPanel.add(mainPanel);


    setVisible(true);

}

public JPanel logPanel() {

    JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    JTextField userTextField = new JTextField(14);
    JTextField passTextField = new JTextField(14);


    userLabel = new JLabel("Username: ");
    JLabel passLabel = new JLabel("Password: ");
    passLabel.setFont(new Font("Arial", Font.BOLD, 24));
    userLabel.setFont(new Font("Arial", Font.BOLD, 24));
    logInPanel.add(userLabel);
    logInPanel.add(userTextField);
    logInPanel.add(passLabel);
    logInPanel.add(passTextField);

    logInPanel.setOpaque(true);
    logInPanel.repaint();

    return logInPanel;
}

public static void main(String[] args){

    LearningFrame x = new LearningFrame();
}

}

这个图像是主JPanel中的两个JPanel,JPanels扩展并且没有压缩

此图像是JPanel中的两个JPanel,位于另一个JPanel中,两个JPanel是压缩的。

Just use 只是用

firstPanel.setOpaque(false);

This is will make the background of the component invisible, but you will still be able to see any components that are positioned inside it. 这将使组件的背景不可见,但您仍然可以看到位于其中的任何组件。

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

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