誰もいません:
私は正しくBoxLayoutでのJPanelの私のJButtonがのサイズを設定しようとしていますが、動作が奇妙を超えています。それはJButton.setPreferredSizeからの高さを取るが、完全幅を無視します。すべてのボタンが同じ高さに設定されているときにものみ動作します。1が小さくなるや否や、それは(すべてのボタンにしても同じではありません)いくつかのランダムな最小サイズにそれらのすべてを戻ります
私のコードはこれです:
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);
これは、このイメージを作成します。
中央のボタンは、常に他の部分よりも幅が広いにもあります。frame.pack()を使用すると、右側のパネルが空であるため、フレームのサイズを変更する以外何もしません。
何が私が間違っているのでしょうか?
編集:次のようになります。
c0der:
分割統治:レイアウトコンテナに小さな、簡単にデザインを破ります。この場合、直接左(のボタンを配置しないでくださいBoxLayout
)コンテナが、入れ子にしてJPanel
使用してGridLayout
マネージャー。
すべてのボタンが同じサイズを持っている。この保証します。
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);