import java.awt.Color;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
public class klik extends Application
{
public static void main(String[] args)
{
launch(args);
}
Button top,right,bottom,left;
@Override public void start(Stage primaryStage)
{
//============Buat button==========
top = new Button("TOP");
top.setPrefWidth(150);
right=new Button("RIGHT");
right.setPrefWidth(150);
bottom=new Button("BOTTOM");
bottom.setPrefWidth(150);
left=new Button("LEFT");
left.setPrefWidth(150);
//============lamda expresi==========
top.setOnAction(e -> btnrightClick());
right.setOnAction(e -> btnbottomClick());
bottom.setOnAction(e -> btnleftClick());
left.setOnAction(e -> btntopClick());
//============tata letak==========
BorderPane pane = new BorderPane();
pane.setTop(top);
pane.setBottom(bottom);
pane.setRight(right);
pane.setLeft(left);
pane.setPadding(new Insets(50, 50, 50, 50));
pane.setAlignment(top, Pos.TOP_CENTER);
pane.setAlignment(bottom, Pos.BOTTOM_CENTER);
pane.setAlignment(right, Pos.CENTER_RIGHT);
pane.setAlignment(left, Pos.CENTER_LEFT);
Scene scene = new Scene(pane, 600, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("The Click Me App");
primaryStage.show();
}
//============method==========
public void btntopClick()
{
if (top.getText() == "TOP")
{
top.setText("Change");
}
else
{
top.setText("TOP");
}
}
public void btnrightClick()
{
if (right.getText() == "RIGHT")
{
right.setText("Change");
}
else
{
right.setText("RIGHT");
}
}
public void btnbottomClick()
{
if (bottom.getText() == "BOTTOM")
{
bottom.setText("Change");
}
else
{
bottom.setText("BOTTOM");
}
}
public void btnleftClick()
{
if (left.getText() == "LEFT")
{
left.setText("Change");
}
else
{
left.setText("LEFT");
}
}
}
output:
Penjelasannya :
- code di bawah adalah untuk mengimport file javafx yang tidak disediakan di eclipse. File yang di import sesuai dengan kebutuhan dalam membuat aplikasi lewat eclipse dengan bahasa pemrograman java.
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
- Code dibawah ini adalah class klik dengan nama klik yang mengextends dari Application dan memiliki stage sebagai tempat menampilkan scane yang akan ditampilkan.
public class klik extends Application
{
public static void main(String[] args)
{
launch(args);
}
Button top,right,bottom,left;
@Override public void start(Stage primaryStage)
{
- Code berikut untuk membuat tombol button yang di buttonnya ada tulisan seperti TOP, RIGHT, BOTTOM, LEFT.
top = new Button("TOP");
top.setPrefWidth(150);
right=new Button("RIGHT");
right.setPrefWidth(150);
bottom=new Button("BOTTOM");
bottom.setPrefWidth(150);
left=new Button("LEFT");
left.setPrefWidth(150);
- Untuk code di bawah ini adalah lamda expresi dari button yang apabila button diklik akan mengacu pada method seperti method “btnrightClick()”.
top.setOnAction(e -> btnrightClick());
right.setOnAction(e -> btnbottomClick());
bottom.setOnAction(e -> btnleftClick());
left.setOnAction(e -> btntopClick());
- Code di bawah ini untuk memposisikan tombol button dengan mengatur node value nya. Dan untuk mengatur ukuran windownya juga bisa dengan scene nya diatur seperti code dibawah.
BorderPane pane = new BorderPane();
pane.setTop(top);
pane.setBottom(bottom);
pane.setRight(right);
pane.setLeft(left);
pane.setPadding(new Insets(50, 50, 50, 50));
pane.setAlignment(top, Pos.TOP_CENTER);
pane.setAlignment(bottom, Pos.BOTTOM_CENTER);
pane.setAlignment(right, Pos.CENTER_RIGHT);
pane.setAlignment(left, Pos.CENTER_LEFT);
Scene scene = new Scene(pane, 600, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("The Click Me App");
primaryStage.show();
}
- Code berikut untuk mengfungsikan tombol button ketika di klik. Jadi ketika salah satu tombol button di klik maka akan memanggil fungsi code yang sudah kita buat sebelumnya seperti TOP, RIGHT, LEFT, BOTTOM.
public void btntopClick()
{
if (top.getText() == "TOP")
{
top.setText("Change");
}
else
{
top.setText("TOP");
}
}
public void btnrightClick()
{
if (right.getText() == "RIGHT")
{
right.setText("Change");
}
else
{
right.setText("RIGHT");
}
}
public void btnbottomClick()
{
if (bottom.getText() == "BOTTOM")
{
bottom.setText("Change");
}
else
{
bottom.setText("BOTTOM");
}
}
public void btnleftClick()
{
if (left.getText() == "LEFT")
{
left.setText("Change");
}
else
{
left.setText("LEFT");
}
}
}
Terimakasih, Semoga Bermanfaat, Mohon maaf bila ada salah kata.

Tidak ada komentar:
Posting Komentar