- OS : Fedora 17 (x86_64) / LXDE(デスクトップ環境)
- JDK : jdk-1.7.0_05-fcs.x86_64
- JavaFX : javafx-sdk2.2.0-beta (build 18)
- IDE : NetBeans IDE 7.2
サンプルソースを下記に示しました。なお、(not.png) と、(ok.png) の二種類の画像を、ソースと同じディレクトリに保存しておく必要があります。
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uibuttonsample; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.effect.DropShadow; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.stage.Stage; /** * * @author bitwalk */ public class UIButtonSample extends Application { private static final Color color = Color.web("#464646"); Button button3 = new Button("Decline"); DropShadow shadow = new DropShadow(); Label label = new Label(); public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Button Sample"); stage.setWidth(300); stage.setHeight(190); label.setFont(Font.font("Times New Roman", 22)); label.setTextFill(color); Image imageDecline = new Image(getClass().getResourceAsStream("not.png")); Image imageAccept = new Image(getClass().getResourceAsStream("ok.png")); VBox vbox = new VBox(); vbox.setLayoutX(20); vbox.setLayoutY(20); HBox hbox1 = new HBox(); HBox hbox2 = new HBox(); Button button1 = new Button("Accept", new ImageView(imageAccept)); button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;"); button1.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { label.setText("Accepted"); } }); Button button2 = new Button("Accept"); button2.setOnAction(new EventHandler () { @Override public void handle(ActionEvent e) { label.setText("Accepted"); } }); button3.setOnAction(new EventHandler () { @Override public void handle(ActionEvent e) { label.setText("Declined"); } }); button3.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler () { @Override public void handle(MouseEvent e) { button3.setEffect(shadow); } }); button3.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler () { @Override public void handle(MouseEvent e) { button3.setEffect(null); } }); hbox1.getChildren().add(button2); hbox1.getChildren().add(button3); hbox1.getChildren().add(label); hbox1.setSpacing(10); hbox1.setAlignment(Pos.BOTTOM_CENTER); Button button4 = new Button(); button4.setGraphic(new ImageView(imageAccept)); button4.setOnAction(new EventHandler () { @Override public void handle(ActionEvent e) { label.setText("Accepted"); } }); Button button5 = new Button(); button5.setGraphic(new ImageView(imageDecline)); button5.setOnAction(new EventHandler () { @Override public void handle(ActionEvent e) { label.setText("Declined"); } }); hbox2.getChildren().add(button4); hbox2.getChildren().add(button5); hbox2.setSpacing(25); vbox.getChildren().add(button1); vbox.getChildren().add(hbox1); vbox.getChildren().add(hbox2); vbox.setSpacing(10); ((Group) scene.getRoot()).getChildren().add(vbox); stage.setScene(scene); stage.show(); } }