Swing 是 Java 的一个图形用户界面(GUI)框架,用于创建基于窗口的应用程序。
它是 Java 基础类库的一部分,提供了一组丰富的组件和容器,如按钮、标签、文本框、面板等,可以帮助开发人员轻松构建出美观且功能强大的桌面应用程序。
![java中swing的用法详解(java中swing教程) 图片[1]-java中swing的用法详解(java中swing教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/05/bf0f14f1df81c20048f48e80c7a57d98.jpg)
以下是 Swing 的用法详解:
1、导入 Swing 包:
要使用 Swing,首先需要在 Java 程序中导入相关的 Swing 包。通常,我们导入以下两个包:
import javax.swing.*;import java.awt.*;import javax.swing.*; import java.awt.*;import javax.swing.*; import java.awt.*;
2、创建一个 JFrame:
JFrame 是 Swing 中的一个基本窗口,用于容纳其他组件。要创建一个 JFrame,可以继承 JFrame 类并调用相应的方法设置窗口属性:
public class MySwingApp extends JFrame {public MySwingApp() {setTitle("My Swing App"); // 设置窗口标题setSize(400, 300); // 设置窗口尺寸setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭时的操作(退出程序)}}public class MySwingApp extends JFrame { public MySwingApp() { setTitle("My Swing App"); // 设置窗口标题 setSize(400, 300); // 设置窗口尺寸 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭时的操作(退出程序) } }public class MySwingApp extends JFrame { public MySwingApp() { setTitle("My Swing App"); // 设置窗口标题 setSize(400, 300); // 设置窗口尺寸 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭时的操作(退出程序) } }
3、添加组件:
Swing 提供了许多可用的组件,如 JButton、JLabel、JTextField 等。要将组件添加到 JFrame 上,首先需要创建一个容器(如 JPanel),然后将组件添加到容器中,最后将容器添加到 JFrame 中:
public class MySwingApp extends JFrame {public MySwingApp() {// ...(省略上面的代码)JPanel panel = new JPanel(); // 创建一个 JPanelJButton button = new JButton("Click me!"); // 创建一个 JButtonpanel.add(button); // 将 JButton 添加到 JPanel 中getContentPane().add(panel); // 将 JPanel 添加到 JFrame 中}}public class MySwingApp extends JFrame { public MySwingApp() { // ...(省略上面的代码) JPanel panel = new JPanel(); // 创建一个 JPanel JButton button = new JButton("Click me!"); // 创建一个 JButton panel.add(button); // 将 JButton 添加到 JPanel 中 getContentPane().add(panel); // 将 JPanel 添加到 JFrame 中 } }public class MySwingApp extends JFrame { public MySwingApp() { // ...(省略上面的代码) JPanel panel = new JPanel(); // 创建一个 JPanel JButton button = new JButton("Click me!"); // 创建一个 JButton panel.add(button); // 将 JButton 添加到 JPanel 中 getContentPane().add(panel); // 将 JPanel 添加到 JFrame 中 } }
4、添加事件监听器:
为了使组件具有交互性,可以为其添加事件监听器。以下是一个为按钮添加 ActionListener 的示例:
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class MySwingApp extends JFrame {public MySwingApp() {// ...(省略上面的代码)JButton button = new JButton("Click me!");button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("Button clicked!");}});panel.add(button);}}import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MySwingApp extends JFrame { public MySwingApp() { // ...(省略上面的代码) JButton button = new JButton("Click me!"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } }); panel.add(button); } }import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MySwingApp extends JFrame { public MySwingApp() { // ...(省略上面的代码) JButton button = new JButton("Click me!"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } }); panel.add(button); } }
5、显示窗口:
在创建完 JFrame 之后,需要调用 setVisible(true)
方法使其显示在屏幕上:
public static void main(String[] args) {MySwingApp app = new MySwingApp();app.setVisible(true);}public static void main(String[] args) { MySwingApp app = new MySwingApp(); app.setVisible(true); }public static void main(String[] args) { MySwingApp app = new MySwingApp(); app.setVisible(true); }
将上述代码整合到一起,我们得到一个简单的 Swing 程序:
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class MySwingApp extends JFrame {public MySwingApp() {setTitle("My Swing App");setSize(400, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();JButton buttonimport javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MySwingApp extends JFrame { public MySwingApp() { setTitle("My Swing App"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton buttonimport javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MySwingApp extends JFrame { public MySwingApp() { setTitle("My Swing App"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton button
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END