How can I use BoxLayout to do this?

后端 未结 1 1418

I\'ve already set up the menu (the centre boxes) perfectly, but I don\'t know how I can position the label. Currently what is happening is the label is going below the menu opti

1条回答
  •  礼貌的吻别
    2021-01-23 04:15

    You can get the desired results using the correct combinations of LayoutManagers. Don't limit yourself to one. Take advantage of the power than comes with combining/nesting layouts.

    enter image description here

    Here's the technique I used

    • A BoxLayout for the center buttons
    • A GridLayout(1, 5) for a panel the consists of everything but the buttom-left button which is in its own
    • FlowLayout with a LEADING alignment
    • All wrapped in the JFrame default BorderLayout

    See the program below

    import java.awt.*;
    import javax.swing.*;
    
    public class BoxLayoutDemo {
    
        JButton button1 = new JButton("Button1");
        public BoxLayoutDemo() {
            JPanel pane = new JPanel(new GridLayout(1, 5));
    
            Box box = Box.createVerticalBox();
            box.add(new JButton("Button 1"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 2"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 3"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 4"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 5"));
            box.add(Box.createVerticalStrut(20));
    
            pane.add(new JPanel());
            pane.add(new JPanel());
            pane.add(box);
            pane.add(new JPanel());
            pane.add(new JPanel());
    
            JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
            pane2.add(new JButton("ButtonButton"));
    
            JFrame frame = new JFrame("GridBag Box");
            frame.add(pane, BorderLayout.CENTER);
            frame.add(pane2, BorderLayout.SOUTH);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    new BoxLayoutDemo();
                }
            });
        }
    }
    

    Disclaimer : excuse the title of the frame. I was first thinking of combining GridBagLayout with the Box, but the way I did it was so much easier. Too lazy to change the title now that I've noticed it.


    For those who say what I did above is somewhat hackish (by adding empty panels), which maybe it is, you could also add the top panel and bottom panel to a containing panel with a BorderLayout and a preferred size, and it will give you similar result

        public BoxLayoutDemo() {
            JPanel pane = new JPanel();
    
            Box box = Box.createVerticalBox();
            box.add(new JButton("Button 1"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 2"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 3"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 4"));
            box.add(Box.createVerticalStrut(20));
            box.add(new JButton("Button 5"));
            box.add(Box.createVerticalStrut(20));
    
            pane.add(box);
    
            JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
            pane2.add(new JButton("ButtonButton"));
    
            JPanel panel = new JPanel(new BorderLayout()){
                public Dimension getPreferredSize() {
                    return new Dimension(400, 260);
                }
            };
            panel.add(pane, BorderLayout.CENTER);
            panel.add(pane2, BorderLayout.SOUTH);
    
            JFrame frame = new JFrame("Slitting using different layouts");
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    

    0 讨论(0)
提交回复
热议问题