zoukankan      html  css  js  c++  java
  • javax.Swing 使用GridBagLayout的程序栗子

    摘自https://zhidao.baidu.com/question/110748776.html 

    javax.Swing 使用GridBagLayout的程序栗子

    总共两个文件,第一个是启动文件,第二个是一个基础面板类
     1 package com;
     2 
     3 import com.ren.BasePanel;
     4 import javax.swing.*;
     5 import java.awt.BorderLayout;
     6 import java.awt.Toolkit;
     7 
     8 public class SwingTest extends JFrame {
     9     public static void main(String args[]) {
    10         new SwingTest();
    11     }
    12 
    13     public SwingTest() {
    14         super("SwingTextField 测试");
    15         init();
    16         setup();
    17         this.setSize(800, 300);
    18         this.setVisible(true);
    19         this.setLocation
    20                 ( //定位框架位置
    21                         (int) (Toolkit.getDefaultToolkit().getScreenSize().
    22                                getWidth() - 400) / 2,
    23                         (int) (Toolkit.getDefaultToolkit().getScreenSize().
    24                                getHeight() - 300) / 2
    25                 ); 
    26         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    27     }
    28 
    29     BasePanel leftPanel;
    30     BasePanel rightPanel;
    31     JTextField country = new JTextField(16);
    32     JButton search = new JButton("SEARCH");
    33     JTextField latitude = new JTextField(40);
    34     JTextField currentTime = new JTextField(40);
    35     JTextField wind = new JTextField(40);
    36     JTextField visibilityField = new JTextField(40);
    37     JTextField skycondition = new JTextField(40);
    38     JTextField dewpoint = new JTextField(40);
    39     JTextField relativehumidity = new JTextField(40);
    40     JTextField presure = new JTextField(40);
    41 
    42     public void init() {
    43         leftPanel = new BasePanel() {
    44             public void initAllComponents() {
    45             }
    46 
    47             public void layoutAllComponents() {
    48                 addComponent(country, 0, 0, 1, 1, 10, 10);
    49                 addComponent(search, 1, 0, 1, 1, 10, 10);
    50             }
    51         };
    52 
    53         rightPanel = new BasePanel() {
    54             public void initAllComponents() {
    55             }
    56 
    57             public void layoutAllComponents() {
    58                 addComponent(latitude, 0, 0, 1, 1, 10, 10);
    59                 addComponent(currentTime, 1, 0, 1, 1, 10, 10);
    60                 addComponent(wind, 2, 0, 1, 1, 10, 10);
    61                 addComponent(visibilityField, 3, 0, 1, 1, 10, 10);
    62                 addComponent(skycondition, 4, 0, 1, 1, 10, 10);
    63                 addComponent(dewpoint, 5, 0, 1, 1, 10, 10);
    64                 addComponent(relativehumidity, 6, 0, 1, 1, 10, 10);
    65                 addComponent(presure, 7, 0, 1, 1, 10, 10);
    66             }
    67         };
    68     }
    69 
    70     public void setup() {
    71         this.setLayout(new BorderLayout());
    72         this.add(leftPanel, BorderLayout.WEST);
    73         this.add(rightPanel, BorderLayout.EAST);
    74     }
    75 }
    以下是我以前的经验总结,比较有用的一个类,你可以好好研究一下
      1 /**<p>
      2  * =============================================================================
      3  * <p>Copyright (c) 2008, Ren Java Studio
      4  * <p>All rights reserved.<p>
      5  * =============================================================================
      6  * <p>文件名称:BasePanel.java
      7  * <p>文件标识:见配置管理计划书
      8  * <p>摘    要:抽象基础面板
      9  * @version 2.0
     10  * @author Ren
     11  * <p>完成日期:2008年5月5日.<p>
     12  =============================================================================*/
     13 package com.ren;
     14 
     15 import java.awt.*;
     16 import javax.swing.*;
     17 
     18 public abstract class BasePanel extends JPanel {
     19     /*当前面板的容器*/
     20     protected Container container;
     21     /*网格布局器*/
     22     protected GridBagLayout layout;
     23     /*网格布局器的约束器*/
     24     protected GridBagConstraints constraints;
     25 
     26     public BasePanel() {
     27         container = this;
     28         layout = new GridBagLayout();
     29         constraints = new GridBagConstraints();
     30         container.setLayout(layout);
     31         constraints.fill = constraints.BOTH; //在水平方向和垂直方向上同时调整组件大小
     32         initAllComponents();
     33         layoutAllComponents();
     34     }
     35 
     36     /**<p>
     37      * =========================================================================
     38      * <p>初始化所有GUI组件.<p>
     39      =========================================================================*/
     40     protected abstract void initAllComponents();
     41 
     42     /**@todo 继承之子类完成所有组件的初始化工作 */
     43 
     44     /**<p>
     45      * =========================================================================
     46      * <p>布局所有已经初始化了的GUI组件
     47      *<p><blockquote><pre>
     48      *   布局器,约束器必须已经初始化好
     49      *   然后再调用布局子函数进行网格布局
     50      *</pre></blockquote></p>
     51      =========================================================================*/
     52     protected abstract void layoutAllComponents();
     53 
     54     /**@todo 继承之子类完成组件具体的布局 */
     55 
     56     /**<p>
     57      * =========================================================================
     58      * <p> 工具函数:用于获取当前屏幕的分辨率
     59      * @return Dimension 屏幕大小<p>
     60      =========================================================================*/
     61     public Dimension getScreenSize() {
     62         return Toolkit.getDefaultToolkit().getScreenSize();
     63     }
     64 
     65     /**<p>
     66      * ==========================================================================
     67      * <p> 设置将本程序框架大小并将其定位于屏幕正中心
     68      * @param container Container 要设置的容器
     69      * @param width 本框架的宽度
     70      * @param height 本框架的高度<p>
     71      ==========================================================================*/
     72     protected void putToCenter(Component container, int width, int height) {
     73         container.setSize(width, height); //设置大小
     74         container.setLocation
     75                 ( //定位框架位置
     76                         (int) (Toolkit.getDefaultToolkit().getScreenSize().
     77                                getWidth() - width) / 2,
     78                         (int) (Toolkit.getDefaultToolkit().getScreenSize().
     79                                getHeight() - height) / 2
     80                 );
     81     }
     82 
     83     /**<p>
     84      * =========================================================================
     85      * <P> 获取整个桌面的大小
     86      * @return Rectangle 桌面大小.<p>
     87      ==========================================================================*/
     88     public java.awt.Rectangle getDesktopBounds() {
     89         GraphicsEnvironment env =
     90                 GraphicsEnvironment.getLocalGraphicsEnvironment();
     91         return env.getMaximumWindowBounds();
     92     }
     93 
     94     /**<p>
     95      * =========================================================================
     96      * <p> 设置将本程序框架大小并将其定位于屏幕(X,Y)坐标处宽和高为 width, height
     97      * @param container Container 要设置的容器
     98      * @param width int  宽度
     99      * @param height int 高度
    100      * @param startX int 放置位置的X坐标
    101      * @param startY int 放置位置的Y坐标<p>
    102      =========================================================================*/
    103     protected void putToPlace(Container container, int width, int height,
    104                               int startX, int startY
    105             ) {
    106         container.setLocation(startX, startY);
    107         container.setSize(width, height); //设置大小
    108     }
    109 
    110     /**<p>
    111      * =========================================================================
    112      * <p> 功能强大的工具函数:布局器
    113      * <p> 具体功能:
    114      * <p> 将 componentToAdd 组件安装到 container 容器中的
    115      * <p> 第 row 行, 第 column 列,并在 container 容器中
    116      * <p> 占据 height 行,width 列, 在X,Y方向分别可伸展x和y长
    117      * <p> 备注:要能如此挑剔的地布局,container 必须预先设定为网格布局器:GridBagLayout,
    118      * <p>       然后用 GridBagContstraints 协调布局
    119      * <p>原函数来自"Java程序设计教程"第五版 P530页,函数有被我加强功能
    120      * @param container   Container 要布局的容器
    121      * @param layout      GridBagLayout 网格布局器
    122      * @param constraints GridBagConstraints 约束器
    123      * @param componentToAdd Component 要添加的目标组件
    124      * @param row         目标组件要添加的行
    125      * @param column      目标组件要添加的列
    126      * @param height      目标组件占据的高度
    127      * @param width       目标组件占据的宽度
    128      * @param x           目标组件在水平方向的拉伸能力
    129      * @param y           目标组件在垂直方向的拉伸能力<p>
    130      =========================================================================*/
    131     protected void addComponent(
    132             Container container, GridBagLayout layout,
    133             GridBagConstraints constraints,
    134             Component componentToAdd, int row, int column, int height,
    135             int width, int x, int y) {
    136         constraints.gridx = column; //组件所在列
    137         constraints.gridy = row; //组件所在行
    138 
    139         constraints.gridwidth = width; //组件宽度
    140         constraints.gridheight = height; //组件高度
    141 
    142         constraints.weightx = x; //组件在水平方向的拉伸能力
    143         constraints.weighty = y; //组件在垂直方向的拉伸能力
    144 
    145         layout.setConstraints(componentToAdd, constraints); //设置目标组件的约束
    146         container.add(componentToAdd); //在容器中添加目标组件
    147     }
    148 
    149     /**<p>
    150      * =========================================================================
    151      * <p> 功能强大的工具函数:布局器
    152      * <p> 将指定组件安装到此 Panel 内
    153      * @param componentToAdd Component 要添加的目标组件
    154      * @param row         目标组件要添加的行
    155      * @param column      目标组件要添加的列
    156      * @param height      目标组件占据的高度
    157      * @param width       目标组件占据的宽度
    158      * @param x           目标组件在水平方向的拉伸能力
    159      * @param y           目标组件在垂直方向的拉伸能力<p>
    160      =========================================================================*/
    161     protected void addComponent(Component componentToAdd,
    162                                 int row, int column, int height,
    163                                 int width, int xAblility, int yAblity) {
    164         addComponent(container, layout, constraints, componentToAdd,
    165                      row, column, height, width, xAblility, yAblity);
    166     }
    167 }
  • 相关阅读:
    ADO.NET 实体框架 资料收集
    sql server 根据执行计划查询耗时操作
    查看sql server数据库文件信息
    TypeScript
    使用mustache.js 模板引擎输出html
    C#委托多播、Lambda表达、多线程、任务
    PID控制器开发笔记之十三:单神经元PID控制器的实现
    C语言学习及应用笔记之五:C语言typedef关键字及其使用
    君子爱财
    PID控制器开发笔记之十二:模糊PID控制器的实现
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/6156223.html
Copyright © 2011-2022 走看看