Java-GUI 编程之 Swing

🚀 优质资源分享 🚀

学习路线指引(点击解锁) 知识定位 人群定位
🧡 Python实战微信订餐小程序 🧡 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
💛Python量化交易实战💛 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

Swing概述

实际使用 Java 开发图形界面程序时 ,很少使用 AWT 组件,绝大部分时候都是用 Swing 组件开发的 。 Swing是由100%纯 Java实现的,不再依赖于本地平台的 GUI, 因此可以在所有平台上都保持相同的界面外观。独立于本地平台的Swing组件被称为轻量级组件;而依赖于本地平台的 AWT 组件被称为重量级组件
 由于 Swing 的所有组件完全采用 Java 实现,不再调用本地平台的 GUI,所以导致 Swing 图形界面的显示速度要比 AWT 图形界面的显示速度慢一些,但相对于快速发展的硬件设施而言,这种微小的速度差别无妨大碍。

使用Swing的优势:

  1. Swing 组件不再依赖于本地平台的 GUI,无须采用各种平台的 GUI 交集 ,因此 Swing 提供了大量图形界面组件 , 远远超出了 AWT 所提供的图形界面组件集。
  2. Swing 组件不再依赖于本地平台 GUI ,因此不会产生与平台 相关的 bug 。
  3. Swing 组件在各种平台上运行时可以保证具有相同的图形界面外观。
  4. Swing 提供的这些优势,让 Java 图形界面程序真正实现了 " Write Once, Run Anywhere" 的 目标。

Swing的特征:

1.Swing 组件采用 MVC(Model-View-Controller, 即模型一视图一控制器)设计模式:

  • 模型(Model): 用于维护组件的各种状态;
  • 视图(View): 是组件的可视化表现;
  • 控制器(Controller):用于控制对于各种事件、组件做出响应 。

当模型发生改变时,它会通知所有依赖它的视图,视图会根据模型数据来更新自己。Swing使用UI代理来包装视图和控制器, 还有一个模型对象来维护该组件的状态。例如,按钮JButton有一个维护其状态信息的模型ButtonModel对象 。 Swing组件的模型是自动设置的,因此一般都使用JButton,而无须关心ButtonModel对象。

2.Swing在不同的平台上表现一致,并且有能力提供本地平台不支持的显示外观 。由于 Swing采用 MVC 模式来维护各组件,所以 当组件的外观被改变时,对组件的状态信息(由模型维护)没有任何影响 。因 此,Swing可以使用插拔式外观感觉 (Pluggable Look And Feel, PLAF)来控制组件外观,使得 Swing图形界面在同一个平台上运行时能拥有不同的外观,用户可以选择自己喜欢的外观 。相比之下,在 AWT 图形界面中,由于控制组件外观的对等类与具体平台相关 ,因此 AWT 组件总是具有与本地平台相同的外观 。

Swing组件层次

Swing组件继承体系图:

​ 大部分Swing 组件都是 JComponent抽象类的直接或间接子类(并不是全部的 Swing 组件),JComponent 类定义了所有子类组件的通用方法 ,JComponent 类是 AWT 里 java.awt. Container 类的子类 ,这也是 AWT 和 Swing 的联系之一。 绝大部分 Swing 组件类继承了 Container类,所以Swing 组件都可作为 容器使用 ( JFrame继承了Frame 类)。

Swing组件和AWT组件的对应关系:

​ 大部分情况下&#x

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Java GUI编程实现计算器,你可以使用SwingAWT这两种Java GUI工具包之一。 以下是一个简单的Java GUI计算器的实现示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JButton[] numberButtons; private JButton[] operatorButtons; private JButton clearButton, equalsButton, decimalButton; private JTextField textField; private double num1 = 0, num2 = 0, result = 0; private char operator; public Calculator() { setTitle("Calculator"); // Create the number buttons numberButtons = new JButton[10]; for (int i = 0; i < 10; i++) { numberButtons[i] = new JButton(String.valueOf(i)); } // Create the operator buttons operatorButtons = new JButton[4]; operatorButtons[0] = new JButton("+"); operatorButtons[1] = new JButton("-"); operatorButtons[2] = new JButton("*"); operatorButtons[3] = new JButton("/"); // Create the clear, equals, and decimal buttons clearButton = new JButton("C"); equalsButton = new JButton("="); decimalButton = new JButton("."); // Create the text field textField = new JTextField(10); textField.setEditable(false); // Set the layout manager and add the components setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.gridwidth = 4; add(textField, gc); gc.gridwidth = 1; gc.gridy = 1; for (int i = 0; i < 10; i++) { gc.gridx = i % 3; gc.gridy = i / 3 + 2; add(numberButtons[i], gc); numberButtons[i].addActionListener(this); } gc.gridx = 0; gc.gridy = 5; add(operatorButtons[0], gc); operatorButtons[0].addActionListener(this); gc.gridx = 1; add(operatorButtons[1], gc); operatorButtons[1].addActionListener(this); gc.gridx = 2; add(operatorButtons[2], gc); operatorButtons[2].addActionListener(this); gc.gridx = 3; add(operatorButtons[3], gc); operatorButtons[3].addActionListener(this); gc.gridx = 0; gc.gridy = 6; add(clearButton, gc); clearButton.addActionListener(this); gc.gridx = 1; add(equalsButton, gc); equalsButton.addActionListener(this); gc.gridx = 2; add(decimalButton, gc); decimalButton.addActionListener(this); setSize(200, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { for (int i = 0; i < 10; i++) { if (e.getSource() == numberButtons[i]) { textField.setText(textField.getText() + i); } } if (e.getSource() == decimalButton) { textField.setText(textField.getText() + "."); } if (e.getSource() == clearButton) { textField.setText(""); num1 = 0; num2 = 0; result = 0; operator = ' '; } if (e.getSource() == operatorButtons[0]) { num1 = Double.parseDouble(textField.getText()); operator = '+'; textField.setText(""); } if (e.getSource() == operatorButtons[1]) { num1 = Double.parseDouble(textField.getText()); operator = '-'; textField.setText(""); } if (e.getSource() == operatorButtons[2]) { num1 = Double.parseDouble(textField.getText()); operator = '*'; textField.setText(""); } if (e.getSource() == operatorButtons[3]) { num1 = Double.parseDouble(textField.getText()); operator = '/'; textField.setText(""); } if (e.getSource() == equalsButton) { num2 = Double.parseDouble(textField.getText()); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } textField.setText(Double.toString(result)); } } public static void main(String[] args) { new Calculator(); } } ``` 这个示例应该可以实现一个简单的Java GUI计算器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值