21xrx.com
2024-12-22 22:40:39 Sunday
登录
文章检索 我的文章 写文章
Java应用领域探究——从桌面应用到Web开发
2023-06-14 20:52:56 深夜i     --     --
Java 应用领域 桌面应用 Web开发 代码案例

Java语言的强大和全面性使其成为应用开发的首选语言之一。Java应用领域广泛,从简单的桌面应用程序到大型企业级Web应用程序,都可以使用Java来开发。

1. 桌面应用程序:Java提供了Swing和AWT等GUI库,可以轻松创建桌面应用程序。以下是一个使用Swing库的简单计算器示例:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener {

  private JTextField display;

  private double num1, num2, result;

  private char operator;

  public Calculator() {

    super("计算器");

    setSize(200, 200);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new BorderLayout());

    display = new JTextField("0.0", 20);

    panel.add(display, BorderLayout.NORTH);

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

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

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

      JButton button = new JButton(labels[i]);

      button.addActionListener(this);

      keypad.add(button);

    }

    panel.add(keypad, BorderLayout.CENTER);

    setContentPane(panel);

    setVisible(true);

  }

  public void actionPerformed(ActionEvent e) {

    String label = e.getActionCommand();

    switch (label.charAt(0)) {

      case '0': case '1': case '2': case '3': case '4':

      case '5': case '6': case '7': case '8': case '9':

        if (operator == '\0') {

          num1 = Double.parseDouble(display.getText());

          display.setText(label);

        } else {

          num2 = Double.parseDouble(display.getText());

          display.setText(label);

        }

        break;

      case '+': case '-': case '*': case '/':

        operator = label.charAt(0);

        break;

      case '.':

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

        break;

      case '=':

        num2 = Double.parseDouble(display.getText());

        switch (operator) {

          case '+': result = num1 + num2; break;

          case '-': result = num1 - num2; break;

          case '*': result = num1 * num2; break;

          case '/': result = num1 / num2; break;

        }

        display.setText("" + result);

        operator = '\0';

        break;

    }

  }

  public static void main(String[] args) {

    new Calculator();

  }

}

2. 服务器应用程序:Java SE和Java EE提供了Socket和Servlet等类库,可以用来开发服务器应用程序。以下是一个简单的Socket服务器示例:


import java.io.*;

import java.net.*;

public class Server {

  public static void main(String[] args) {

    try {

      ServerSocket server = new ServerSocket(8000);

      while (true) {

        Socket client = server.accept();

        new Handler(client).start();

      }

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

}

class Handler extends Thread {

  private Socket client;

  public Handler(Socket client)

    this.client = client;

  

  @Override

  public void run() {

    try {

      BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));

      PrintWriter output = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));

      String line;

      while ((line = input.readLine()) != null) {

        output.println("Echo: " + line);

        output.flush();

      }

      input.close();

      output.close();

      client.close();

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

}

3. Web应用程序:Java EE提供了Servlet和JSP等技术,用于开发动态Web应用程序。以下是一个简单的Servlet示例:


import java.io.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/HelloServlet")

public class HelloServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String name = request.getParameter("name");

    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();

    out.println("");

    out.println(" Hello " + name + "");

    out.println("");

    out.println("

Hello " + name + "

");

    out.println("");

    out.println("");

    out.close();

  }

}

  
  

评论区

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