简体   繁体   中英

Resize a JPanel BorderLayout Java

I try to use swing and I have a litle problem that I fail to solve. That I want to do is simple: I just want to had to JPanel in a JFrame using BorderLayout. The problem is my center panel is always placed above my North Jpanel. In fact whatever the size I give my north panel just have like 10 pixel, after the center pannel beggin (like on this image ).

Note: when I put my second panel south the first panel have enough place to be drawn but even if it has more place the second one also take just 10 pixel which is not enough (like on this image).

this is my Plateau constructor class which extends JFrame:

public Plateau(){
    super("arkanoid");

    this.setLayout(new BorderLayout());

    setFocusable(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    this.getContentPane().add(affich,BorderLayout.NORTH);
    this.getContentPane().add(jeu, BorderLayout.CENTER);



    setVisible(true);
    this.setResizable(false);  
    this.setMinimumSize(new Dimension(700,800));
    this.setLocationRelativeTo(null);

} 

here a part of my panel placed in center (the rest is dvariable modification and drawing functions):

public class Jeu extends JPanel {
    public Jeu(int score, int plateauX, int balleX, int balleY, boolean perdu){

        super();
    }
    public void paint(Graphics g){


        Graphics2D g2 = (Graphics2D)g;
        this.setSize(new Dimension(Width,Heigth));
    }
 }

and here is all my class supposed to be on north:

public class Affich extends JPanel {
    public Affich() {
        super();
        this.setSize(100,100);
    }

    public void paint(Graphics g){
        this.setSize(100,100);
        g.drawOval(0, 0, 50, 50);
    }
}

I hope I was clear enough

NEVER call setSize(...) or anything like it within a painting method. These methods should be for painting and painting only, and if you try to change size state, you can end up with a vicious cycle of endless calls -- set size which calls repaint which sets size, which calls repaint.

Instead:

  • Override the JPanel's paintComponent not paint, since paint is responsible for more than painting the JPanel, and overriding it can have unintended consequences on the JPanel's borders and child components.
  • Call the super's paintComponent within the override
  • Again, do only painting within a painting method
  • Don't set size but instead set preferred size and from code that is called once, and not within a painting method.

For example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;

public class MyDrawing {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            int affichW = 100;
            int affichH = 100;
            Affich affich = new Affich(affichW , affichH );
            Jeu jeu = new Jeu();

            frame.add(affich, BorderLayout.PAGE_START);
            frame.add(jeu, BorderLayout.CENTER);

            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

@SuppressWarnings("serial")
class Jeu extends JPanel {
    private static final int JEU_W = 600;
    private static final int JEU_H = 450;

    public Jeu(int score, int plateauX, int balleX, int balleY, boolean perdu) {
        super();
        setBorder(BorderFactory.createTitledBorder("Jeu"));
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();            
        } else {
            return new Dimension(JEU_W, JEU_H);
        }
    }

    public Jeu() {
        this(0, 0, 0, 0, false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        // draw with g2 here
    }
}

@SuppressWarnings("serial")
class Affich extends JPanel {
    private int width = 0;
    private int height = 0;

    public Affich(int width, int height) {
        super();
        this.width = width;
        this.height = height;
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        } else {
            return new Dimension(width, height);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        // draw smooth oval
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawOval(0, 0, 50, 50);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM