zoukankan      html  css  js  c++  java
  • java 网格组布局

    简介

    GridBagLayout
    网格组布局
    java 核心编程

    code

    /*
     * @Author: your name
     * @Date: 2020-11-08 12:48:42
     * @LastEditTime: 2020-11-08 12:49:12
     * @LastEditors: your name
     * @Description: In User Settings Edit
     * @FilePath: /java/swing/FontFrame.java
     */
    package swing;
    
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    
    /**
     * A frame that uses a grid bag layout to arrange font selection components.
     */
    public class FontFrame extends JFrame {
        public static final int TEXT_ROWS = 10;
        public static final int TEXT_COLUMNS = 20;
    
        private JComboBox<String> face;
        private JComboBox<Integer> size;
        private JCheckBox bold;
        private JCheckBox italic;
        private JTextArea sample;
    
        public FontFrame() {
            var layout = new GridBagLayout();
            setLayout(layout);
    
            ActionListener listener = event -> updateSample();
    
            // construct components
    
            var faceLabel = new JLabel("Face: ");
    
            face = new JComboBox<>(new String[] { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" });
    
            face.addActionListener(listener);
    
            var sizeLabel = new JLabel("Size: ");
    
            size = new JComboBox<>(new Integer[] { 8, 10, 12, 15, 18, 24, 36, 48 });
    
            size.addActionListener(listener);
    
            bold = new JCheckBox("Bold");
            bold.addActionListener(listener);
    
            italic = new JCheckBox("Italic");
            italic.addActionListener(listener);
    
            sample = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
            sample.setText("The quick brown fox jumps over the lazy dog");
            sample.setEditable(false);
            sample.setLineWrap(true);
            sample.setBorder(BorderFactory.createEtchedBorder());
    
            // add components to grid, using GBC convenience class
    
            add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
            add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(1));
            add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
            add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(1));
            add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
            add(italic, new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
            add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
            pack();
            updateSample();
        }
    
        public void updateSample() {
            var fontFace = (String) face.getSelectedItem();
            int fontStyle = (bold.isSelected() ? Font.BOLD : 0) + (italic.isSelected() ? Font.ITALIC : 0);
            int fontSize = size.getItemAt(size.getSelectedIndex());
            var font = new Font(fontFace, fontStyle, fontSize);
            sample.setFont(font);
            sample.repaint();
        }
    }
    
    
    package swing;
    
    import java.awt.*;
    
    /**
     * This class simplifies the use of the GridBagConstraints class.
     * 
     * @version 1.01 2004-05-06
     * @author Cay Horstmann
     */
    public class GBC extends GridBagConstraints {
        /**
         * Constructs a GBC with a given gridx and gridy position and all other grid bag
         * constraint values set to the default.
         * 
         * @param gridx the gridx position
         * @param gridy the gridy position
         */
        public GBC(int gridx, int gridy) {
            this.gridx = gridx;
            this.gridy = gridy;
        }
    
        /**
         * Constructs a GBC with given gridx, gridy, gridwidth, gridheight and all other
         * grid bag constraint values set to the default.
         * 
         * @param gridx      the gridx position
         * @param gridy      the gridy position
         * @param gridwidth  the cell span in x-direction
         * @param gridheight the cell span in y-direction
         */
        public GBC(int gridx, int gridy, int gridwidth, int gridheight) {
            this.gridx = gridx;
            this.gridy = gridy;
            this.gridwidth = gridwidth;
            this.gridheight = gridheight;
        }
    
        /**
         * Sets the anchor.
         * 
         * @param anchor the anchor value
         * @return this object for further modification
         */
        public GBC setAnchor(int anchor) {
            this.anchor = anchor;
            return this;
        }
    
        /**
         * Sets the fill direction.
         * 
         * @param fill the fill direction
         * @return this object for further modification
         */
        public GBC setFill(int fill) {
            this.fill = fill;
            return this;
        }
    
        /**
         * Sets the cell weights.
         * 
         * @param weightx the cell weight in x-direction
         * @param weighty the cell weight in y-direction
         * @return this object for further modification
         */
        public GBC setWeight(double weightx, double weighty) {
            this.weightx = weightx;
            this.weighty = weighty;
            return this;
        }
    
        /**
         * Sets the insets of this cell.
         * 
         * @param distance the spacing to use in all directions
         * @return this object for further modification
         */
        public GBC setInsets(int distance) {
            this.insets = new Insets(distance, distance, distance, distance);
            return this;
        }
    
        /**
         * Sets the insets of this cell.
         * 
         * @param top    the spacing to use on top
         * @param left   the spacing to use to the left
         * @param bottom the spacing to use on the bottom
         * @param right  the spacing to use to the right
         * @return this object for further modification
         */
        public GBC setInsets(int top, int left, int bottom, int right) {
            this.insets = new Insets(top, left, bottom, right);
            return this;
        }
    
        /**
         * Sets the internal padding
         * 
         * @param ipadx the internal padding in x-direction
         * @param ipady the internal padding in y-direction
         * @return this object for further modification
         */
        public GBC setIpad(int ipadx, int ipady) {
            this.ipadx = ipadx;
            this.ipady = ipady;
            return this;
        }
    }
    
    
    /*
     * @Author: your name
     * @Date: 2020-11-08 12:50:49
     * @LastEditTime: 2020-11-08 12:51:16
     * @LastEditors: your name
     * @Description: In User Settings Edit
     * @FilePath: /java/swing/GridBagLayoutTest.java
     */
    package swing;
    
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    /**
     * @version 1.36 2018-04-10
     * @author Cay Horstmann
     */
    public class GridBagLayoutTest {
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                var frame = new FontFrame();
                frame.setTitle("GridBagLayoutTest");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            });
        }
    }
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    数据库常用面试题
    Base64、Md5、Des加密
    C#反射类型的使用
    C# 反射工厂模式的实现
    生成验证码的例子
    轻松实现页面提交时,显示“提交中..” (转)
    编写高性能 Web 应用程序的 10 个技巧 (转)
    由Duwamish学习web.config的配置(转)
    .Net配置文件常用配置说明 (转)
    用asp.net还原与恢复sqlserver数据库(转)
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13944010.html
Copyright © 2011-2022 走看看