[英]Java setBackground() confusion
I'm new to the Java swing library and I'm currently having some trouble setting the background of my JFrame. 我是Java swing库的新手,我目前在设置JFrame的背景时遇到了一些麻烦。
I have read jframe-setbackground-not-working-why and the link inside it, but it doesn't seem to fit here. 我已经阅读了jframe-setbackground-not-working-why和它里面的链接,但它似乎不适合这里。
Here is my codes: 这是我的代码:
public class Board extends JPanel{
public enum pointType{
EMPTY,
CARRIER,
BALL;
}
private class Point{
int x;
int y;
pointType type;
public void paint (Graphics2D g2){
// color changes depends on pointType
g2.setColor(Color.WHITE);
g2.fillOval(x,y,25,25);
}
}
Point[][] myBoard;
public Board(){
//constructor, myBoard = 2d List of points
}
//.. other methods and class variables
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
for(int k =HEIGHT; k>=0; k--){
for(int i=WIDTH; i>=0; i--){
// call paint method for each points on board
myBoard[i][k].print(g2);
}
}
}
public static void main(String[] args){
Board board = new Board();
JFrame myFrame = new Jframe("Game");
myFrame.add(board);
board.setBackground(Color.YELLOW);
myFrame.setVisible(true);
mtFrane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
My code successfully prints all the points according to their pointType, but the board color is not set correctly (still default background). 我的代码根据其pointType成功打印所有点,但未正确设置电路板颜色(仍为默认背景)。
So here are the questions: 所以这里是问题:
1) How should I set the background correctly? 1)我应该如何正确设置背景?
2) I feel that my code is not using JPanels/JFrames/Graphics together correctly, if that's the case, any suggestions on how I improve my code structures? 2)我觉得我的代码没有正确使用JPanels / JFrames / Graphics,如果是这样的话,有关如何改进代码结构的任何建议吗?
Use paintComponent()
instead of paint()
使用paintComponent()
而不是paint()
public class Board extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
...
}
}
For more info have a look at below posts: 欲了解更多信息,请查看以下帖子:
The default paintComponent()
method in JPanel
uses the background color stored in the JPanel
's instance variable; 默认paintComponent()
在方法JPanel
使用存储在背景颜色JPanel
的实例变量; however, your overridden paintComponent()
method doesn't make use of the instance variable and so changing it with setBackground()
won't do anything. 但是,重写的paintComponent()
方法不会使用实例变量,因此使用setBackground()
更改它将不会执行任何操作。
If you want to stick with overriding the paintComponent()
method, you should draw a box filling the entire area of the JPanel
with the color which you want inside the paintComponent()
method. 如果你想坚持重写paintComponent()
方法,你应该画一个框填充的整个区域JPanel
与你想要的内部颜色paintComponent()
方法。
The new paintComponent()
method for Board
would look like this: Board
的新paintComponent()
方法如下所示:
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.YELLOW);
g2.fillRect(0, 0, getWidth(), getHeight()); // Fill in background
// Do everything else
for(int k =HEIGHT; k>=0; k--){
for(int i=WIDTH; i>=0; i--){
// call paint method for each points on board
myBoard[i][k].print(g2);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.