Swing怎么把Panel设置为透明

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(null);                      // 把背景设置为会
buttonPanel.setOpaque(false);                          // 设置为透明

 使用这两行代码就行了。

展开阅读全文

Swing设置透明问题

08-06

我编写了一个Swing登录框,有设置背景,但是外层的jpanel会把背景覆盖掉,感觉很丑,要怎么设置透明,各位大神帮帮忙!rn代码如下:rnrnpublic class UserLogin extends JFramern rn private Graphics g;rn rn //构造函数rn public UserLogin()rn //加载背景图片rn final ImageIcon icon = new ImageIcon("D:\\workspace\\images\\bg_login.jpg");rn JPanel container = new JPanel() rn public void paintComponent(Graphics g) rn super.paintComponent(g);rn Graphics2D g2d = (Graphics2D) g;rn if(icon != null)rn g2d.drawImage(icon.getImage(), 0, 0, getWidth(), this.getHeight(), this);rn rn rn ;rn rn //logo图片rn JLabel lblIamge = new JLabel();rn ImageIcon admin_p = new ImageIcon("D:\\workspace\\images\\admin_p.gif");rn lblIamge.setIcon(admin_p);rn rn //用户名、密码、登录rn JTextField tfUserName = new JTextField(15);rn JPasswordField pfPassword = new JPasswordField(15);rn JButton bntLogin = new JButton("登录");rn tfUserName.setBackground(null);rn tfUserName.setOpaque(false);rn pfPassword.setOpaque(false);rn rn JPanel center_right = new JPanel();rn rn center_right.setLayout(new GridLayout(3,1,10,10));rn center_right.setBackground(null);rn center_right.setOpaque(false);rn center_right.add(tfUserName);rn center_right.add(pfPassword);rn center_right.add(bntLogin);rn rn rn JPanel center = new JPanel();rn center.add(lblIamge);rn center.add(center_right);rn rn rn container.add(center);rn rn //this.setBounds(0,0,400,300);rn this.setSize(400,300); //设置窗口大小rn this.setLocationRelativeTo(null); //设置窗口居中rn this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn this.setContentPane(container);rn this.setVisible(true);rn rn rn rn public static void main(String[] args)rn SwingUtilities.invokeLater(new Runnable() rn rn @Overridern public void run() rn new UserLogin();rn rn rn );rn rnrn运行后的效果:rn[img=https://img-bbs.csdn.net/upload/201408/06/1407296214_504496.jpg][/img]rn 论坛

设置JPanel透明

06-22

我定义了一个JPanel,定义如下:rn[code=Java][/code]rnpackage ui.mainUi;rnrnimport javax.swing.JButton;rnimport javax.swing.JPanel;rnimport javax.swing.JSlider;rnrnimport java.awt.*;rnrnclass MainPanel extends JPanel rnrn private JButton playerButton;rn private JButton nextone;rn private JButton previousone;rn private JButton vloumimage;rn protected static Label labelfilepath = new Label("播放目录:");rnrn private static final Image image = Toolkit.getDefaultToolkit().getImage(rn "img/skin6.jpg");rnrn // 加载播放器的背景rn protected void paintComponent(Graphics g) rn super.paintComponent(g);rn Graphics gBackground = g.create();rn gBackground.drawImage(image, 0, 0, getWidth(), getHeight(), this);rn gBackground.dispose();rn rnrn MainPanel() rn nextone = new Button("img/rewind.png");rn nextone.setBounds(new Rectangle(165, 60, 40, 35));rn playerButton = new Button("img/play.png");rn playerButton.setBounds(210, 60, 40, 35);rn previousone = new Button("img/fastForward.png");rn previousone.setBounds(255, 60, 40, 35);rn vloumimage = new Button("img/sound.png");rn vloumimage.setBounds(315, 65, 30, 25);rn labelfilepath.setBounds(20, 100, 400, 35);rn setLayout(null);rn add(labelfilepath, null);rn add(nextone, null);rn add(previousone, null);rn add(playerButton, null);rn JSlider VolumControl = new JSlider();rn VolumControl.setBounds(350, 60, 110, 35);rn VolumControl.setPaintLabels(true);rn add(VolumControl);rn add(vloumimage);rn this.setOpaque(false);rn// setBackground(null);rn setVisible(true);rn rnrnrnrnrn我现在因为JPanle的背景是一张图片,所以当我在向JPane加新的控件的时候,就覆盖了。rn向请问下要怎么样才能将JPanel设置成透明的 ,而不会被新加的控件覆盖呢? 论坛

没有更多推荐了,返回首页