ข้ามไปที่เนื้อหาหลัก

เรื่องที่แนะนำ

คู่มือ Java ฉบับทันสมัย (อัปเดตล่าสุดปี 2025)

 คู่มือ Java ฉบับทันสมัย (อัปเดตล่าสุดปี 2025) บทนำ Java ยังคงเป็นภาษายอดนิยมที่ได้รับความนิยมอย่างต่อเนื่องตั้งแต่ยุค 1990 จนถึงปัจจุบัน ในปี 2025 Java ได้ก้าวหน้าอย่างมากทั้งด้านภาษาหลักและเครื่องมือสนับสนุน ล่าสุด Java 24 ได้เปิดตัวออกมาแล้ว และ Java 21 เป็นเวอร์ชัน LTS (Long-Term Support) ที่แนะนำให้ใช้งานสำหรับระบบจริงในระยะยาว บทความนี้จะช่วยให้คุณเข้าใจภาพรวมของ Java เวอร์ชันล่าสุด รวมถึงฟีเจอร์เด่น โครงสร้างภาษาที่ควรรู้ และแนวทางการพัฒนา Java ยุคใหม่ ภาพรวมเวอร์ชัน Java ล่าสุด เวอร์ชัน สถานะ วันเปิดตัว Java 24 เวอร์ชันล่าสุด มีนาคม 2025 Java 21 เวอร์ชัน LTS ล่าสุด กันยายน 2023 Java 17 LTS รุ่นก่อนหน้า กันยายน 2021 Java 11 LTS เก่า กันยายน 2018 โครงสร้างพื้นฐานของภาษา Java คลาสและอ็อบเจกต์ : โครงสร้างหลักของโปรแกรม Java Primitive Types : int, double, char, boolean เป็นต้น Control Statements : if, switch, while, for, do-while Methods : การแยกโค้ดเป็นหน่วยที่นำกลับมาใช้ซ้ำได้ Array และ Collection : จัดเก็บและจัดการข้อมูลหลายรายการ Exception Handling : try-catch-finally OOP Co...

สอนสร้าง Slot Machine ด้วย JavaFX

วิธีการตั้งค่า pom.xml สำหรับโปรเจ็กต์ Slot Machine ด้วย JavaFX

  1. สร้างไฟล์ pom.xml สำหรับโครงการ Maven และใส่การตั้งค่าดังนี้:
<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>
    <groupId>com.example</groupId>
    <artifactId>slot-machine</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>17</java.version>
        <javafx.version>20</javafx.version>
    </properties>
    <dependencies>
        <!-- JavaFX Dependencies -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>${javafx.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>SlotMachine</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

คำอธิบาย:

  • JavaFX Version: กำหนดเวอร์ชันของ JavaFX ตามเวอร์ชันที่คุณติดตั้ง (ตัวอย่างเช่น 20).
  • Java Version: กำหนดเวอร์ชัน Java (เช่น 17) ที่ตรงกับระบบของคุณ.
  • Plugin: ใช้ javafx-maven-plugin เพื่อช่วยรันและสร้างโปรเจกต์ JavaFX.

ตัวอย่างโค้ด

/**
 * JavaFX Slot Machine เกมสุ่มสัญลักษณ์โดยใช้ Emoticons
 *
 * โค้ดนี้สร้างเกมสล็อตแมชชีนอย่างง่าย โดยแสดงผลเป็น Emoticons
 */

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.Random;

public class SlotMachine extends Application {

    // กำหนดรายการ Emoticons ที่จะใช้ในสล็อตแมชชีน
    private static final String[] EMOTICONS = {"\uD83D\uDE00", "\uD83D\uDE0D", "\uD83D\uDE02", "\uD83D\uDE31", "\uD83D\uDE21"};
    private final Text[] slots = new Text[3];
    private final Random random = new Random();
    private Timeline spinTimeline;

    @Override
    public void start(Stage primaryStage) {
        // สร้างข้อความสำหรับแสดงผลสล็อต
        for (int i = 0; i < slots.length; i++) {
            slots[i] = new Text(EMOTICONS[random.nextInt(EMOTICONS.length)]);
            slots[i].setFont(Font.font(50));
        }

        // ปุ่มสำหรับเริ่มการหมุนสล็อต
        Button spinButton = new Button("Spin");
        spinButton.setFont(Font.font(20));
        spinButton.setOnAction(e -> spinSlots());

        // จัดตำแหน่งสล็อตและปุ่มใน VBox
        VBox layout = new VBox(20, slots[0], slots[1], slots[2], spinButton);
        layout.setStyle("-fx-alignment: center; -fx-padding: 20;");

        Scene scene = new Scene(layout, 300, 400);
        primaryStage.setTitle("Slot Machine");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void spinSlots() {
        if (spinTimeline != null && spinTimeline.getStatus() == Timeline.Status.RUNNING) {
            return; // ป้องกันไม่ให้กดปุ่ม Spin ซ้ำ
        }

        spinTimeline = new Timeline(new KeyFrame(Duration.millis(100), event -> {
            for (Text slot : slots) {
                slot.setText(EMOTICONS[random.nextInt(EMOTICONS.length)]);
            }
        }));

        spinTimeline.setCycleCount(20); // หมุน 20 รอบ
        spinTimeline.setOnFinished(event -> checkWin());
        spinTimeline.play();
    }

    private void checkWin() {
        String first = slots[0].getText();
        boolean allMatch = true;

        for (Text slot : slots) {
            if (!slot.getText().equals(first)) {
                allMatch = false;
                break;
            }
        }

        if (allMatch) {
            showMessage("\uD83C\uDF89 แจ็คพอต! คุณชนะ! \uD83C\uDF89");
        } else {
            showMessage("ลองใหม่อีกครั้ง!");
        }
    }

    private void showMessage(String message) {
        Stage messageStage = new Stage();
        Text messageText = new Text(message);
        messageText.setFont(Font.font(30));

        VBox layout = new VBox(messageText);
        layout.setStyle("-fx-alignment: center; -fx-padding: 20;");

        Scene scene = new Scene(layout, 300, 200);
        messageStage.setTitle("ผลลัพธ์");
        messageStage.setScene(scene);
        messageStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

ความคิดเห็น

บทความที่ได้รับความนิยม