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);
});
}
}
java 网格组布局的更多相关文章
- JAVA中GridBagLayout布局管理器应用详解
很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...
- GridBagLayout:网格包布局管理器
GridBagLayout:网格包布局管理器 GridBagLayout可以说是布局管理器Layout中最复杂的一个,其中涉及到的参数也比较得多,比如说: GridBagConstraints g ...
- Java界面编程—布局管理
布局是指容器中组件的排列方式 常用的布局管理器 布局管理器名称 所属类包 说明 FlowLayout(流式布局) java.awt 组件按照加入的先后顺序.按照设置的对齐方式从左向右排列,一行排满后到 ...
- Java基础——GridBagLayout布局
1.GridBagLayout布局管理器非常灵活,每个 GridBagLayout 对象维持一个动态的矩形单元网格: 2.需要和它的约束类(GridBagConstraints类)一起使用: 3.Gr ...
- supermap布局设定地图网格及布局网格
地图网格设定 LayoutElements elements = m_mapLayoutControl.MapLayout.Elements; //构造GeoMap GeoMap geoMap = n ...
- Masonry – 实现 Pinterest 模式的网格砌体布局
Masonry 是一款 JavaScript 网格布局插件,可以实现类似 Pinterest 模式的砌体布局效果.通过把元素自动填充在垂直的空白区域,就像墙上堆砌的石头一样.这个库还可以作为 jQue ...
- JAVA流式布局管理器--JAVA基础
JAVA流式布局管理器的使用: FlowLayoutDeme.java: import java.awt.*;import javax.swing.*;public class FlowLayoutD ...
- Java线程组(ThreadGroup)使用
JDK 对线程组类注释: A thread group represents a set of threads. In addition, a thread group can also includ ...
- 【前端】Vue.js实现网格列表布局转换
网格列表布局转换 实现效果: 实现代码及注释: <!DOCTYPE html> <html> <head> <title>布局转换</title& ...
- 2019 第十届蓝桥杯大赛软件类省赛 Java A组 题解
2019 第十届蓝桥杯大赛软件类省赛 Java A组 试题A 题解 题目最后一句贴心的提示选手应该使用 long (C/C++ 应该使用 long long). 本题思路很直白,两重循环.外层 ...
随机推荐
- 关于C#的Dynamic调用方法前的一些准备的小Demo
using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Linq; us ...
- 异步日志分析:MongoDB与FastAPI的高效存储揭秘
title: 异步日志分析:MongoDB与FastAPI的高效存储揭秘 date: 2025/05/22 17:04:56 updated: 2025/05/22 17:04:56 author: ...
- HarmonyOS NEXT从图库选择资源上传到服务器或者把网络资源下载到图库
用户需要分享文件.保存图片.视频等用户文件时,开发者可以通过系统预置的文件选择器(FilePicker),实现该能力.通过Picker访问相关文件,将拉起对应的应用,引导用户完成界面操作,接口本身无需 ...
- 如何在Mac系统上把U盘分成2个区?
插入U盘后,使用命令行执行如下命令: diskutil partitionDisk /dev/disk8 GPT MS-DOS TESLAMUSIC 7% ExFAT TESLADRIVE 93% 第 ...
- 如何给Git设置ssh代理
由于不可描述的原因,在某些情况下Github不可用(git push和git pull都显示远程服务器不可用或无权限),于是就想到上代理. 首先网上搜索一通,发现如下方法: git config -- ...
- manim边做边学--隐函数图像
在数学可视化中,显函数$ y=f(x) \(相对容易处理,但**隐函数**\) F(x,y)=0 $的绘制则更具挑战性. Manim库中的ImplicitFunction类专门用于解决这个问题,它能够 ...
- 通义灵码2.5+qwen3——节假日抢票不用愁,基于12306-MCP实现个人火车票智能查询小助手!
在日常生活中,12306 是中国铁路售票系统的官方平台.为了提升购票效率.自动化查询余票信息以及获取车站代码等功能,我们希望通过使用智能体编程方式,结合 MCP(Model-as-a-Servic ...
- Docker安装运行报错wsl问题排查方案
Docker运行报错wsl问题排查方案 在window中安装运行Docker Desktop时容易遇到有关wsl的报错问题,这里给出几个排查解决的方法用于记录. 排查步骤如下: 一.开启虚拟化问题 1 ...
- 使用hive数据查询小结
业务背景: 公司大数据查询需要通过hive查询和分析一些数据 产品提出业务分析需求: 我的处理方式: 接到需求就想怎么写SQL语句,然后不断调整SQL语句进行验证,最后这个需求写了170行的SQL语句 ...
- 从困境到突破,EasyMR 集群迁移助力大数据底座信创国产化
在大数据时代,企业对数据的依赖程度越来越高.然而,随着业务的不断发展和技术的快速迭代,大数据平台的集群迁移已成为企业数据中台发展途中无法回避的需求.在大数据平台发展初期,国内数据中台市场主要以国外开源 ...