我有一个简单的GUI代码如下,其中我想使JButton
一个半透明,以便JButton
后面的图像可见!JButton的不透明度/半透明度?
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)
{}
}
现在如果我想将按钮设置为半透明以便背景图像通过按钮可见,代码如何。顺便说一句,请不要混淆半透明与透明!
我认为这个链接可以帮助你http://stackoverflow.com/questions/7373345/how-to-create-partly-transparent-jbutton-on-fully-transparent-jframe – hmatar
为了更好的帮助更快,发布一个[SSCCE ](http://sscce.org/)(添加一个'main(String])',包含'ImageP anel'&在代码中创建一个图像)。 –
为什么不说明半透明和透明之间的区别以避免混淆。 –