2016-10-25 3 views
0

このチュートリアルで説明したのと全く同じダイアログを作成したいと思います。http://code.makery.ch/blog/javafx-8-dialogs/ ダイアログは「コマンドリンクダイアログ」と呼ばれます。JavaFXコマンドリンクダイアログ

これらのダイアログはJDK 8u40の中にあるので、このチュートリアルは残念ながら欠点です。

ControlsFXを使用しないで新しいJDKを使用して同じダイアログを作成する簡単な方法はありますか?

答えて

0

最初にCommandLinkのような独自のボタングラフィックを作成します。独自のカスタムダイアログを作成し、「ボタンでそれを埋めるためにJavaFX dialogs tutorialからカスタムダイアログの例を使用すると

GridPane graphicGrid = new GridPane(); 
graphicGrid.setHgap(5); 
graphicGrid.setVgap(5); 

// Create an icon using FontAwesomeFX. 
Text icon = GlyphsDude.createIcon(FontAwesomeIcon.ARROW_RIGHT); 
icon.setFill(Color.GREEN); 

Label headerLabel = new Label("Go to the Circus"); 
headerLabel.setStyle("-fx-font-size: 1.5em;"); 

Label detailsLabel = new Label("Watch acrobats fly around and clowns, of course."); 

graphicGrid.add(icon, 0, 0); 
graphicGrid.add(headerLabel, 1, 0); 
graphicGrid.add(detailsLabel, 1, 1); 

Button button = new Button("", graphicGrid); 

Button example

:私の例では、私は、緑色の矢印アイコンを作成するために、FontAwesomeFXを使用しています作成されました。ここには完全な例があります:

import de.jensd.fx.glyphs.GlyphsDude; 
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon; 
import javafx.application.Application; 
import javafx.scene.Node; 
import javafx.scene.control.Button; 
import javafx.scene.control.ButtonType; 
import javafx.scene.control.Dialog; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 

public class MCVE extends Application { 
    @Override 
    public void start(Stage stage) { 
     Dialog dialog = new Dialog(); 

     GridPane content = new GridPane(); 
     content.setHgap(10); 
     content.setVgap(5); 

     Text headerIcon = GlyphsDude.createIcon(FontAwesomeIcon.INFO_CIRCLE, "3em"); 
     headerIcon.setFill(Color.BLUE); 

     Label headerLabel = new Label("Where do you want to go?"); 
     headerLabel.setStyle("-fx-font-size: 1.5em;"); 

     CommandLink zooButton = new CommandLink("Go to the Zoo", 
       "Here you will see zebras, monkeys, lions, elephants, and maybe also an alligator."); 
     zooButton.setOnAction(e -> dialog.close()); 

     CommandLink circusButton = new CommandLink("Go to the Circus", 
       "Watch acrobats fly around and clowns, of course."); 
     circusButton.setOnAction(e -> dialog.close()); 

     CommandLink homeButton = new CommandLink("Stay Home", "Watch TV or play some board games with your siblings."); 

     // To enable closing of the dialog. We need to add a hidden close 
     // button. See this thread: 
     // http://stackoverflow.com/questions/32048348/javafx-scene-control-dialogr-wont-close-on-pressing-x 
     dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE); 
     Node closeButton = dialog.getDialogPane().lookupButton(ButtonType.CLOSE); 
     closeButton.managedProperty().bind(closeButton.visibleProperty()); 
     closeButton.setVisible(false); 

     content.add(headerIcon, 0, 0); 
     content.add(headerLabel, 1, 0); 
     content.add(zooButton, 1, 1); 
     content.add(circusButton, 1, 2); 
     content.add(homeButton, 1, 3); 

     dialog.getDialogPane().setContent(content); 

     dialog.showAndWait(); 
    } 

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

    } 

    class CommandLink extends Button { 
     public CommandLink(String header, String text) { 
      GridPane graphicGrid = new GridPane(); 
      graphicGrid.setHgap(5); 
      graphicGrid.setVgap(5); 

      // Create an icon using FontAwesome. 
      Text icon = GlyphsDude.createIcon(FontAwesomeIcon.ARROW_RIGHT); 
      icon.setFill(Color.GREEN); 

      Label btnHeaderLabel = new Label(header); 
      btnHeaderLabel.setStyle("-fx-font-size: 1.5em;"); 

      Label detailsLabel = new Label(text); 

      graphicGrid.add(icon, 0, 0); 
      graphicGrid.add(btnHeaderLabel, 1, 0); 
      graphicGrid.add(detailsLabel, 1, 1); 

      setGraphic(graphicGrid); 
     } 
    } 
} 

そして、結果は次のとおりです。コマンドリンクダイアログのように見えるダイアログ。完璧にするためには、自分でスタイリングして遊ぶ必要があります。元の情報アイコンが必要な場合は、公式ダイアログから情報ダイアログを使用することもできます。

Dialog example full

+0

ご回答ありがとうございます。私は、それをやる方が簡単な、おそらく2行の方法があることを期待していました。私はいつも私の問題を解決するための最も簡単な方法を探していますが、あなたのようにしているようですが、私はScene Builderを使って考えています。もう一度ありがとう:) – scoozi17

関連する問題