21xrx.com
2025-03-23 03:25:05 Sunday
文章检索 我的文章 写文章
Java图形用户界面实验报告
2023-06-16 06:14:56 深夜i     8     0
Java GUI

Java图形用户界面(Java GUI)是指使用Java编程语言创建的图形用户界面。Java GUI可以创建各种窗口、对话框、按钮、文本框、标签等多种控件,并且可以给控件添加事件监听器,以响应用户的输入操作。在本次实验中,我们学习了Java GUI的基本概念和使用方法,并完成了一个小型的图形化计算器程序。

代码实现:

import javax.swing.*; //导入Swing类库
public class Calculator extends JFrame {
  private JLabel label1,label2,result;
  private JTextField input1,input2;
  private JButton add,sub,mul,div;
  public Calculator(){
    //创建控件并设置位置大小
    label1 = new JLabel("Value 1:");
    label1.setBounds(20, 20, 80, 25);
    input1 = new JTextField();
    input1.setBounds(100, 20, 165, 25);
    label2 = new JLabel("Value 2:");
    label2.setBounds(20, 50, 80, 25);
    input2 = new JTextField();
    input2.setBounds(100, 50, 165, 25);
    add = new JButton("+");
    add.setBounds(20, 80, 50, 25);
    sub = new JButton("-");
    sub.setBounds(80, 80, 50, 25);
    mul = new JButton("*");
    mul.setBounds(140, 80, 50, 25);
    div = new JButton("/");
    div.setBounds(200, 80, 50, 25);
    result = new JLabel("");
    result.setBounds(20, 110, 230, 25);
    //添加事件监听器
    add.addActionListener(e -> {
      try {
        double num1 = Double.parseDouble(input1.getText());
        double num2 = Double.parseDouble(input2.getText());
        double ans = num1 + num2;
        result.setText("Result: " + ans);
      } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(null, "Invalid input!");
      }
    });
    sub.addActionListener(e -> {
      try {
        double num1 = Double.parseDouble(input1.getText());
        double num2 = Double.parseDouble(input2.getText());
        double ans = num1 - num2;
        result.setText("Result: " + ans);
      } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(null, "Invalid input!");
      }
    });
    mul.addActionListener(e -> {
      try {
        double num1 = Double.parseDouble(input1.getText());
        double num2 = Double.parseDouble(input2.getText());
        double ans = num1 * num2;
        result.setText("Result: " + ans);
      } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(null, "Invalid input!");
      }
    });
    div.addActionListener(e -> {
      try {
        double num1 = Double.parseDouble(input1.getText());
        double num2 = Double.parseDouble(input2.getText());
        double ans = num1 / num2;
        result.setText("Result: " + ans);
      } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(null, "Invalid input!");
      }
    });
    //将控件添加到窗口中
    add(label1);
    add(input1);
    add(label2);
    add(input2);
    add(add);
    add(sub);
    add(mul);
    add(div);
    add(result);
    //设置窗口的基本属性
    setTitle("Calculator");
    setSize(300,200);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
  }
  public static void main(String[] args){
    Calculator calculator = new Calculator();
    calculator.setVisible(true);
  }
}

该程序使用Swing类库创建了一个窗口,包含两个输入框、四个按钮和一个标签控件。按钮分别对应加、减、乘、除四种操作,当用户点击按钮时,程序会将输入框中的内容转换成数值并进行相应的计算,最后将结果显示在标签中。

、Swing、事件监听器

  
  

评论区

请求出错了