JFrame / BoxLayout comportamiento extraño tamaño

Nadie :

Estoy tratando de establecer los tamaños de mis JButtons en un JPanel con BoxLayout correctamente, pero el comportamiento es más allá de raro. Se llevará a la altura de JButton.setPreferredSize, pero ignoran por completo el ancho. Esto también sólo funciona cuando todos los botones se ponen a la misma altura. Tan pronto como uno es más pequeño, éste se retrotrae a todos ellos a un cierto tamaño mínimo al azar (que ni siquiera es el mismo para todos los botones)

Mi código es la siguiente:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);

JPanel rightPanel = new JPanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

JButton bBookmarks = new JButton("Bookmarks");
bBookmarks.setPreferredSize(new Dimension(200, 100));
//more buttons with same size

leftPanel.add(bBookmarks);
//more buttons

JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
mainPanel.setDividerLocation(200);

frame.add(mainPanel);
frame.setResizable(false);
frame.setVisible(true);

Esto crea esta imagen.

esta imagen

El botón central es siempre más ancha que el resto también. Usando frame.pack () no hace nada excepto cambiar el tamaño del marco debido a que el panel de la derecha está vacía.

¿Qué estoy haciendo mal?

Editar: debe tener este aspecto:

c0der:

Divide y vencerás: romper el diseño en pequeño, fácil de contenedores de diseño. En este caso, no coloque los botones directamente en la izquierda ( BoxLayoutcontenedor), pero en un anidado JPanelusando GridLayoutgerente.
Esto asegura que todos los botones tienen el mismo tamaño:

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    //add all buttons to a panel using a GridLayout which shows all components having the same size
    JPanel buttons = new JPanel(new GridLayout(0,1));
    JButton bBookmarks = new JButton("Bookmarks");  buttons.add(bBookmarks);
    JButton bPlotter = new JButton("Plotter");      buttons.add(bPlotter);
    JButton bShips = new JButton("Ships");          buttons.add(bShips);

    //add buttons and text area to a panel using BoxLayout
    JPanel leftPanel = new JPanel();
    leftPanel.setPreferredSize(new Dimension(100,400));
    leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
    leftPanel.add(buttons);
    leftPanel.add(new TextArea(10,30));

    JPanel rightPanel = new JPanel();
    rightPanel.setPreferredSize(new Dimension(600,400));
    rightPanel.add(new JLabel("right pane"));

    JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, leftPanel, rightPanel);

    frame.add(mainPanel);
    frame.pack();
    frame.setVisible(true);

introducir descripción de la imagen aquí

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=389851&siteId=1
Recomendado
Clasificación