21xrx.com
2024-11-25 06:01:16 Monday
登录
文章检索 我的文章 写文章
Java开发常用的领域以及实例介绍
2023-06-11 13:42:08 深夜i     --     --
Java开发 桌面应用 Web开发 移动开发 GUI Swing Spring Android Flutter

Java是一种广泛使用的编程语言,被应用于各类领域,包括web开发、桌面应用、移动应用、人工智能、游戏开发等。而在Java开发中,常用的一些领域包括:

### 1. Java桌面应用开发

Java提供了丰富的GUI库,可以方便地创建桌面应用程序。下面是一个Java Swing的示例代码,实现了一个简单的计算器:


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Calculator extends JFrame implements ActionListener {

  private JTextField textField;

  private double firstNumber = 0;

  private String operator = "";

  public Calculator() {

    super("计算器");

    init();

    setSize(400, 400);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);

  }

  public void init() {

    textField = new JTextField();

    textField.setFont(new Font("楷体", Font.BOLD, 20));

    textField.setHorizontalAlignment(JTextField.RIGHT);

    textField.setEditable(false);

    add(textField, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel(new GridLayout(4, 4));

    String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};

    for (String button : buttons) {

      JButton btn = new JButton(button);

      btn.setFont(new Font("楷体", Font.BOLD, 20));

      btn.addActionListener(this);

      buttonPanel.add(btn);

    }

    add(buttonPanel, BorderLayout.CENTER);

  }

  @Override

  public void actionPerformed(ActionEvent e) {

    String text = e.getActionCommand();

    if (!text.matches("\\d|\\.")) {

      switch (text) {

        case "/":

        case "*":

        case "+":

        case "-":

          firstNumber = Double.valueOf(textField.getText());

          operator = text;

          textField.setText("");

          break;

        case "=":

          double secondNumber = Double.valueOf(textField.getText());

          double result = 0;

          switch (operator) {

            case "/":

              result = firstNumber / secondNumber;

              break;

            case "*":

              result = firstNumber * secondNumber;

              break;

            case "+":

              result = firstNumber + secondNumber;

              break;

            case "-":

              result = firstNumber - secondNumber;

              break;

          }

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

          break;

      }

    } else {

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

    }

  }

  public static void main(String[] args) {

    new Calculator();

  }

}

### 2. Java Web开发

Java Web开发可以基于Servlet、JSP等技术,也可以使用各种框架,如Spring、Struts、Hibernate等。下面是一个基于Spring框架的Web应用实例:


@Controller

public class HelloController {

  @GetMapping("/hello")

  @ResponseBody

  public String hello() World!";

  

}

### 3. Java移动开发

Java移动开发可以基于Java ME、Android等技术,也可以使用各种框架,如Flutter、React Native等。下面是一个基于Android的移动应用实例:


public class MainActivity extends AppCompatActivity {

  private TextView helloTextView;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    helloTextView = findViewById(R.id.hello_text_view);

    Button helloButton = findViewById(R.id.hello_button);

    helloButton.setOnClickListener(new View.OnClickListener() {

      @Override

      public void onClick(View view) {

        helloTextView.setText("Hello, World!");

      }

    });

  }

}

### 关键词:

Java开发、桌面应用、Web开发、移动开发、GUI、Swing、Spring、Android、Flutter

  
  

评论区

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