Java SWING. Не отображается компонент пока не наведешь на него мышкой
Создал класс Bone, наследником от JPanel. При его добавлении в другую панель не отображается пока не наведешь на него мышкой. метод paintComponent() перегружал, метод super.paintComponent(g) вызывал. Но все равно остается та же проблема.
public class MainFrame extends JFrame {
MainFrame() {
setTitle("Домино");
setSize(new Dimension(1920, 1080));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//setResizable(false);
setExtendedState(JFrame.MAXIMIZED_BOTH);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 7; j++) {
Image img = null;
Singleton.getCollections().getBones().add(new Bone(img,i,j));
}
}
System.out.println(Singleton.getCollections().getBones().size());
JPanel south = new JPanel();
JPanel north = new JPanel();
JPanel west = new JPanel();
JPanel east = new JPanel();
GamePanel center = new GamePanel();
south.setPreferredSize(new Dimension(1620, 150));
north.setPreferredSize(new Dimension(1620, 150));
west.setPreferredSize(new Dimension(150, 780));
east.setPreferredSize(new Dimension(150, 780));
south.setLayout(new FlowLayout());
for (int i = 0; i < 8; i++) {
Bone forSouth = new Bone(null, 1,2); //Тут создаю компонент.
forSouth.setBackground(Color.RED);
forSouth.setPreferredSize(new Dimension(80, 130));
forSouth.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
forSouth.setBackground(Color.WHITE);
System.out.println(center.getSize());
}
@Override
public void mouseExited(MouseEvent e) {
forSouth.setBackground(Color.RED);
}
@Override
public void mouseClicked(MouseEvent e) {
south.remove(forSouth);
south.repaint();
south.doLayout();
center.add(forSouth);
forSouth.setBounds(center.getWidth() / 2 - 50, center.getHeight() / 2 - 50, 56, 91);
}
});
south.add(forSouth); //Тут добавляю его в панель.
}
for (int i = 0; i < 8; i++) {
JPanel forSouth = new JPanel();
forSouth.setBackground(Color.RED);
forSouth.setPreferredSize(new Dimension(80, 130));
forSouth.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
forSouth.setBackground(Color.WHITE);
}
@Override
public void mouseExited(MouseEvent e) {
forSouth.setBackground(Color.RED);
}
@Override
public void mouseClicked(MouseEvent e) {
north.remove(forSouth);
north.repaint();
center.add(forSouth);
forSouth.setBounds(751, 326, 91, 56);
}
});
north.add(forSouth);
}
//west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
for (int i = 0; i < 15; i++) {
JPanel forSouth = new JPanel();
forSouth.setBackground(Color.BLUE);
forSouth.setPreferredSize(new Dimension(120, 60));
forSouth.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
forSouth.setBackground(Color.WHITE);
}
@Override
public void mouseExited(MouseEvent e) {
forSouth.setBackground(Color.BLUE);
}
@Override
public void mouseClicked(MouseEvent e) {
west.remove(forSouth);
west.repaint();
center.add(forSouth);
forSouth.setBounds(842, 326, 91, 56);
}
});
if (i >= 11) west.setPreferredSize(new Dimension(280, 780));
west.add(forSouth);
}
for (int i = 0; i < 11; i++) {
JPanel forSouth = new JPanel();
forSouth.setBackground(Color.BLUE);
forSouth.setPreferredSize(new Dimension(120, 60));
forSouth.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
forSouth.setBackground(Color.WHITE);
}
@Override
public void mouseExited(MouseEvent e) {
forSouth.setBackground(Color.BLUE);
}
@Override
public void mouseClicked(MouseEvent e) {
east.remove(forSouth);
east.repaint();
center.add(forSouth);
forSouth.setBounds(877, 235, 56, 91);
}
});
east.add(forSouth);
}
add(south, BorderLayout.SOUTH);
add(north, BorderLayout.NORTH);
add(west, BorderLayout.WEST);
add(east, BorderLayout.EAST);
add(center, BorderLayout.CENTER);
south.setBackground(Color.DARK_GRAY);
north.setBackground(Color.BLUE);
east.setBackground(Color.GREEN);
west.setBackground(Color.RED);
setVisible(true);
}
}
Класс Bone:
public void setImage(Image image) {
this.image = image;
}
Bone(Image image, int firstCount, int secondCount) {
if (firstCount == secondCount) isDouble = true;
this.image = image;
this.firstCount = firstCount;
this.secondCount = secondCount;
setBackground(Color.RED);
}
public boolean isDouble() {
return isDouble;
}
public boolean isLeft() {
return isLeft;
}
public boolean isDownLeft() {
return isDownLeft;
}
public boolean isDownRight() {
return isDownRight;
}
public boolean isUpLeft() {
return isUpLeft;
}
public boolean isUpRight() {
return isUpRight;
}
public boolean isHorizontal() {
return isHorizontal;
}
public int getFirstCount() {
return firstCount;
}
public int getSecondCount() {
return secondCount;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//g.drawImage(image, 0, 0, 56, 91, null);
}
}
UPDATE: При использовании экземпляра обычного JPanel(this.add(new JPanel())) все работает отлично.