java jpanel 半透明_如何使jPanel半透明?

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

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

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

下面是代码:

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

}

}

}

}

这里是有效果的,希望它可以帮助你。

N3VMX.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值