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). 本题思路很直白,两重循环.外层 ...
随机推荐
- MySql随笔记基础
XAMPP使用 shell 命令 每个数据库对应 一个子文件夹 mysql 进入mySQL的命令 -uroot userroot 登录用户 -uroot -p password 登录密码 -p123 ...
- 【深度思考】自定义日期格式,为什么@JSONField生效,@JsonFormat不生效?
1. 前言 最近在自测接口时,发现一个问题:字段类型定义的是Date,但接口返回值里却是时间戳(1744959978674), 而不是预期的2025-04-18 15:06:18. private D ...
- 解决get请求特殊字符问题
@Bean public ServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory fa = new Tom ...
- 代码随想录第四天 | Leecode 24. 两两交换链表、19.删除链表的倒数第N个节点、 面试题 02.07. 链表相交、 142.环形链表II
Leecode 24 两两交换链表 题目描述 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点.你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换). 示例 1: 输入:h ...
- 【公众号搬运】React-Native开发鸿蒙NEXT(2)
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- qt获得当前窗口所在屏幕的大小
假如这个窗口的指针为this 记得要加头文件哦 #include <QDesktopWidget> #include <QApplication> //获得当前屏幕是第几屏幕 ...
- 测试Python编译器性能
import time t=time.time() num=input("输入一个质数:") def prime_number(num): i=2 for i in range(i ...
- IntelliJ IDEA FIX协议报文解析插件
Fix协议报文手动对照对手方的API查看十分繁琐,尤其是在开发的过程中. 于是我写了一个Fix协议报文解析插件,在idea插件应用市场搜索"Fix Protocol Parser" ...
- cpp的lambda表达式
在C++中,lambda表达式提供了一种方便的方式来定义匿名函数.Lambda可以用来创建简单的函数对象,常用于算法库中的函数参数,特别是在STL(Standard Template Library) ...
- C# 将 CSV 转化为 Excel
引言 在数据处理和交换的过程中,CSV(逗号分隔值)格式和 Excel 文件格式都是非常常见的.CSV 文件以纯文本形式存储表格数据,易于生成和处理:而 Excel 文件则提供了更丰富的功能,如数据可 ...