⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boxlayoutdemo.java

📁 这是一个很好的java教程
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class BoxLayoutDemo {    protected static int NUM_COMPONENTS = 3;    protected static float[] xAlignment = {Component.LEFT_ALIGNMENT,                                 Component.CENTER_ALIGNMENT,                                 Component.RIGHT_ALIGNMENT};    protected static float[] hue = {0.0f, 0.33f, 0.67f};    protected static boolean restrictSize = true;    protected static boolean sizeIsRandom = false;    protected static BLDComponent[] bldComponent =         new BLDComponent[NUM_COMPONENTS];    public static void main(String[] args) {        final JPanel panel = new JPanel();        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));        //Create the rectangles.        int shortSideSize = 15;        for (int i = 0; i < NUM_COMPONENTS; i++) {            if (sizeIsRandom) {                shortSideSize = (int)(30.0 * Math.random()) + 30;            } else {                shortSideSize += 10;            }            bldComponent[i] = new BLDComponent(xAlignment[i], hue[i],                                              shortSideSize,                                              restrictSize,                                             sizeIsRandom,                                             String.valueOf(i));            panel.add(bldComponent[i]);        }        //Create the instructions.        JLabel label = new JLabel("Click a rectangle to "                                + "change its X alignment.");        JCheckBox cb = new JCheckBox("Restrict maximum rectangle size.");        cb.setSelected(restrictSize);        cb.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent e) {                if (e.getStateChange() == ItemEvent.SELECTED) {                    restrictSize = true;                } else {                    restrictSize = false;                }                notifyBLDComponents();            }        });        JFrame f = new JFrame("BoxLayoutDemo");        Container contentPane = f.getContentPane();        contentPane.add(panel, BorderLayout.CENTER);        panel.setBorder(BorderFactory.createLineBorder(Color.red));        Box box = Box.createVerticalBox();        box.add(label);        box.add(cb);        contentPane.add(box, BorderLayout.SOUTH);        f.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        f.pack();        f.setVisible(true);    }    static public void notifyBLDComponents() {        for (int i = 0; i < NUM_COMPONENTS; i++) {            bldComponent[i].setSizeRestriction(restrictSize);        }        bldComponent[0].revalidate();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -