我有如下简单的 GUI 代码,其中我想让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)
{}
}
现在,如果我想将按钮设置为半透明以便通过按钮可以看到背景图像,那么代码将如何。顺便说一句,请不要将半透明与透明混淆!