setBackgroundColor 不起作用
setBackgroundColor is not working
我正在尝试为 JFrame 框架设置背景颜色,但它不起作用。
我在这里错过了什么?
这是代码:
public class PingPong extends JPanel{
private static final long serialVersionUID = 1L;
Ball ball = new Ball(this);
Table table = new Table(this);
Player player = new Player(this);
PC pc = new PC(this);
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
table.paint(g);
ball.repaint(g);
player.repaint(g);
pc.repaint(g);
}
public static void main(String[] args){
/* Creating the frame */
JFrame frame = new JFrame();
frame.setTitle("Ping Pong!");
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PingPong());
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setVisible(true);
}
}
就是不会变色..
尝试添加:
frame.getContentPane().setOpaque(true);
由于您将 JPanel
(PingPong 对象)添加到 JFrame
,因此 JPanel
在 JFrame
之上,因此 [=11= 的颜色] 变得不可见。
将 setBackground(Color.DARK_GRAY);
应用于您的 PingPong 对象。
我正在尝试为 JFrame 框架设置背景颜色,但它不起作用。 我在这里错过了什么? 这是代码:
public class PingPong extends JPanel{
private static final long serialVersionUID = 1L;
Ball ball = new Ball(this);
Table table = new Table(this);
Player player = new Player(this);
PC pc = new PC(this);
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
table.paint(g);
ball.repaint(g);
player.repaint(g);
pc.repaint(g);
}
public static void main(String[] args){
/* Creating the frame */
JFrame frame = new JFrame();
frame.setTitle("Ping Pong!");
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PingPong());
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setVisible(true);
}
}
就是不会变色..
尝试添加:
frame.getContentPane().setOpaque(true);
由于您将 JPanel
(PingPong 对象)添加到 JFrame
,因此 JPanel
在 JFrame
之上,因此 [=11= 的颜色] 变得不可见。
将 setBackground(Color.DARK_GRAY);
应用于您的 PingPong 对象。