- Swing 编程示例
- 示例 - 主页
- 示例 - 环境设置
- 示例 - 边框
- 示例 - 按钮
- 示例 - 复选框
- 示例 - 组合框
- 示例 - 颜色选择器
- 示例 - 对话框
- 示例 - 编辑器窗格
- 示例 - 文件选择器
- 示例 - 格式化文本字段
- 示例 - 框架
- 示例 - 列表
- 示例 - 布局
- 示例 - 菜单
- 示例 - 密码字段
- 示例 - 进度条
- 示例 - 滚动窗格
- 示例 - 滑块
- 示例 - 旋转器
- 示例 - 表格
- 示例 - 工具栏
- 示例 - 树
- Swing 实用资源
- Swing - 快速指南
- Swing - 实用资源
- Swing - 讨论
Swing 示例 - 使用边框
以下示例展示如何在 Java Swing 应用程序中使用边框。
我们正在使用以下 API。
BorderFactory.createLineBorder() − 创建线边框。
BorderFactory.createEtchedBorder() − 创建蚀刻边框。
BorderFactory.createEmptyBorder() − 创建空边框。
JPanel.setBorder(border) − 向 JPanel 设置所需的边框。
示例
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.LayoutManager; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; public class SwingTester { public static void main(String[] args) { createWindow(); } private static void createWindow() { JFrame frame = new JFrame("Swing Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); createUI(frame); frame.setSize(560, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } private static void createUI(JFrame frame){ Border blackline = BorderFactory.createLineBorder(Color.black); Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); Border raisedbevel = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); Border loweredbevel = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); Border empty = BorderFactory.createEmptyBorder(); JPanel panel = new JPanel(); LayoutManager layout = new FlowLayout(); panel.setLayout(layout); JPanel panel1 = new JPanel(); panel1.add(new JLabel("Black border")); panel1.setBorder(blackline); JPanel panel2 = new JPanel(); panel2.add(new JLabel("Raised border")); panel2.setBorder(raisedetched); JPanel panel3 = new JPanel(); panel3.add(new JLabel("Lowered border")); panel3.setBorder(loweredetched); JPanel panel4 = new JPanel(); panel4.add(new JLabel("Raised Bevel border")); panel4.setBorder(raisedbevel); JPanel panel5 = new JPanel(); panel5.add(new JLabel("Lowered Bevel border")); panel5.setBorder(loweredbevel); JPanel panel6 = new JPanel(); panel6.add(new JLabel("Empty border")); panel6.setBorder(empty); panel.add(panel1); panel.add(panel2); panel.add(panel3); panel.add(panel4); panel.add(panel5); panel.add(panel6); frame.getContentPane().add(panel, BorderLayout.CENTER); } }
swingexamples_borders.htm
广告