如何在其JSplitPane中创建此FlowLayout包装? [英] How do I make this FlowLayout wrap within its JSplitPane?

查看:155
本文介绍了如何在其JSplitPane中创建此FlowLayout包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这段代码示例来说明我在程序中遇到的问题。

I wrote this code sample to illustrate a problem I'm having with my program.

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

I expect to be able to slide the JSplitPane's slider bar to the left, beyond the edge of the buttons, compressing that JPanel, and have the FlowLayout wrap the buttons to a second row.

相反,JSplitPane不允许我移动滑块超过屏幕上最右边的按钮,如果我调整整个JFrame的大小以强制压缩,按钮(我猜)就是在滑动条下方的JPanel的右侧运行(我猜,因为我显然看不到他们)。

Instead, the JSplitPane does not allow me to move the slider past the rightmost button on the screen, and if I resize the entire JFrame to force the compression, the buttons (I presume) are just running off the righthand side of the JPanel, underneath the slider bar (I guess, because I obviously cannot see them).

我做错了什么?

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


推荐答案


除了FlowLayout之外还有另一个布局管理器可以做我想要的吗?

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

换行布局应该有效。

这篇关于如何在其JSplitPane中创建此FlowLayout包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆