java Swing第5集:BoxLayout

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Swing第5集:BoxLayout相关的知识,希望对你有一定的参考价值。

import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Episode 5");
        frame.setBounds(400,400,400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//        Container content = frame.getContentPane();
//        BoxLayout boxLayout = new BoxLayout(content, BoxLayout.Y_AXIS); //First param is the container it will be in, second is the direction it will be laid out in
//        content.setLayout(boxLayout);
//        for (int i = 1;i<5;i++){
//            content.add(new JButton("Button " + i));
//        }
        //The upper part is comemented out because you generally don't want to do it. Whenever you create a box layout, a direction must be specified, so that means you can only have one direction in your container. This may be problematic if you want multiple rows or column in the same window. What you would really want to do is create a container for each row or column.
        JPanel panel1 = new JPanel(); //Our little subcontainer
        BoxLayout layout1 = new BoxLayout(panel1, BoxLayout.X_AXIS); //Hooks the boxlayout with our panel and sets the layout to vertical
        panel1.setLayout(layout1); //Sets the layout to the panel. Before this line it was a BorderLayout
        //Lets add a couple of buttons and see how they are placed
        for (int i = 1;i<10;i++){
            panel1.add(new JButton("Button " + i));
        }

        JPanel panel2 = new JPanel();
        BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.X_AXIS);
        panel2.setLayout(layout2);
        for (int i = 1;i<10;i++){
            panel2.add(new JButton("[G2]Button " + i));
        }

        //Using Boxs this time instead of panels. Boxes are specially made for boxlayouts so you dont have to do as much work to set them up
        Box box1 = Box.createHorizontalBox();
        //Add buttons to our box
        for (int i = 1;i<10;i++){
            box1.add(new JButton("[G3]Button " + i));
        }

        //Adds the panels to the main container
        Container container = frame.getContentPane();
        container.add(panel1, BorderLayout.NORTH); //Sets to the north because default is center. If we leave multiple at the center of course only the most recently added one will be shown.
        container.add(panel2, BorderLayout.CENTER);
        container.add(box1, BorderLayout.SOUTH); //Adds our box container and sets it to be on the bottom

        frame.pack(); //At this point just auto-sizes the window
        frame.setVisible(true);

    }
}
import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Episode 5");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container content = frame.getContentPane();

        Box box1 = Box.createHorizontalBox();

        JButton button1 = new JButton("Button");
        JButton button2 = new JButton("Button");
        JButton button3 = new JButton("Button");
        JButton button4 = new JButton("Button");
        JButton button5 = new JButton("Button");

        box1.add(button1);
        box1.add(Box.createHorizontalGlue()); //Glues are expandable spaces
        box1.add(button2);
        box1.add(Box.createHorizontalStrut(100)); //Struts are spaces that cannot change size. They can be vertical too.
        box1.add(button3);
        box1.add(Box.createRigidArea(new Dimension(50, 50))); //Rigid areas are invisible spaces that can be specified with both a width and a height. Must use dimension object
        box1.add(button4);

        content.add(box1, BorderLayout.NORTH);

        frame.pack();
        frame.setVisible(true);
    }
}
import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("BoxLayout with Glue");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        Box hBox = Box.createHorizontalBox();
        hBox.add(new JButton("<<First"));
        hBox.add(new JButton("<Previous"));
        hBox.add(Box.createHorizontalGlue());
        hBox.add(new JButton("Next>"));
        hBox.add(new JButton("Last>>"));

        contentPane.add(hBox, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}

以上是关于java Swing第5集:BoxLayout的主要内容,如果未能解决你的问题,请参考以下文章

如何防止 Java Swing BoxLayout 中的 JTextFields 扩展?

java Swing组件和事件处理

运用 BoxLayout 进行 Swing 控件布局

将JLabel添加到使用BoxLayout的JFrame

Java AWT 图形界面编程LayoutManager 布局管理器 ⑥ ( BoxLayout 布局 )

Java AWT 图形界面编程LayoutManager 布局管理器 ⑥ ( BoxLayout 布局 )