【发布时间】:2021-11-01 10:27:57
【问题描述】:
我有如下简单的 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)
{}
}
现在,如果我想将按钮设置为半透明以便通过按钮可以看到背景图像,那么代码会如何。顺便说一句,请不要将半透明与透明混淆!
【问题讨论】:
-
我觉得这个链接可以帮到你stackoverflow.com/questions/7373345/…
-
为了尽快获得更好的帮助,请发布SSCCE(添加
main(String]),包含ImagePanel并在代码中创建图像)。 -
为什么不说明半透明和透明之间的区别以避免混淆。
-
看看这个例子Translucent JButton,不确定这是不是你想要的!!
-
@GagandeepBali 这是一个很棒的链接! +1
标签: java swing transparency jbutton translucency