java - 如何让JComponents背景透明?

标签 java swing jpanel transparency

有两件事我想弄清楚。首先,我想弄清楚如何使 JComponent 背景透明。当将 JPanel 放入另一个设置了颜色作为背景的 JPanel 中时,在另一个 JPanel 中设置的 JPanel 具有我似乎无法摆脱的白色背景。我尝试使用firstpanel.setOpache函数并重新绘制,但它没有做任何事情。

其次,我注意到将一个 JPanel 放入另一个 JPanel 中(该 JPanel 又位于另一个 JPanel 中)会压缩其大小。下面的图片将显示我想要描述的内容。我想知道如何避免压缩 JPanel 大小并仍然能够将其放在其他 JPanel 的两个级别内。下面的代码是我正在练习的代码。

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();
}

}

This image is of two JPanels within the main JPanel, the JPanels expands and are not compressed

This image is of two JPanels within a JPanel that is within another JPanel, the two JPanels are compressed.

最佳答案

只需使用

firstPanel.setOpaque(false);

这将使组件的背景不可见,但您仍然可以看到位于其中的任何组件。

关于java - 如何让JComponents背景透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38257242/

相关文章:

java - 添加 admob 横幅广告时出现 RecyclerView ClassCastException

java - 并发映射按具有快速增量值操作的值排序

java - 不知道在 Windows 上使用哪个 Apache SVN 包

java - 自定义 JPanel 未更新

Java Swing - JLabel 位置

java - 使用 JSON 和 PHP 从 SQL 数据库(无 MYSQL DB)(在 android 中)获取数据

java - 关闭 Swing Frame,用户与系统启动关闭。如何区分?

java - 如何使用 Processing 打开多个窗口?

java - 调整 JFrame 大小时调整 JFrame 上的图像和 JPanel 大小

java - 刷新 JPanel 并切换 JTabbedPane 操作Perfmored