Java Swing BorderLayout调整了难度

时间:2012-09-04 16:02:49

标签: java swing layout resize

我想让我的屏幕分成两部分,所以我使用了带有东西部分的BorderLayout。我在调整大小时遇到​​了问题here我最终发现东西面板的宽度没有变化,北面和南面板的高度没有变化,两者都在中心面板中更改。

但是,我希望在调整大小时更改宽度和高度,并且并排放置两个面板。我尝试了各种级别的嵌套来尝试使其工作,但我认为它不适用于BorderLayout。

对于默认的布局管理器来说,这似乎应该很容易,但也许我应该尝试不同的布局(例如BoxLayout)来实现我想要的。

此外还有一些代码可以复制我正在讨论的问题(尝试调整窗口大小):

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

}

编辑:我不希望双方平等,大约2:1是我想要的比例。

4 个答案:

答案 0 :(得分:8)

为什么不试试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);
        });

    }
}

你会得到这个:

enter image description here

答案 1 :(得分:7)

您可以在{3}}中使用的内容为JButtonsJFrame会调整大小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(); } }

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

}

此外,最好从EDT - Event Dispatch Thread运行与GUI相关的代码,而不是从主线程运行。请阅读GridLayout,了解有关该主题的更多信息。

最新编辑:根据要求提供的评论

使用GridBagLayout指定要提供的大小

{{1}}

答案 2 :(得分:1)

如果你想保留你的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);
  }
}

现在使用ResizablePanel的实例包装您要调整大小的部分,然后您可以通过拖动细长按钮调整其大小。

请注意,这是代码用于调整放置在边框布局底部(PAGE_END)部分的面板的高度,但更改它以调整大小应该相当简单宽度。

答案 3 :(得分:0)

很抱歉回复旧帖子。 我的解决方法是仍然使用BorderLayout,但在调整组件的大小后将以下行扔进去

getLayout().layoutContainer(this);