环境
jdk17(jdk14 以后自带将jar 打成安装包工具,版本从1.8调整到17)
Maven:3.2.5
pom.xml:在上篇文章中《Java Swing-2.环境搭建及窗口背景》
效果
下一章:如何通过jdk 自带的工具 打包成 exe ,msi ,或者面安装版的exe 文件(自带jre环境),无需安装这安装jdk
- exe4j: 免费版会有弹框 提示用户使用的免费版exe4j打包工具
- Launch4j: 开源,但目前没找到如何把jre 环境直接放到exe 中,需要把exe和jre 打成一个压缩包形式使用
说明
Swing 控制样式总体来说分为两种
- 通过布局管理器
- 组件提供的修改样式方法(部分组件,有限调整)
- 继承组件后通过画布 重写绘制该组件(按钮变圆,背景色等)
1.布局管理器
布局管理器之前要先了解Swing AWT 的容器(窗口)概念,因为布局管理器是针对容器的,每个容器可以设置一个属于自己的布局管理器。基本概念在这;同时容器可以添加其他容器,组件。
一个容器的整体分布、布局方式。
FlowLayout
流式布局,依次排序,遇到边界挪至下一行
BorderLayout
边界布局(区域布局),分为北、南、西、东、中(上、下、左、右、中) 五个区域
GridLayout
纵横风格的网络,每个网格所占区域大小相同。默认从左向右
GridBagLayout
功能最强大的布局管理器,纵横网格,可以跨越一个或多个网格,设置网格的大小(类似Excel中的合并单元格 ,上下 左右合并)比较难控制
CardLayout
将加入容器的租价看成一叠卡片,只有最上层的才可见(类似轮播图)
绝对定位
setLayout(null)
设置后不会再自动填充这个容器,
BoxLayout
GridBagLayout布局管理器的简化版,配合 Box 一起使用,如果想要简单控制组件间距,可以使用该组件
组件方法
不是所有组件都有以下方法。
.setBounds();
// x起点,y起点 宽,高
button.setBounds(5, 5, 80, 25);
.setFont();
// 设置字体 形状,大小
zlabel.setFont(new Font("Serif", Font.BOLD, 36));
.setSize();
// 宽,高
newLabel.setSize(10,20);
.setIcon();
// 设置图标(按钮图标,树状图图标)
newLabel.setIcon(new ImageIcon("/path/xxx/ccc"));
// 设置背景色(边框)
jPanel.setBackground(Color.black);
继承重绘组件
主要依赖于以下两种画布
- Graphics
继承组件 重新 paintComponent(Graphics g) 方法 - Canvase
主要代码
项目目录
控制样式部分都在 首页及编辑页面,主要使用了布局管理器,组件方法来控制按钮的位置
首页代码
package com.tiger.four;
import com.tiger.four.table.GoodsJTable;
import com.tiger.four.table.LableJTable;
import com.tiger.four.util.DataUtil;
import com.tiger.four.util.FileTUtil;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.util.Random;
import java.util.Set;
public class InitWindow {
public static JComboBox<String> comboBox;
private static Timer timer;
/**
* 创建窗口
*/
public static void createWindow() {
JFrame jFrame = new JFrame("随便");
//jFrame.setIconImage(new ImageIcon("src/main/resources/static/imgs/yu.png").getImage());
jFrame.setIconImage(FileTUtil.readImage("static/imgs/yu.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口宽高
jFrame.setSize(600, 500);
// 添加菜单
jFrame.setJMenuBar(createMenus(jFrame));
// 添加开始按钮
JPanel jPanel = new JPanel();
jPanel.setBackground(Color.black);
// 添加标签
jFrame.add(createLable(jFrame,jPanel), BorderLayout.NORTH);
startFace(jFrame, jPanel);
jFrame.add(jPanel, BorderLayout.CENTER);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);
}
/**
* 开始界面
* @param jFrame
* @param jPanel
*/
private static void startFace(JFrame jFrame, JPanel jPanel) {
// 清空当前容器所有组件
jPanel.removeAll();
if(timer!=null&&timer.isRunning()){
timer.stop();
}
// 加载开始图片
JLabel imageLabel = new JLabel();
ImageIcon secondImage = FileTUtil.readImage("static/imgs/startb1.png");//new ImageIcon("src/main/resources/static/imgs/startb1.png");
imageLabel.setIcon(secondImage);
jPanel.add(imageLabel);
// 增加图片点击时间
imageLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// 跳转抽签页面
drawLotsFace(jFrame, jPanel);
}
});
jFrame.revalidate();
jFrame.repaint();
}
/**
* 抽签界面
*/
private static void drawLotsFace(JFrame jFrame, JPanel jPanel) {
// 清空当前容器所有组件
jPanel.removeAll();
// 加载抽签界面
JLabel imageLabel = new JLabel();
ImageIcon secondImage = FileTUtil.readImage("static/imgs/move/chou2.gif"); //new ImageIcon("src/main/resources/static/imgs/move/chou2.gif");
imageLabel.setIcon(secondImage);
jPanel.add(imageLabel);
// 重新渲染窗口
jFrame.revalidate();
jFrame.repaint();
// 创建一个Timer,在2秒后触发ActionEvent
timer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// 跳转结束画面
endFace(jFrame, jPanel, (Timer) evt.getSource());
}
});
timer.start();
}
/**
* 结束界面
* @param jFrame
* @param jPanel
* @param timer1
*/
private static void endFace(JFrame jFrame, JPanel jPanel, Timer timer1) {
// 清空容器中所有组件
jPanel.removeAll();
// 绘制结束画面
// 首级容器 让文字和按钮换行展示
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new BoxLayout(firstPanel, BoxLayout.Y_AXIS)); // 设置垂直布局
// 获取当前选择的下来框
String selectedItem = comboBox.getSelectedItem() + "";
Set<String> strings = DataUtil.getGoods().get(selectedItem);
// 生成随机数,随机生成结果
Object[] array = strings.toArray();
int i = new Random().nextInt(array.length);
String good = array[i] + "";
// 二级容器(为了让文字 和 按钮都居中展示,不加会错位)
JPanel secondPanel = new</