如何防止JPANELS调整大小?

发布于 2025-01-26 19:53:54 字数 2426 浏览 2 评论 0原文

我想在其中有一个带有多个jpanels的jframe。 我使用gridbag,但是每次调整框架大小时,其中的所有内容都会被踩踏并划分以适合Jframe。 我只想拖放jframe的大小,但是jpanel上的图像以保持完全相同的大小。 (因此或多或少地揭示,但总是保持相同的维度)。

有什么办法吗? (虽然不需要代码,因为这不是一个特定的问题,但无论如何是代码。我希望电网始终保持确切的维度。设置和setProferables不起作用)

public class SetupBoard extends JFrame {
public SetupBoard() {
    int resolution = 10;
    System.out.println("This is the SetUpBoard");
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);

    
    
// setup different Panels //

    // BlackPiecePanel //
    JPanel bPieceTaken = new JPanel();
    JLabel bPieceTakenText = new JLabel("Black Pieces");
    bPieceTaken.add(bPieceTakenText);

    // WhitePiecePanel //
    JPanel wPieceTaken = new JPanel();
    JLabel wPieceTakenText = new JLabel("White Pieces");
    wPieceTaken.add(wPieceTakenText);

    // ChessBoardPanel //
    JPanel chessBoard = new JPanel(new GridLayout(8,8));
    chessBoard.setSize(new Dimension(300,300));

    
    JLabel[] squares = new JLabel[64];
    for (int i = 0; i < 64; i++) {
        squares[i] = new JLabel();
        squares[i].setBorder(BorderFactory.createLineBorder(Color.black));
        chessBoard.add(squares[i]);
    }
    
    
    // Empty Panel left & right //

    JPanel ePLeft = new JPanel();
    JLabel ePLeftText = new JLabel("empty Panel left");
    ePLeft.add(ePLeftText);

    JPanel ePRight = new JPanel();
    JLabel ePRightText = new JLabel("empty Panel right");
    ePRight.add(ePRightText);
    ePRight.setBackground(Color.RED);

    
    
    
    // Construct Layout //

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    
    // left Side //
    gc.weightx = 0.1;
    gc.weighty = 1.0;

    gc.gridx = 0;
    gc.gridy = 1;

    add(ePLeft, gc);
    
    // middle //
    gc.weightx = 0.8;
    gc.weighty = 0.1;
    
    gc.gridy = 0;
    gc.gridx = 1;
    add (bPieceTaken, gc);
    
    
    gc.weighty = 0.8;
    gc.gridy = 1;
    gc.fill = GridBagConstraints.BOTH;
    add (chessBoard, gc);
    
    gc.weighty = 0.1;
    gc.gridy = 2;
    gc.fill = GridBagConstraints.BASELINE;
    add (wPieceTaken, gc);
    
    
    // right Side //
    gc.weightx = 0.1;
    gc.weighty = 1;
    
    gc.gridx = 2;
    gc.gridy = 1;
    add (ePRight, gc);
    

    setVisible(true);

    System.out.println (chessBoard.getSize());
}

I want to have a jframe with multiple jpanels within them.
I use the gridbag, but every time I resize the frame, everything within it will be smooched and streched to fit the JFrame.
I just want to drag and drop the size of the JFrame, but the images on the JPanel to stay the exact same size. (so revealing more or less, but always staying the same dimension).

Is there any way to do that?
(while code is not needed since this is not a specific problem, here is the code anyways. I want the grid to always stay the exact dimensions. setSize and setPreferableSize did not work)

public class SetupBoard extends JFrame {
public SetupBoard() {
    int resolution = 10;
    System.out.println("This is the SetUpBoard");
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);

    
    
// setup different Panels //

    // BlackPiecePanel //
    JPanel bPieceTaken = new JPanel();
    JLabel bPieceTakenText = new JLabel("Black Pieces");
    bPieceTaken.add(bPieceTakenText);

    // WhitePiecePanel //
    JPanel wPieceTaken = new JPanel();
    JLabel wPieceTakenText = new JLabel("White Pieces");
    wPieceTaken.add(wPieceTakenText);

    // ChessBoardPanel //
    JPanel chessBoard = new JPanel(new GridLayout(8,8));
    chessBoard.setSize(new Dimension(300,300));

    
    JLabel[] squares = new JLabel[64];
    for (int i = 0; i < 64; i++) {
        squares[i] = new JLabel();
        squares[i].setBorder(BorderFactory.createLineBorder(Color.black));
        chessBoard.add(squares[i]);
    }
    
    
    // Empty Panel left & right //

    JPanel ePLeft = new JPanel();
    JLabel ePLeftText = new JLabel("empty Panel left");
    ePLeft.add(ePLeftText);

    JPanel ePRight = new JPanel();
    JLabel ePRightText = new JLabel("empty Panel right");
    ePRight.add(ePRightText);
    ePRight.setBackground(Color.RED);

    
    
    
    // Construct Layout //

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    
    // left Side //
    gc.weightx = 0.1;
    gc.weighty = 1.0;

    gc.gridx = 0;
    gc.gridy = 1;

    add(ePLeft, gc);
    
    // middle //
    gc.weightx = 0.8;
    gc.weighty = 0.1;
    
    gc.gridy = 0;
    gc.gridx = 1;
    add (bPieceTaken, gc);
    
    
    gc.weighty = 0.8;
    gc.gridy = 1;
    gc.fill = GridBagConstraints.BOTH;
    add (chessBoard, gc);
    
    gc.weighty = 0.1;
    gc.gridy = 2;
    gc.fill = GridBagConstraints.BASELINE;
    add (wPieceTaken, gc);
    
    
    // right Side //
    gc.weightx = 0.1;
    gc.weighty = 1;
    
    gc.gridx = 2;
    gc.gridy = 1;
    add (ePRight, gc);
    

    setVisible(true);

    System.out.println (chessBoard.getSize());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2025-02-02 19:53:54

您可以尝试构建自己的layoutmanager,然后您将对组件的调整大小以及每次调整容器的位置进行位置。
您应该通过创建一个实现LayoutManger接口的类来做到这一点,并根据您的设计根据需要实现其中的方法。

如果您尚未完成此操作,则可以看到有关此主题的Oracle文章:

创建自定义布局管理器

You can try to build your own LayoutManager, and then you will have full control of how your components resized and where they positioned every time your container resized.
You should do this by creating a class which implements the LayoutManger interface, and implement the method within it as you need, according to your design.

If you haven't done this already, you can see this oracle article on this subject:

Creating a Custom Layout Manager

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文