Java中创建自己的界面,主要依赖于Swing和AWT(Abstract Window Toolkit)这两种API,以下是详细的步骤和示例,帮助你理解如何创建和管理Java图形用户界面(GUI)。
基本概念
AWT(Abstract Window Toolkit):是Java的基础GUI工具包,提供了创建图形用户界面所需的基本功能,AWT组件是平台依赖的,因此在不同操作系统上可能会有不同的外观和行为。
Swing:是对AWT的扩展,提供了更加复杂的界面组件,并且支持更加丰富的界面效果,Swing组件大多位于javax.swing
包中,并且是轻量级的,不依赖于平台特定的GUI库。
创建基本的窗口
在Java中,通常使用JFrame
来创建一个窗口。JFrame
是一个顶级容器,可以包含其他Swing组件。
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class MyWindow { public static void main(String[] args) { // 使用SwingUtilities.invokeLater确保线程安全 SwingUtilities.invokeLater(() -> { // 创建JFrame实例 JFrame frame = new JFrame("My Window"); // 设置默认关闭操作 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口大小 frame.setSize(400, 300); // 添加一个简单的标签 frame.add(new JLabel("Hello, World!", JLabel.CENTER)); // 设置窗口可见 frame.setVisible(true); }); } }
添加组件
在窗口中,你可以添加各种Swing组件,如按钮、文本框、标签等,这些组件需要先被创建,然后添加到容器中。
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class MyForm { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("My Form"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLayout(new java.awt.FlowLayout()); // 设置流式布局 // 添加标签和文本框 JLabel nameLabel = new JLabel("Name:"); JTextField nameField = new JTextField(20); frame.add(nameLabel); frame.add(nameField); // 添加按钮 JButton submitButton = new JButton("Submit"); frame.add(submitButton); frame.setVisible(true); }); } }
事件处理
图形界面程序通常需要对用户的操作进行响应,这涉及到事件驱动编程,Java中的事件处理是通过实现事件监听器接口完成的,例如ActionListener
、MouseListener
、KeyListener
等。
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyFormWithEvent { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("My Form"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLayout(new java.awt.FlowLayout()); JLabel nameLabel = new JLabel("Name:"); JTextField nameField = new JTextField(20); JButton submitButton = new JButton("Submit"); JLabel responseLabel = new JLabel(""); frame.add(nameLabel); frame.add(nameField); frame.add(submitButton); frame.add(responseLabel); // 添加事件监听器 submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = nameField.getText(); responseLabel.setText("Hello, " + name + "!"); } }); frame.setVisible(true); }); } }
布局管理器
布局管理器用于管理组件在容器中的位置和大小,常见的布局管理器有FlowLayout
、BorderLayout
、GridLayout
等。
布局管理器 | 特点 |
---|---|
FlowLayout |
组件按顺序排列,从左到右,从上到下 |
BorderLayout |
将容器分为五个区域:北、南、东、西、中 |
GridLayout |
将容器分为网格,组件按网格排列 |
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import java.awt.BorderLayout; import java.awt.GridLayout; public class MyLayout { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("My Layout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); // 使用BorderLayout frame.setLayout(new BorderLayout()); // 添加按钮到北部 JButton northButton = new JButton("North"); frame.add(northButton, BorderLayout.NORTH); // 添加按钮到南部 JButton southButton = new JButton("South"); frame.add(southButton, BorderLayout.SOUTH); // 添加按钮到东部 JButton eastButton = new JButton("East"); frame.add(eastButton, BorderLayout.EAST); // 添加按钮到西部 JButton westButton = new JButton("West"); frame.add(westButton, BorderLayout.WEST); // 添加按钮到中部 JButton centerButton = new JButton("Center"); frame.add(centerButton, BorderLayout.CENTER); frame.setVisible(true); }); } }
自定义组件
你可以通过继承Swing组件并重写它们的方法来实现自定义样式和功能,创建一个自定义的面板组件:
import javax.swing.JPanel; import javax.swing.JLabel; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class ClockPanel extends JPanel { private JLabel timeLabel; public ClockPanel() { super(); timeLabel = new JLabel(); add(timeLabel); // 定时更新时间 java.util.Timer timer = new java.util.Timer(); timer.scheduleAtFixedRate(new java.util.TimerTask() { @Override public void run() { LocalTime now = LocalTime.now(); String formattedTime = now.format(DateTimeFormatter.ofPattern("HH:mm:ss")); timeLabel.setText("Current Time: " + formattedTime); } }, 0, 1000); // 每秒更新一次 } }
综合示例我们可以创建一个简单的登录界面:
import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginUI { private JTextField tf_user; // 定义用户名文本框 private JPasswordField pf_pass; // 定义密码框 private JButton bt_login; // 定义登录按钮 private JButton bt_close; // 定义关闭按钮 private JLabel lb_user; // 定义用户名标签 private JLabel lb_pass; // 定义密码标签 private JLabel lb_response; // 定义响应标签 / 构造方法:用于初始化“登录界面” / public LoginUI() { // 第1步:获取框架内容面板,并设置内容面板的布局为绝对布局 this.getContentPane().setLayout(null); // 第2步:在框架的内容面板上,创建、设置各个组件 lb_user = new JLabel("用户名:"); // 创建用户名标签 // 设置用户名标签的放置位置和放置大小 lb_user.setBounds(new Rectangle(100, 50, 70, 25)); add(lb_user); // 将用户名标签添加到内容面板上 tf_user = new JTextField(); // 创建用户名文本框 tf_user.setBounds(new Rectangle(170, 50, 110, 25)); add(tf_user); // 将用户名文本框添加到内容面板上 lb_pass = new JLabel("密码:"); // 创建密码标签 lb_pass.setBounds(new Rectangle(100, 90, 50, 25)); add(lb_pass); // 将密码标签添加到内容面板上 pf_pass = new JPasswordField(); // 创建密码框 pf_pass.setBounds(new Rectangle(170, 90, 110, 25)); add(pf_pass); // 将密码框添加到内容面板上 bt_login = new JButton("登录"); // 创建登录按钮 bt_login.setBounds(new Rectangle(100, 160, 80, 25)); // 第3步:为按钮添加单击事件监听器 // 为“登录”按钮添加单击事件监听器 bt_login.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 调用“登录”按钮事件响应方法 bt_login_actionPerformed(); } }); add(bt_login); // 将登录按钮添加到内容面板上 bt_close = new JButton("关闭"); // 创建关闭按钮 bt_close.setBounds(new Rectangle(200, 160, 80, 25)); // 为“关闭”按钮添加单击事件监听器 bt_close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 释放“登录界面”占用的屏幕资源,即关闭“登录界面” dispose(); } }); add(bt_close); // 将关闭按钮添加到内容面板上 lb_response = new JLabel(""); // 创建响应标签,用于显示登录结果或提示信息 lb_response.setBounds(new Rectangle(100, 200, 200, 25)); // 设置响应标签的放置位置和大小 add(lb_response); // 将响应标签添加到内容面板上 // 第4步:设置“登录界面的标题、大小、位置等属性” setTitle("大帅哥-登录界面"); // 设置“登录界面”标题 setSize(380, 260); //...
原创文章,发布者:酷盾叔,转转请注明出处:https://www.kd.cn/ask/56652.html