Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 强制JButton不绘制背景_Java_Swing_Jbutton_Jcomponent - Fatal编程技术网

Java 强制JButton不绘制背景

Java 强制JButton不绘制背景,java,swing,jbutton,jcomponent,Java,Swing,Jbutton,Jcomponent,我正在编写一个小程序,这基本上是我第一次使用JComponent来绘制东西。我将组件的背景设置为黑色 但只要我在里面画一个JButton,它就会被默认的灰色覆盖。我已经找了一个小时了,似乎找不到任何答案 尝试将按钮设置为不透明使用setOpaque(布尔不透明);方法 我不确定我是否是对的,但我可能是对的 编辑: 尝试使用以下方法: button.setBorderPainted(false); button.setContentAreaFilled(false); button.se

我正在编写一个小程序,这基本上是我第一次使用
JComponent
来绘制东西。我将组件的背景设置为黑色

但只要我在里面画一个
JButton
,它就会被默认的灰色覆盖。我已经找了一个小时了,似乎找不到任何答案


尝试将按钮设置为不透明使用setOpaque(布尔不透明);方法

我不确定我是否是对的,但我可能是对的

编辑:

尝试使用以下方法:

 button.setBorderPainted(false); 
 button.setContentAreaFilled(false); 
 button.setFocusPainted(false); 
 button.setOpaque(false);

您看到的是添加了
JComponent
的框架,因此如果您想要黑色背景框架,则需要设置JFrame的背景颜色

大概是这样的:

JFrame frame = new JFrame();
frame.add(new GUI());
frame.pack();
frame.getContentPane().setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

是班上东西最多的。在主方法中,我创建了一个框架,并向其中添加了一个实例。哦,其他方法使它看起来像btw,是没有按钮的样子,是的,我添加了一个aero效果,但没有它也无法工作。好的,试试按钮。setContentAreaFilled(true);但我不完全确定。这些都不起作用:
button.setboordpainted(false);按钮。setContentAreaFilled(假);按钮。SetFocuspaint(假);按钮。设置不透明(假)您指的是按钮背景的灰色,还是整个面板背景的灰色?您可能需要明确设置面板的背景(如果您正在谈论的是背景)
getContentPane().setBackground(Color.BLACK)
。这是我的框架的代码:
JFrame f=new JFrame();GUI=新GUI();f.add(gui);f.包装();f.立根背景(颜色:黑色);f.设置尺寸(600400);f.可设置大小(假);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(真)这起作用了:
frame.getContentPane().setBackground(Color.black)谢谢。唯一的问题是我不能再使用aero效果了,因为我不能设置透明的背景色。那应该没问题。