21xrx.com
2025-06-22 21:03:39 Sunday
文章检索 我的文章 写文章
Java程序的分类及代码实例
2023-06-15 16:26:38 深夜i     --     --
Java程序分类 基于JVM的应用程序 基于JVM的系统程序

Java是一种常用的编程语言,可以开发各种类型的程序。但是,Java程序可以分为两大类:基于JVM的应用程序和基于JVM的系统程序。

基于JVM的应用程序是指运行在JVM(Java虚拟机)上的应用程序。这种程序可以在Windows、Mac OS、Linux和其他操作系统上运行。这些程序通常是面向对象的,使用Java API和第三方库来实现各种功能。以下是一个基于JVM的应用程序的代码示例,它用于创建一个简单的文本编辑器:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextEditor extends JFrame {
  private static final long serialVersionUID = 1L;
  private JTextArea textArea = new JTextArea();
  public TextEditor() {
    setLayout(new BorderLayout());
    add(new JScrollPane(textArea), BorderLayout.CENTER);
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem openMenuItem = new JMenuItem("Open");
    JMenuItem saveMenuItem = new JMenuItem("Save");
    JMenuItem exitMenuItem = new JMenuItem("Exit");
    openMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showOpenDialog(TextEditor.this) == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          // 读取文件并将内容显示在文本区域中
        }
      }
    });
    saveMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showSaveDialog(TextEditor.this) == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          // 将文本区域中的内容保存到文件中
        }
      }
    });
    exitMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    fileMenu.add(openMenuItem);
    fileMenu.add(saveMenuItem);
    fileMenu.addSeparator();
    fileMenu.add(exitMenuItem);
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);
  }
  public static void main(String[] args) {
    TextEditor editor = new TextEditor();
    editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    editor.setSize(300, 300);
    editor.setVisible(true);
  }
}

另一方面,基于JVM的系统程序是指运行在JVM上的系统程序。这种程序通常是有特殊目的的,例如Java虚拟机本身、应用服务器和其他中间件、框架和库等。这些程序也使用Java API和第三方库,但与基于JVM的应用程序不同,它们更多地关注系统层面的问题,例如线程、内存管理和网络通信。以下是一个基于JVM的系统程序的代码示例,它使用Java Socket API创建了一个简单的HTTP服务器:

import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleHttpServer {
  private static Map
  contentTypes = new HashMap 
  
   () {
  
 
    private static final long serialVersionUID = 1L;
    { put("html", "text/html"); put("txt", "text/plain"); }
  };
  public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(8080);
    while (true) {
      Socket clientSocket = serverSocket.accept();
      HttpRequest request = new HttpRequest(clientSocket.getInputStream());
      HttpResponse response = new HttpResponse(clientSocket.getOutputStream());
      response.setContentType("text/html");
      response.write("
Hello, world!".getBytes()); 
      clientSocket.close();
    }
  }
  private static class HttpRequest {
    private InputStream input;
    private String method;
    private String uri;
    public HttpRequest(InputStream input) throws Exception {
      this.input = input;
      BufferedReader reader = new BufferedReader(new InputStreamReader(input));
      String line = reader.readLine();
      String[] parts = line.split(" ");
      method = parts[0];
      uri = parts[1];
    }
    public String getMethod() {
      return method;
    }
    public String getUri() {
      return uri;
    }
  }
  private static class HttpResponse {
    private OutputStream output;
    private String contentType;
    public HttpResponse(OutputStream output) {
      this.output = output;
    }
    public void setContentType(String contentType) {
      this.contentType = contentType;
    }
    public void write(byte[] data) throws Exception {
      writeHeaders();
      output.write(data);
      output.flush();
    }
    private void writeHeaders() throws Exception {
      output.write("HTTP/1.1 200 OK\r\n".getBytes());
      output.write(("Content-Type: " + contentType + "\r\n").getBytes());
      output.write("\r\n".getBytes());
    }
  }
}

  
  

评论区

    相似文章