2016-10-25 73 views

回答

0

首先創建類似於CommandLink的自己的按鈕圖形。在我的例子我使用FontAwesomeFX創建綠色箭頭圖標:

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

然後使用自定義對話框例如,從JavaFX dialogs tutorial創建自己的自定義對話框,並用按鈕,你」填充已創建。這裏有一個完整的例子:

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

感謝您的回答。我希望有更容易的,也許是兩條線的方式來做到這一點。我一直在尋找最簡單的方法來解決我的問題,但看起來我會像你這樣做,但我認爲是場景構建器。再次感謝:) – scoozi17