2
這是下面的代碼,問題在於JScrollPane保持其首選大小並且不會調整BorderLayout.CENTER區域的大小。當然,我希望它填充該區域,然後使用它滾動表格中的內容。BorderLayout.CENTER中的JScrollPane沒有調整大小
frame = new JFrame();
frame.setBounds(100, 100, 500, 400);
JPanel panelCenter = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelCenter.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
panelCenter.setPreferredSize(new Dimension(300, 400));
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setLayout(null);
panelRight.setPreferredSize(new Dimension(200, 400));
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
table = new JTable(new MyTableModel());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
基本上,你必須學習LayoutManagers的確切行爲。然後_always_使用LayoutManager(您的右側面板沒有)。 _千萬-ever_使用任何setXXSize方法(原因見http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing) – kleopatra