繁体   English   中英

调整JPanel BorderLayout Java的大小

[英]Resize a JPanel BorderLayout Java

我尝试使用秋千,但有一个无法解决的小问题。 我想做的很简单:我只想使用BorderLayout在JFrame中进行JPanel。 问题是我的中央面板始终放置在North Jpanel上方。 实际上,无论我给北面板提供多少尺寸,在中央面板开始后(如该图所示 ),它只有10像素。

注意:当我将第二个面板放在南部时,第一个面板有足够的位置可以绘制,但是即使第二个面板有更多的位置,它也仅占用了10个像素,这是不够的(就像这张图片一样)。

这是我的Plateau构造函数类,它扩展了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);

} 

这是我面板的一部分放在中间(其余部分是可变的修改和绘图功能):

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));
    }
 }

这是我班上所有应该上的课:

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);
    }
}

我希望我足够清楚

永远不要在绘画方法中调用setSize(...)或类似的东西。 这些方法仅适用于绘画,如果尝试更改大小状态,可能会陷入无休止的恶性循环-设置大小(调用重画图)和设置大小(重画图)。

代替:

  • 覆盖JPanel的paintComponent 而不是 paint,因为paint不仅要绘制JPanel,而且覆盖它可能会对JPanel的边界和子组件产生意想不到的后果。
  • 在覆盖范围内调用上级的paintComponent
  • 同样, 在绘画方法内进行绘画
  • 不要设置大小,而是从一次调用的代码中而不是在绘画方法中设置首选的大小和代码。

例如

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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