代码提交
This commit is contained in:
47
swing-and-javafx/pom.xml
Normal file
47
swing-and-javafx/pom.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.mangmang</groupId>
|
||||
<artifactId>learning-nexus</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<name>swing-and-javafx</name>
|
||||
<artifactId>swing-and-javafx</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<javafx.version>21</javafx.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-controls</artifactId>
|
||||
<version>${javafx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-fxml</artifactId>
|
||||
<version>${javafx.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-maven-plugin</artifactId>
|
||||
<version>0.0.8</version>
|
||||
<configuration>
|
||||
<mainClass>com.mangmang.HelloWorldFX</mainClass> <!-- 替换成你的主类 -->
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.mangmang;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class HelloWordSwing {
|
||||
public static void main(String[] args) {
|
||||
JFrame jFrame = new JFrame("Hello World Swing");
|
||||
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
jFrame.setSize(300, 300);
|
||||
|
||||
|
||||
JButton button = new JButton("点击我");
|
||||
button.addActionListener(e -> {
|
||||
JOptionPane.showConfirmDialog(jFrame, "你点击了按钮");
|
||||
});
|
||||
|
||||
|
||||
jFrame.getContentPane().add(button, BorderLayout.CENTER);
|
||||
|
||||
jFrame.setLocationRelativeTo(null);
|
||||
jFrame.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.mangmang;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class HelloWorldFX extends Application {
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
Button btn = new Button("点击我");
|
||||
btn.setOnAction(e -> System.out.println("你好,JavaFX!"));
|
||||
|
||||
StackPane root = new StackPane();
|
||||
root.getChildren().add(btn);
|
||||
|
||||
Scene scene = new Scene(root, 300, 250);
|
||||
|
||||
primaryStage.setTitle("Hello JavaFX");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
package com.mangmang;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.Dragboard;
|
||||
import javafx.scene.input.TransferMode;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class ImageConverterApp extends Application {
|
||||
|
||||
private ImageView imagePreview;
|
||||
private Label statusLabel;
|
||||
private Label imageInfoLabel;
|
||||
private ComboBox<String> formatComboBox;
|
||||
private TextField outputDirField;
|
||||
private List<File> selectedFiles;
|
||||
|
||||
// 支持的图像格式
|
||||
private final String[] supportedFormats = {"JPEG", "PNG", "BMP", "GIF", "TIFF"};
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
primaryStage.setTitle("图像格式转换工具");
|
||||
|
||||
// 创建主布局
|
||||
BorderPane mainLayout = new BorderPane();
|
||||
mainLayout.setPadding(new Insets(10));
|
||||
|
||||
// 创建顶部工具栏
|
||||
HBox toolbar = createToolbar();
|
||||
mainLayout.setTop(toolbar);
|
||||
|
||||
// 创建中间内容区域
|
||||
VBox centerContent = createCenterContent();
|
||||
mainLayout.setCenter(centerContent);
|
||||
|
||||
// 创建底部状态栏
|
||||
HBox statusBar = createStatusBar();
|
||||
mainLayout.setBottom(statusBar);
|
||||
|
||||
// 设置场景
|
||||
Scene scene = new Scene(mainLayout, 800, 600);
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
private HBox createToolbar() {
|
||||
HBox toolbar = new HBox(10);
|
||||
toolbar.setPadding(new Insets(10));
|
||||
|
||||
// 导入按钮
|
||||
Button importButton = new Button("导入图片");
|
||||
importButton.setOnAction(e -> importImages());
|
||||
|
||||
// 格式选择下拉框
|
||||
Label formatLabel = new Label("转换格式:");
|
||||
formatComboBox = new ComboBox<>();
|
||||
formatComboBox.getItems().addAll(supportedFormats);
|
||||
formatComboBox.setValue(supportedFormats[0]);
|
||||
|
||||
// 输出目录
|
||||
Label outputDirLabel = new Label("输出目录:");
|
||||
outputDirField = new TextField(System.getProperty("user.home"));
|
||||
outputDirField.setPrefWidth(200);
|
||||
Button browseButton = new Button("浏览...");
|
||||
browseButton.setOnAction(e -> browseOutputDirectory());
|
||||
|
||||
// 转换按钮
|
||||
Button convertButton = new Button("开始转换");
|
||||
convertButton.setOnAction(e -> convertImages());
|
||||
|
||||
toolbar.getChildren().addAll(
|
||||
importButton, new Separator(javafx.geometry.Orientation.VERTICAL),
|
||||
formatLabel, formatComboBox, new Separator(javafx.geometry.Orientation.VERTICAL),
|
||||
outputDirLabel, outputDirField, browseButton, new Separator(javafx.geometry.Orientation.VERTICAL),
|
||||
convertButton
|
||||
);
|
||||
|
||||
return toolbar;
|
||||
}
|
||||
|
||||
private VBox createCenterContent() {
|
||||
VBox centerContent = new VBox(10);
|
||||
centerContent.setPadding(new Insets(10));
|
||||
|
||||
// 图片预览区域
|
||||
Label previewLabel = new Label("预览:");
|
||||
imagePreview = new ImageView();
|
||||
imagePreview.setFitHeight(300);
|
||||
imagePreview.setFitWidth(400);
|
||||
imagePreview.setPreserveRatio(true);
|
||||
|
||||
// 图片信息标签
|
||||
imageInfoLabel = new Label("未加载图片");
|
||||
|
||||
// 质量设置滑块
|
||||
HBox qualityBox = new HBox(10);
|
||||
Label qualityLabel = new Label("图像质量:");
|
||||
Slider qualitySlider = new Slider(0, 100, 80);
|
||||
qualitySlider.setShowTickLabels(true);
|
||||
qualitySlider.setShowTickMarks(true);
|
||||
qualitySlider.setMajorTickUnit(25);
|
||||
qualitySlider.setMinorTickCount(5);
|
||||
Label qualityValueLabel = new Label("80%");
|
||||
qualitySlider.valueProperty().addListener((obs, oldVal, newVal) -> qualityValueLabel.setText(String.format("%.0f%%", newVal.doubleValue())));
|
||||
qualityBox.getChildren().addAll(qualityLabel, qualitySlider, qualityValueLabel);
|
||||
|
||||
// 创建拖放区域
|
||||
VBox dropZone = new VBox();
|
||||
dropZone.getChildren().addAll(imagePreview);
|
||||
dropZone.setStyle("-fx-border-color: #cccccc; -fx-border-style: dashed; -fx-background-color: #f7f7f7;");
|
||||
dropZone.setPadding(new Insets(20));
|
||||
dropZone.setAlignment(javafx.geometry.Pos.CENTER);
|
||||
|
||||
// 设置拖放功能
|
||||
setupDragAndDrop(dropZone);
|
||||
|
||||
centerContent.getChildren().addAll(previewLabel, dropZone, imageInfoLabel, qualityBox);
|
||||
return centerContent;
|
||||
}
|
||||
|
||||
private HBox createStatusBar() {
|
||||
HBox statusBar = new HBox();
|
||||
statusBar.setPadding(new Insets(5));
|
||||
statusBar.setStyle("-fx-background-color: #e0e0e0;");
|
||||
|
||||
statusLabel = new Label("就绪");
|
||||
statusBar.getChildren().add(statusLabel);
|
||||
|
||||
return statusBar;
|
||||
}
|
||||
|
||||
private void setupDragAndDrop(VBox dropZone) {
|
||||
dropZone.setOnDragOver(event -> {
|
||||
if (event.getDragboard().hasFiles()) {
|
||||
event.acceptTransferModes(TransferMode.COPY);
|
||||
}
|
||||
event.consume();
|
||||
});
|
||||
|
||||
dropZone.setOnDragDropped(event -> {
|
||||
Dragboard db = event.getDragboard();
|
||||
boolean success = false;
|
||||
if (db.hasFiles()) {
|
||||
selectedFiles = db.getFiles();
|
||||
success = true;
|
||||
updatePreview(selectedFiles.get(0));
|
||||
}
|
||||
event.setDropCompleted(success);
|
||||
event.consume();
|
||||
});
|
||||
}
|
||||
|
||||
private void importImages() {
|
||||
FileChooser fileChooser = new FileChooser();
|
||||
fileChooser.setTitle("选择图片文件");
|
||||
fileChooser.getExtensionFilters().addAll(
|
||||
new FileChooser.ExtensionFilter("图像文件", "*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff")
|
||||
);
|
||||
selectedFiles = fileChooser.showOpenMultipleDialog(null);
|
||||
|
||||
if (selectedFiles != null && !selectedFiles.isEmpty()) {
|
||||
updatePreview(selectedFiles.get(0));
|
||||
statusLabel.setText("已选择 " + selectedFiles.size() + " 个文件");
|
||||
}
|
||||
}
|
||||
|
||||
private void browseOutputDirectory() {
|
||||
javafx.stage.DirectoryChooser directoryChooser = new javafx.stage.DirectoryChooser();
|
||||
directoryChooser.setTitle("选择输出目录");
|
||||
File selectedDirectory = directoryChooser.showDialog(null);
|
||||
|
||||
if (selectedDirectory != null) {
|
||||
outputDirField.setText(selectedDirectory.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
private void updatePreview(File imageFile) {
|
||||
try {
|
||||
Image image = new Image(imageFile.toURI().toString());
|
||||
imagePreview.setImage(image);
|
||||
|
||||
// 更新图像信息
|
||||
String format = imageFile.getName().substring(imageFile.getName().lastIndexOf('.') + 1).toUpperCase();
|
||||
imageInfoLabel.setText(String.format(
|
||||
"文件名: %s\n尺寸: %.0fx%.0f\n格式: %s\n大小: %.2f KB",
|
||||
imageFile.getName(),
|
||||
image.getWidth(),
|
||||
image.getHeight(),
|
||||
format,
|
||||
imageFile.length() / 1024.0
|
||||
));
|
||||
} catch (Exception e) {
|
||||
statusLabel.setText("加载图片预览失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void convertImages() {
|
||||
if (selectedFiles == null || selectedFiles.isEmpty()) {
|
||||
showAlert("请先选择图片文件");
|
||||
return;
|
||||
}
|
||||
|
||||
String targetFormat = formatComboBox.getValue().toLowerCase();
|
||||
String outputDirectory = outputDirField.getText();
|
||||
File outputDir = new File(outputDirectory);
|
||||
|
||||
if (!outputDir.exists() || !outputDir.isDirectory()) {
|
||||
if (!outputDir.mkdirs()) {
|
||||
showAlert("创建输出目录失败");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int convertedCount = 0;
|
||||
for (File file : selectedFiles) {
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(file);
|
||||
if (image == null) {
|
||||
statusLabel.setText("无法读取图片: " + file.getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
String fileName = file.getName().substring(0, file.getName().lastIndexOf('.'));
|
||||
File outputFile = new File(outputDir, fileName + "." + targetFormat);
|
||||
|
||||
// 对于JPEG格式,可以设置质量
|
||||
ImageIO.write(image, targetFormat, outputFile);
|
||||
|
||||
convertedCount++;
|
||||
} catch (IOException e) {
|
||||
statusLabel.setText("转换失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
statusLabel.setText("转换完成,成功转换 " + convertedCount + " 个文件");
|
||||
showAlert("转换完成", "成功转换 " + convertedCount + " 个文件到 " + outputDirectory, Alert.AlertType.INFORMATION);
|
||||
}
|
||||
|
||||
private void showAlert(String message) {
|
||||
showAlert("提示", message, Alert.AlertType.WARNING);
|
||||
}
|
||||
|
||||
private void showAlert(String title, String message, Alert.AlertType alertType) {
|
||||
Alert alert = new Alert(alertType);
|
||||
alert.setTitle(title);
|
||||
alert.setHeaderText(null);
|
||||
alert.setContentText(message);
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user