21xrx.com
2024-11-05 14:58:15 Tuesday
登录
文章检索 我的文章 写文章
Java 应用程序开发实例及代码
2023-06-12 11:21:07 深夜i     --     --
Java 应用程序开发

Java 作为一种广泛使用的编程语言,具有广泛的应用范围。它不仅是服务器端开发的首选语言,而且还可以用于应用程序开发。今天我们将介绍一些 Java 开发应用程序的实例和代码。

1. 计算器应用

计算器是一种非常简单的应用程序,使用 Java 编写代码可以很容易地构建这样的应用程序。我们可以使用 Java 图形用户界面 Swing 框架来实现用户界面,并在代码中添加计算逻辑。以下是一个简单的计算器应用程序代码示例:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame

{

  private static final long serialVersionUID = 1L;

  private JButton jbtNum1 = new JButton("1");

  private JButton jbtNum2 = new JButton("2");

  private JButton jbtNum3 = new JButton("3");

  private JButton jbtDivide = new JButton("/");

  private JButton jbtNum4 = new JButton("4");

  private JButton jbtNum5 = new JButton("5");

  private JButton jbtNum6 = new JButton("6");

  private JButton jbtMultiply = new JButton("*");

  private JButton jbtNum7 = new JButton("7");

  private JButton jbtNum8 = new JButton("8");

  private JButton jbtNum9 = new JButton("9");

  private JButton jbtSubtract = new JButton("-");

  private JButton jbtNum0 = new JButton("0");

  private JButton jbtEquals = new JButton("=");

  private JButton jbtAdd = new JButton("+");

  private JButton jbtClear = new JButton("C");

  private TextField tfResult = new TextField("0");

  private double num1;

  private double num2;

  private String operator;

  public Calculator()

  {

    JPanel p1 = new JPanel(new GridLayout(4, 3));

    p1.add(jbtNum1);

    p1.add(jbtNum2);

    p1.add(jbtNum3);

    p1.add(jbtNum4);

    p1.add(jbtNum5);

    p1.add(jbtNum6);

    p1.add(jbtNum7);

    p1.add(jbtNum8);

    p1.add(jbtNum9);

    p1.add(jbtNum0);

    p1.add(jbtClear);

    JPanel p2 = new JPanel(new GridLayout(4, 1));

    p2.add(jbtDivide);

    p2.add(jbtMultiply);

    p2.add(jbtSubtract);

    p2.add(jbtAdd);

    JPanel p3 = new JPanel(new FlowLayout());

    p3.add(tfResult);

    JPanel p4 = new JPanel(new GridLayout(1, 1));

    p4.add(jbtEquals);

    JPanel p5 = new JPanel(new BorderLayout());

    p5.add(p3, BorderLayout.NORTH);

    p5.add(p4, BorderLayout.CENTER);

    JPanel p6 = new JPanel(new BorderLayout());

    p6.add(p1, BorderLayout.CENTER);

    p6.add(p2, BorderLayout.EAST);

    add(p5, BorderLayout.NORTH);

    add(p6, BorderLayout.CENTER);

    jbtNum1.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum2.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum3.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum4.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum5.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum6.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum7.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum8.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum9.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtNum0.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

      }

    });

    jbtDivide.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

        operator = "/";

        tfResult.setText("");

      }

    });

    jbtMultiply.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

        operator = "*";

        tfResult.setText("");

      }

    });

    jbtSubtract.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

        operator = "-";

        tfResult.setText("");

      }

    });

    jbtAdd.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

        operator = "+";

        tfResult.setText("");

      }

    });

    jbtEquals.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

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

        if (operator.equals("/"))

        {

          tfResult.setText(String.valueOf(num1 / num2));

        }

        if (operator.equals("*"))

        {

          tfResult.setText(String.valueOf(num1 * num2));

        }

        if (operator.equals("-"))

        {

          tfResult.setText(String.valueOf(num1 - num2));

        }

        if (operator.equals("+"))

        {

          tfResult.setText(String.valueOf(num1 + num2));

        }

      }

    });

    jbtClear.addActionListener(new ActionListener()

    {

      public void actionPerformed(ActionEvent e)

      {

        tfResult.setText("0");

      }

    });

  }

  public static void main(String[] args)

  {

    Calculator frame = new Calculator();

    frame.setTitle("Calculator");

    frame.setSize(400, 400);

    frame.setVisible(true);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

}

2. 音乐播放器应用

Java 还可以用于音乐播放器的开发。我们可以使用 Java 的音频库来处理音频文件,并使用 JavaFX 框架实现用户界面。这里是一个音乐播放器应用程序代码示例:

import java.io.File;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.media.Media;

import javafx.scene.media.MediaPlayer;

import javafx.stage.FileChooser;

import javafx.stage.Stage;

public class MusicPlayer extends Application

{

  private static final String MUSIC_PATH = "music/";

  private Media media;

  private MediaPlayer mediaPlayer;

  public static void main(String[] args)

  {

    launch(args);

  }

  @Override

  public void start(final Stage primaryStage) throws Exception

  {

    Button openButton = new Button("打开音乐文件");

    Label statusLabel = new Label("请打开音乐文件");

    openButton.setOnAction(new EventHandler ()

    {

      @Override

      public void handle(ActionEvent arg0)

      {

        FileChooser fileChooser = new FileChooser();

        fileChooser.setTitle("选择音乐文件");

        fileChooser.setInitialDirectory(new File(MUSIC_PATH));

        File selectedFile = fileChooser.showOpenDialog(primaryStage);

        if (selectedFile != null)

        {

          String path = selectedFile.toURI().toString();

          media = new Media(path);

          mediaPlayer = new MediaPlayer(media);

          mediaPlayer.setAutoPlay(true);

          statusLabel.setText("正在播放:" + selectedFile.getName());

        }

      }

    });

    Scene scene = new Scene(new javafx.scene.layout.VBox(openButton, statusLabel), 400, 100);

    primaryStage.setScene(scene);

    primaryStage.setTitle("音乐播放器");

    primaryStage.show();

  }

}

3. 图片查看器应用

Java 还可以用于图片查看器的开发。我们可以使用 JavaFX 框架实现用户界面,并使用 Java 的图像处理库来加载和处理图像。以下是一个简单的图片查看器应用程序代码示例:

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.StackPane;

import javafx.stage.FileChooser;

import javafx.stage.Stage;

public class ImageViewer extends Application

{

  private static final String IMAGE_PATH = "images/";

  private ImageView imageView;

  public static void main(String[] args)

  {

    launch(args);

  }

  @Override

  public void start(final Stage primaryStage) throws Exception

  {

    imageView = new ImageView();

    imageView.setFitWidth(400);

    imageView.setFitHeight(400);

    StackPane root = new StackPane();

    root.getChildren().add(imageView);

    primaryStage.setTitle("图片查看器");

    Scene scene = new Scene(root, 400, 400);

    primaryStage.setScene(scene);

    primaryStage.show();

    FileChooser fileChooser = new FileChooser();

    fileChooser.setTitle("选择图片文件");

    fileChooser.setInitialDirectory(new File(IMAGE_PATH));

    File selectedFile = fileChooser.showOpenDialog(primaryStage);

    if (selectedFile != null)

    {

      String path = selectedFile.getPath();

      try

      {

        BufferedImage bufferedImage = ImageIO.read(selectedFile);

        Image image = SwingFXUtils.toFXImage(bufferedImage, null);

        imageView.setImage(image);

        primaryStage.setTitle("图片查看器:" + selectedFile.getName());

      }

      catch (IOException ex)

      {

        System.out.println("无法打开文件:" + selectedFile.getAbsolutePath());

      }

    }

  }

}

3个 ,Swing 框架,JavaFX 框架。

  
  

评论区

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