I wrote this sample code to illustrate the problem that I am encountering with my program.
I expect that you can move the JSplitPane slider to the left, over the edge of the buttons, compress this JPanel, and FlowLayout to wrap the buttons in the second line.
Instead, 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 compression, the buttons (I suppose) just run away from the right side of the JPanel, under the slider (I think because I, obviously I don’t see them).
What am I doing wrong?
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); } }
java layout-manager swing
Maarx
source share