JavaFX
JavaFx adalah sebuah platform software untuk membangun sebuah aplikasi rich internet application (RIA) yang bisa berjalan pada berbagai macam perangkat seperti komputer desktop, web browser di Linux, Windows, macOS, dan lain-lain.
Pada Projek kali ini, saya akan memperlihatkan contoh implementasian dari JavaFx dengan membuat program FortuneTeller. Di dalam program ini, Dalam mengklik tombol yang telah disedia maka akan muncul kata-kata yang diacak.
Diagram Class nya :
Source Code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
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 java.util.Random; | |
import java.util.*; | |
import java.awt.*; | |
import javafx.application.*; | |
import javafx.event.*; | |
import javafx.geometry.*; | |
import javafx.scene.*; | |
public class FortuneTeller extends Application | |
{ | |
Text fortune = new Text(""); | |
String[] fortunes = { | |
"You will be a successfull person", | |
"You'll have a bad luck in the next 3 days", | |
"Congratulation you'll find your soulmate in the future", | |
"You will find happiness in your heart", | |
"Bad luck will happen if you being a bad person"}; | |
@Override | |
public void start(Stage stage) throws Exception | |
{ | |
VBox box = new VBox(); | |
box.setPadding(new Insets(20)); | |
box.setSpacing(20); | |
box.setAlignment(Pos.CENTER); | |
Text title = new Text("Hello Fortune Teller"); | |
title.setFont(Font.font("ARIAL", 40)); | |
box.getChildren().add(title); | |
fortune.setFont(Font.font("ARIAL", 20)); | |
box.getChildren().add(fortune); | |
Button button = new Button("Click to see your fortune"); | |
box.getChildren().add(button); | |
button.setOnAction(this::buttonClick); | |
Scene scene = new Scene(box, 500, 500); | |
stage.setTitle("Hello Fortune Teller"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
//set button click | |
private void buttonClick(ActionEvent event) | |
{ | |
//RANDOM OUTPUT | |
Random rand = new Random(); | |
fortune.setText(fortunes[rand.nextInt(fortunes.length)]); | |
} | |
} |
Output :
Tampilan awal :
Comments
Post a Comment