21xrx.com
2024-12-23 03:40:38 Monday
登录
文章检索 我的文章 写文章
Java应用程序开发实例
2023-06-15 00:16:14 深夜i     --     --
Java 应用程序 Swing框架

Java作为一门广泛应用于企业级应用开发的语言,其强大的跨平台特性以及兼具面向对象特性的编程语言,使其成为了软件开发领域的一种重要语言。在本篇文章中,将会通过一个Java应用程序的开发实例,展示Java在应用开发领域的应用。

本实例将基于Java Swing框架开发一个简单的窗口应用程序,实现一个计算器,用户可以通过该应用程序进行简单的四则运算。以下是Java应用程序的开发步骤:

第一步:创建Java项目和类文件

在Eclipse或IntelliJ IDEA等Java开发工具中,创建一个Java项目,并在该项目中创建一个Calculator类。

第二步:编写Java程序

在Calculator类中,使用Swing框架中的JFrame类创建一个应用程序窗口,并在该窗口中添加一些按钮和文本框等控件,实现一个简单的计算器。

代码案例:


import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.Font;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Calculator extends JFrame {

private JPanel contentPane;

private JTextField textField;

private JButton btn1;

private JButton btn2;

private JButton btn3;

private JButton btn4;

private JButton btn5;

private JButton btn6;

private JButton btn7;

private JButton btn8;

private JButton btn9;

private JButton btn0;

private JButton btnPlus;

private JButton btnMinus;

private JButton btnMultiple;

private JButton btnDivide;

private JButton btnClear;

private JButton btnEquals;

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Calculator frame = new Calculator();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public Calculator() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 300, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

textField = new JTextField();

textField.setBounds(10, 11, 264, 36);

contentPane.add(textField);

textField.setColumns(10);

btn1 = new JButton("1");

btn1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "1");

}

});

btn1.setFont(new Font("Tahoma", Font.BOLD, 18));

btn1.setBounds(10, 58, 60, 36);

contentPane.add(btn1);

btn2 = new JButton("2");

btn2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "2");

}

});

btn2.setFont(new Font("Tahoma", Font.BOLD, 18));

btn2.setBounds(80, 58, 60, 36);

contentPane.add(btn2);

btn3 = new JButton("3");

btn3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "3");

}

});

btn3.setFont(new Font("Tahoma", Font.BOLD, 18));

btn3.setBounds(150, 58, 60, 36);

contentPane.add(btn3);

btnPlus = new JButton("+");

btnPlus.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "+");

}

});

btnPlus.setFont(new Font("Tahoma", Font.BOLD, 18));

btnPlus.setBounds(220, 58, 60, 36);

contentPane.add(btnPlus);

btn4 = new JButton("4");

btn4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "4");

}

});

btn4.setFont(new Font("Tahoma", Font.BOLD, 18));

btn4.setBounds(10, 105, 60, 36);

contentPane.add(btn4);

btn5 = new JButton("5");

btn5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "5");

}

});

btn5.setFont(new Font("Tahoma", Font.BOLD, 18));

btn5.setBounds(80, 105, 60, 36);

contentPane.add(btn5);

btn6 = new JButton("6");

btn6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "6");

}

});

btn6.setFont(new Font("Tahoma", Font.BOLD, 18));

btn6.setBounds(150, 105, 60, 36);

contentPane.add(btn6);

btnMinus = new JButton("-");

btnMinus.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "-");

}

});

btnMinus.setFont(new Font("Tahoma", Font.BOLD, 18));

btnMinus.setBounds(220, 105, 60, 36);

contentPane.add(btnMinus);

btn7 = new JButton("7");

btn7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "7");

}

});

btn7.setFont(new Font("Tahoma", Font.BOLD, 18));

btn7.setBounds(10, 152, 60, 36);

contentPane.add(btn7);

btn8 = new JButton("8");

btn8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "8");

}

});

btn8.setFont(new Font("Tahoma", Font.BOLD, 18));

btn8.setBounds(80, 152, 60, 36);

contentPane.add(btn8);

btn9 = new JButton("9");

btn9.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "9");

}

});

btn9.setFont(new Font("Tahoma", Font.BOLD, 18));

btn9.setBounds(150, 152, 60, 36);

contentPane.add(btn9);

btnMultiple = new JButton("*");

btnMultiple.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "*");

}

});

btnMultiple.setFont(new Font("Tahoma", Font.BOLD, 18));

btnMultiple.setBounds(220, 152, 60, 36);

contentPane.add(btnMultiple);

btn0 = new JButton("0");

btn0.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "0");

}

});

btn0.setFont(new Font("Tahoma", Font.BOLD, 18));

btn0.setBounds(80, 199, 60, 36);

contentPane.add(btn0);

btnClear = new JButton("C");

btnClear.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText("");

}

});

btnClear.setFont(new Font("Tahoma", Font.BOLD, 18));

btnClear.setBounds(10, 199, 60, 36);

contentPane.add(btnClear);

btnEquals = new JButton("=");

btnEquals.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String expression = textField.getText();

if (expression.matches("^-?\\d+([+\\-*/]\\d+)+$")) {

  String temp = expression.replaceAll("-", "&");

  String[] nums = temp.split("[+|&|*|/]");

  String[] symbols = expression.split("\\d+");

  double[] newNums = new double[nums.length];

  for (int i = 0; i < nums.length; i++) {

    if (nums[i].contains("&")) {

      newNums[i] = Double.parseDouble(nums[i].replace("&", "-"));

    } else {

      newNums[i] = Double.parseDouble(nums[i]);

    }

  }

  double result = newNums[0];

  for (int i = 1; i < symbols.length; i++) {

    if (symbols[i].equals("+")) {

      result += newNums[i];

    } else if (symbols[i].equals("-")) {

      result -= newNums[i];

    } else if (symbols[i].equals("*")) {

      result *= newNums[i];

    } else if (symbols[i].equals("/")) {

      if (newNums[i] != 0) {

        result /= newNums[i];

      } else {

        textField.setText("除数不能为0!");

      }

    }

  }

  textField.setText(String.valueOf(result));

} else {

  textField.setText("输入格式错误!");

}

}

});

btnEquals.setFont(new Font("Tahoma", Font.BOLD, 18));

btnEquals.setBounds(150, 199, 130, 36);

contentPane.add(btnEquals);

btnDivide = new JButton("/");

btnDivide.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText() + "/");

}

});

btnDivide.setFont(new Font("Tahoma", Font.BOLD, 18));

btnDivide.setBounds(220, 199, 60, 36);

contentPane.add(btnDivide);

}

}

第三步:运行Java应用程序

在开发工具中,单击“运行”按钮或使用快捷键“Ctrl + F11”运行Java应用程序。

通过以上实例,可以看出Java在应用程序开发方面的应用非常广泛,尤其是在企业级应用领域。无论是桌面应用程序、Web应用程序还是移动应用程序,都可以使用Java语言进行开发。同时也要注意Java应用程序的安全性和稳定性,因为大量的企业级应用程序正在使用Java语言进行开发。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复