简体   繁体   English

Java Swing BorderLayout 调整大小困难

[英]Java Swing BorderLayout resize difficulties

I want to have my screen split in two so I used a BorderLayout with East and West sections.我想将屏幕一分为二,所以我使用了带有东西部分的 BorderLayout。 I had problems resizing and here I eventually found out that width is not changed in the East and West panels and height is not changed in the North and South panels and both are changed in the Center panel.我在调整大小时遇到​​了问题,在这里我最终发现东面板和西面板中的宽度没有改变,北面板和南面板中的高度没有改变,并且两者都在中心面板中改变了。

However, I want both width and height to be changed upon resize, and have two panels side by side.但是,我希望在调整大小时更改宽度和高度,并且并排放置两个面板。 I have tried various levels of nesting to try getting it to work but I do not think it will work with BorderLayout.我已经尝试了各种级别的嵌套来尝试让它工作,但我认为它不适用于 BorderLayout。

It seems like this should be easy for the default layout manager but maybe I should try a different layout (eg BoxLayout) to achieve what I want.对于默认布局管理器来说,这似乎应该很容易,但也许我应该尝试不同的布局(例如 BoxLayout)来实现我想要的。

Also here is some code which replicates the problem I am talking about (try resizing the window):这里还有一些代码可以复制我正在谈论的问题(尝试调整窗口大小):

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

    public static void main(String[] args) {
        JFrame window = new Main();
        window.setVisible(true);
    }

    public Main() {
        JButton east = new JButton("East");
        JButton west = new JButton("West");

        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());

        content.add(east, BorderLayout.EAST);
        content.add(west, BorderLayout.WEST);

        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

}

Edit: I do not want the two sides to be equal, roughly 2:1 is the ratio which I want.编辑:我不希望两侧相等,大约 2:1 是我想要的比例。

What you can use in your case isGridLayout , here two JButtons will resize themselves as the JFrame resizes.您可以在您的情况下使用的是GridLayout ,这里有两个JButtons将在JFrame调整大小时调整自己的大小。

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame window = new Main();
                window.setVisible(true);
            }
        });        
    }

    public Main() {
        JButton east = new JButton("East");
        JButton west = new JButton("West");

        JPanel content = new JPanel();
        content.setLayout(new GridLayout(1, 2));

        content.add(east);
        content.add(west);

        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

}

Moreover, it's always best to run your GUI related code from the EDT - Event Dispatch Thread, and not from the Main Thread.此外,最好从 EDT - 事件调度线程运行与 GUI 相关的代码,而不是从主线程运行。 Do read Concurrency in Swing , for more info on the topic.请阅读Swing 中的并发,以获取有关该主题的更多信息。

LATEST EDIT : As per requested comment最新编辑:根据要求的评论

Use GridBagLayout to specify the size that you want to give使用 GridBagLayout 来指定你想要给的大小

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame window = new Main();
                window.setVisible(true);
            }
        });        
    }

    public Main() {
        JPanel east = new JPanel();
        east.setOpaque(true);
        east.setBackground(Color.WHITE);
        JPanel west = new JPanel();
        west.setOpaque(true);
        west.setBackground(Color.BLUE);

        JPanel content = new JPanel();
        content.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.3;
        gbc.weighty = 1.0;
        gbc.gridx = 0;
        gbc.gridy = 0;

        content.add(east, gbc);
        gbc.weightx = 0.7;
        gbc.gridx = 1;
        content.add(west, gbc);

        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

}

Why don't you try with JSplitPane :你为什么不试试JSplitPane

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

public class AppDemo {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            JButton eastButton = new JButton("East");
            JButton westButton = new JButton("West");
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, eastButton, westButton);

            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(splitPane, BorderLayout.CENTER);

            frame.setContentPane(content);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setPreferredSize(new Dimension(500, 400));
            frame.pack();
            frame.setVisible(true);
        });

    }
}

You will get this:你会得到这个:

在此处输入图片说明

If you want to keep your BorderLayout you can use something like the following object:如果您想保留 BorderLayout,您可以使用类似以下对象的内容:

public class ResizablePanel extends JPanel {

  public ResizablePanel(JComponent body) {

    setLayout(new BorderLayout());
    JButton resize = new JButton();
    resize.setPreferredSize(new Dimension(Integer.MAX_VALUE, 4));
    resize.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {
            Dimension preferredSize = ResizablePanel.this.getPreferredSize();
            ResizablePanel.this.setPreferredSize(new Dimension(preferredSize.width, preferredSize.height-e.getY()));
            ResizablePanel.this.revalidate();
        }
    });             
    add(resize, BorderLayout.PAGE_START);
    add(body, BorderLayout.CENTER);
  }
}

Now wrap the part you want to resize with an instance of ResizablePanel and you'll be able to resize it by dragging the thin button.现在用一个ResizablePanel实例包裹你想要调整大小的部分,你就可以通过拖动细按钮来调整它的大小。

Note that this is code is for resizing the height of a panel that you put at the bottom (PAGE_END) part of a border layout, but it should be fairly straightforward to change it for resizing the width.请注意,这是用于调整放置在边框布局底部 (PAGE_END) 部分的面板高度的代码,但更改它以调整宽度大小应该相当简单。

Sorry about replying to an old post.抱歉回复一个旧帖子。 My fix is to still use BorderLayout but to throw in the following line after the Component is resized我的解决方法是仍然使用 BorderLayout 但在调整组件大小后放入以下行

getLayout().layoutContainer(this);

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

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