[英]How can I change the background color of a jPanel containing JRadioButtons?
我有一个Java作业,需要根据用户选择的单选按钮列出不同的颜色来更改GUI的背景色。 我的程序有一个JFrame可以容纳所有内容,然后在该帧内包含3个JPanels(1个指令区域,1个单选按钮网格,1个结果文本字段)。
我的动作侦听器当前正在使用以下语句设置背景色:getContentPane()。setBackground(Color.decode(colorMap.get(btn.getName())));
JFrame的背景和三个面板中的两个面板成功更改为正确的颜色,但是装有JRadioButton的面板根本不会改变!
我尝试更改不透明设置,尝试将面板的背景色设置为(0,0,0,0),但到目前为止,这些都不对我有用。 有人对我可能会尝试的内容有建议吗?
我没有足够的声誉来发布图片,但是如果发现我在说什么对您有所帮助,请告诉我,我可以通过电子邮件给您截图。
谢谢!
您还必须另外设置所有JRadioButtons.setOpaque(false)
。
一个JRadioButton不透明和一个不透明的示例:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class XFrame
extends JFrame
{
public XFrame(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JRadioButton b1 = new JRadioButton("Non opaque button");
// button must not be opaque
b1.setOpaque(false);
// this button is opaque and does not use the background color of the frame
JRadioButton b2 = new JRadioButton("Opaque button");
JPanel p1 = new JPanel();
// panel must be non opaque also
p1.setOpaque(false);
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
p1.add(b1);
p1.add(b2);
add(p1, BorderLayout.CENTER);
getContentPane().setBackground(Color.red);
setSize(200, 200);
}
public static void main(String[] args)
{
XFrame frame = new XFrame("Test");
frame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.