How does one make a component in the .CENTER of a Borderlayout occupy all center space and resize with the app?

前端 未结 1 807
南旧
南旧 2020-12-21 10:52

My app/JFrame, using Borderlayout, has a toolbar at the top or north, a statusbar at the bottom or south and a JPanel.JTabbedPane.JScrollPane.JTable in the center. The JPane

相关标签:
1条回答
  • 2020-12-21 11:07

    This is characteristic of retaining the default FlowLayout of JPanel and adding the panel to the center of a BorderLayout. The example below compares panels having FlowLayout, the default, or GridLayout. For contrast, the two are added to a GridLayout, which allows expansion in a manner similar to that of BorderLayout center.

    layout comparison image

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTree;
    
    /** @see http://stackoverflow.com/questions/5822810 */
    public class LayoutPanel extends JPanel {
    
        public LayoutPanel(boolean useGrid) {
            if (useGrid) {
                this.setLayout(new GridLayout());
            } // else default FlowLayout
            this.add(new JTree());
        }
    
        private static void display() {
            JFrame f = new JFrame("LayoutPanels");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLayout(new GridLayout(1, 0));
            f.add(new LayoutPanel(false));
            f.add(new LayoutPanel(true));
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    display();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题