为什么我的 JPanel 背景颜色不会改变?
Why won't my JPanel background color change?
我一直在尝试将框架中 JPanel 的背景设为黑色。我可以让我的 JButtons 显示在面板中,但背景仍然不是我设置的黑色。我不确定我做错了什么。
我试过将框架和所有面板的背景设置为黑色背景。我已确保已将所有内容设置为可见。我也试过将 setOpaque 设置为 true,但没有任何作用,所以我将其删除。
这是我的相框class:
public class GUI extends JFrame{
public static void main(String[] args) {
GUI frame = new GUI();
}
GUI(){
setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
setTitle("Polygon Maker");
DrawPane buttonPane = new DrawPane(false);
add("North", buttonPane);
DrawPane drawPane = new DrawPane(true);
add("Center", drawPane);
}
}
这是我的面板 class:
DrawPane() {
drawPane = false;
setLayout(new FlowLayout());
setBackground(Color.BLACK);
setVisible(true);
}
DrawPane(boolean draw) {
drawPane = draw;
setLayout(new FlowLayout());
setBackground(Color.BLACK);
setVisible(true);
if (!draw)
buttonSetup();
else {
addMouseListener(new Drawing());
current = new DrawPoly();
}
}
我希望得到一个顶部有 3 个按钮的空白黑框,但我得到的是一个顶部有 3 个按钮的空白白框。我试图避免这个问题,但现在它让我继续前进,我不知道是什么导致背景不显示。
您正在从 Jframe 扩展,因此您需要更改设置颜色的方式,从此
setBackground(Color.BLACK);
这样
getContentPane().setBackground(Color.BLACK);
我一直在尝试将框架中 JPanel 的背景设为黑色。我可以让我的 JButtons 显示在面板中,但背景仍然不是我设置的黑色。我不确定我做错了什么。
我试过将框架和所有面板的背景设置为黑色背景。我已确保已将所有内容设置为可见。我也试过将 setOpaque 设置为 true,但没有任何作用,所以我将其删除。
这是我的相框class:
public class GUI extends JFrame{
public static void main(String[] args) {
GUI frame = new GUI();
}
GUI(){
setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
setTitle("Polygon Maker");
DrawPane buttonPane = new DrawPane(false);
add("North", buttonPane);
DrawPane drawPane = new DrawPane(true);
add("Center", drawPane);
}
}
这是我的面板 class:
DrawPane() {
drawPane = false;
setLayout(new FlowLayout());
setBackground(Color.BLACK);
setVisible(true);
}
DrawPane(boolean draw) {
drawPane = draw;
setLayout(new FlowLayout());
setBackground(Color.BLACK);
setVisible(true);
if (!draw)
buttonSetup();
else {
addMouseListener(new Drawing());
current = new DrawPoly();
}
}
我希望得到一个顶部有 3 个按钮的空白黑框,但我得到的是一个顶部有 3 个按钮的空白白框。我试图避免这个问题,但现在它让我继续前进,我不知道是什么导致背景不显示。
您正在从 Jframe 扩展,因此您需要更改设置颜色的方式,从此
setBackground(Color.BLACK);
这样
getContentPane().setBackground(Color.BLACK);