簡體   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