控制台程序。

网格布局管理器可以在容器的矩形网格中布局组件。

 import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder; public class TryGridLayout { public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size
aWindow.setSize(wndSize.width/2, wndSize.height/2); // Set window size
aWindow.setLocationRelativeTo(null); // Center window
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(3,4,30,20); // Create a layout manager
Container content = aWindow.getContentPane(); // Get the content pane
content.setLayout(grid); // Set the container layout mgr
EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED); // Button border // Now add ten Button components
JButton button = null; // Stores a button
for(int i = 1 ; i <= 10 ; ++i) {
content.add(button = new JButton(" Press " + i)); // Add a Button
button.setBorder(edge); // Set the border
}
aWindow.pack(); // Size for components
aWindow.setVisible(true); // Display the window
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}

应用程序窗口显示的最初大小由传送给JFrame对象的setBounds()方法的值来决定。如果希望窗口的大小能适合包含的组件,可以调用JFrame对象的pack()方法。在调用setVisible()方法之前,添加下面的代码:
aWindow.pack();

Java基础之创建窗口——使用网格布局管理器(TryGridLayout)的更多相关文章

  1. Java基础之创建窗口——使用卡片布局管理器(TryCardLayout)

    控制台程序. 卡片布局管理器会生成一叠组件——一个组件放在另一个组件的上面.添加到容器中的第一个组件在堆栈的顶部,因此是可见的,添加的最后一个组件在堆栈的底部.使用默认的构造函数CardLayout( ...

  2. Java基础之创建窗口——使用边界布局管理器(TryBorderLayout)

    控制台程序. 边界布局管理器最多能在容器中放置5个组件.在这种布局管理器中,可以把组件放在容器的任意一个边界上,也可以把组件放在容器的中心.每个位置只能放置一个组件.如果把组件放置在已被占用的边界上, ...

  3. python之tkinter使用-Grid(网格)布局管理器

    # 使用tkinter编写登录窗口 # Grid(网格)布局管理器会将控件放置到一个二维的表格里,主控件被分割为一系列的行和列 # stricky设置对齐方式,参数N/S/W/E分别表示上.下.左.右 ...

  4. JAVA简单的网格布局管理器--JAVA基础

    网格布局管理器: GridLayoutDemo.java: import java.awt.GridLayout;import javax.swing.JButton;import javax.swi ...

  5. JAVA 网格布局管理器

    //网格布局管理器 import java.awt.*; import javax.swing.*; public class Jiemian3 extends JFrame{ //定义组件 JBut ...

  6. java网格布局管理器

    public class GirdLayoutPosition extends JFrame{    public GirdLayoutPosition()    {        Container ...

  7. 编写Java程序,将JButton按钮按网格布局管理器格式放置

    返回本章节 返回作业目录 需求说明: 将JButton按钮按网格布局管理器格式放置 实现思路: 实现代码: public void init(){ setLayout(new GridLayout(4 ...

  8. Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)

    控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...

  9. Java基础之创建窗口——使用BoxLayout管理器(TryBoxLayout4)

    控制台程序. javax.swing.BoxLayout类定义的布局管理器在单行或单列中布局组件.创建BoxLayout对象时,需要指定是在行还是列中布局组件. 对于行,组件是从左到右地添加:对于列, ...

随机推荐

  1. DNS:www.flickr.com

    203.84.197.9 203.84.197.25 203.84.197.26 203.84.197.27

  2. mysql IN 比等价的OR写法效率更高

  3. Yii源码阅读笔记(七)

    接上次的组件(component)代码: /** * Returns a list of behaviors that this component should behave as. * 定义该对象 ...

  4. 蓝牙Bluetooth技术手册规范下载

    [背景] 之前就已经整理和转帖了和蓝牙技术相关的一些内容: [资源下载]bluetooth 协议 spec specification 蓝牙1.1.蓝牙1.2.蓝牙2.0(蓝牙2.0+EDR)区别 但 ...

  5. 非Controller类无法使用Service bean解决方案

      尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 ...

  6. php--在apache上配制rewrite重写

    配置步骤: 第一步:找到apache的配置文件httpd.conf(文件在conf目录下) 第二步:你首先必须得让服务器支持mod_rewrite,如果你使用的是虚拟主机,请事先询问你的主机提供商. ...

  7. 去除Html标签

    public static string ParseTags(string Htmlstring)     {         //删除脚本          Htmlstring = Regex.R ...

  8. 模糊搜索UISearchBar

    #import "Search_ViewController.h" @interface Search_ViewController ()<UITableViewDataSo ...

  9. 5分钟弄懂Docker!

    http://www.csdn.net/article/2014-07-02/2820497-what%27s-docker 关注点:1.DOCKER和VM的架构区别 2.Docker 的容器利用了  ...

  10. eclipse调试jdk源码

    摘要 介绍使用eclipse调试jdk源码 java是一门开源的程序设计语言,喜欢研究源码的java开发者总会忍不住debug一下jdk源码.虽然官方的jdk自带了源码包src.zip,然而在debu ...