简体   繁体   English

为什么 JPanel 大小不对?

[英]Why is JPanel size is wrong?

There is constructor of class extending JFrame:有 class 的构造函数扩展 JFrame:

import java.awt.*;
import javax.swing.*;

public class ChessFrame extends JFrame {
    public ChessFrame () {
        setSize(520, 520);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 1));

        // Add components
        getContentPane().add(new Board());
        pack();
        setVisible(true);
    }
}

And class extending JPanel:和 class 扩展 JPanel:

import javax.swing.*;

public class Board extends JPanel {
    public Board() {
        setSize(new Dimension(520, 520));
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.fillRect(0, 0, 520, 520);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(520, 520);
    }
}

As a result rectangle smaller then 520x520.结果矩形小于 520x520。 在此处输入图像描述

Size of black rectangle is about 496x510.黑色矩形的大小约为 496x510。 There is more:还有更多:

  1. getWidth() and getHegiht() written inside the Board class, returns 0 and 0 (so size of this JPanel into JFrame is 0x0) getWidth()getHegiht()写在Board class 中,返回 0 和 0(所以这个 JPanel 到 JFrame 的大小是 0x0)
  2. If I remove pack() , size of frame becomes 496x510 (black rectangle size)如果我删除pack() ,框架大小变为 496x510 (黑色矩形大小)

It's actually copypaste of official java tutorial: https://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html . It's actually copypaste of official java tutorial: https://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html .

Do I do something wrong or it's something related with java?我做错了什么还是与 java 有关? If it's second, why does this happen?如果是第二个,为什么会这样? Any help would be appreciated.任何帮助,将不胜感激。

在此处输入图像描述

This example only tries to establish the panel size once the frame (and panel) are visible on-screen.此示例仅在框架(和面板)在屏幕上可见时尝试确定面板大小。 It returns the exact size (300 pixels) set in the code.它返回代码中设置的确切大小(300 像素)。

import java.awt.*;
import javax.swing.*;

public class ChessBoard extends JPanel {
    
    int size = 300;
    JLabel sizeLabel = new JLabel();

    ChessBoard() {
        setBackground(Color.CYAN);
        setLayout(new GridBagLayout());
        add(sizeLabel);
    }
    
    public void showSize() {
        sizeLabel.setText(String.format("%1sx%1s", getWidth(), getHeight()));
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(size,size);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            ChessBoard cb = new ChessBoard();
            
            JFrame f = new JFrame(cb.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(cb);
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
            // delay showing size until app. is on-screen
            Runnable r1 = cb::showSize;
            SwingUtilities.invokeLater(r1);
        };
        SwingUtilities.invokeLater(r);
    }
}

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

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