我编写此代码示例是为了说明我的程序遇到的问题。
我希望能够将 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/