java - 如何让这个 FlowLayout 包裹在它的 JSplitPane 中?

标签 java swing layout-manager

我编写此代码示例是为了说明我的程序遇到的问题。

我希望能够将 JSplitPane 的 slider 向左滑动,超出按钮的边缘,压缩 JPanel,并让 FlowLayout 将按钮换行到第二行。

相反,JSplitPane 不允许我将 slider 移过屏幕上最右边的按钮,如果我调整整个 JFrame 的大小以强制压缩,按钮(我假设)会从JPanel,在 slider 下方(我猜,因为我显然看不到它们)。

我做错了什么?

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Driver implements Runnable {
    public static void main(String[] args) {
        (new Driver()).run();
    }
    public void run() {
        try {
            go();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    private void go() throws Exception {
        JFrame jframe = new JFrame("FlowLayoutTest");
        JPanel left = new JPanel();
        left.setBackground(Color.RED);
        left.setLayout(new BorderLayout());
        JPanel right = new JPanel();
        right.setBackground(Color.BLUE);
        right.setLayout(new BorderLayout());
        JSplitPane topmost =
            new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        jframe.setContentPane(topmost);
        JPanel bottomleft = new JPanel();
        bottomleft.setBackground(Color.GREEN);
        bottomleft.setLayout(new FlowLayout());
        left.add(bottomleft, BorderLayout.PAGE_END);
        for (int i = 0; i < 10; i++) {
            bottomleft.add(new JButton("" + i));
        }
        jframe.pack();
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

最佳答案

Is there another layout manager besides FlowLayout that will do what I'm looking for?

Wrap Layout应该可以。

关于java - 如何让这个 FlowLayout 包裹在它的 JSplitPane 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5709690/

相关文章:

java - Eclipse Oxygen 项目链接到类文件而不是源文件

java - 找不到基本名称/ bundle 的 bundle ,语言环境 en_US

swing - 对象的 JTree?

Java 布局,组件始终位于右上角

java - 如何使用 GridBagLayout 让所有组件出现在 JPanel 的中心?

Java:在绘制主 Canvas 的类之外使用 Graphics 进行绘制

java - MongoDB 中的查询场景

Java 动画 JLabel

java - PaintComponent 花费很长时间,占用了 Swing 事件调度线程

java - Jframe 内的 JPanel