JButton 的不透明度/半透明度? [英] Opacity/Translucency of a JButton?

查看:90
本文介绍了JButton 的不透明度/半透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的GUI代码如下,其中我想让JButton变成半透明的,这样JButton后面的图像是可见的!

I have simple GUI code as follows, in which I want to make the JButton one translucent, so that the image behind the JButton is visible!

package dealORnodeal;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2");
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{

    super("Deal Or No Deal");
    setSize(800,850);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLayout(null);
    contentPane.add(bg);

    JMenuItem item1;

    item1 = new JMenuItem("Start Game");
    item1.addActionListener(this);
    menu1.add(item1);

    item1 = new JMenuItem("GoTo Rules");
    item1.addActionListener(this);
    menu1.add(item1);

    item1 = new JMenuItem("GoTo Credits");
    item1.addActionListener(this);
    menu1.add(item1);

    item1 = new JMenuItem("GoTo Menu");
    item1.addActionListener(this);

    menu1.add(item1);
    bar1.add(menu1);
    setJMenuBar(bar1);

    //GAME CODE
    one.setBounds(25,151,190,49);
    one.addActionListener(this);

    add(one);
    //GAME CODE END

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) 
{}
}

现在,如果我想将按钮设置为半透明,以便通过按钮可以看到背景图像,代码会怎样.顺便说一句,请不要将半透明与透明混淆!

Now how would the code be if I wanted to set the button to be translucent so that the background image would be visible through the button. BTW please don't confuse Translucent with transparent!

推荐答案

setOpaque 不适用于 JButtons,正确的属性是:

setOpaque doesn't work for JButtons, the right property is:

button.setContentAreaFilled(false);

这篇关于JButton 的不透明度/半透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆