2015-06-08 64 views
2

我想在按钮内放置一个svg图像,然后我希望能够将按钮大小调整为任意大小。如何调整包含svg图像的按钮的大小

首先,这是多么我已经做了按钮内部的装载和SVG图像的摆放:

SVGPath svg = new SVGPath(); 
     svg.setContent("M87.5,50.002C87.5,29.293,70.712,12.5,50,12.5c-20.712,0-37.5,16.793-37.5,37.502C12.5,70.712,29.288,87.5,50,87.5 
     c6.668,0,12.918-1.756,18.342-4.809c0.61-0.22,1.049-0.799,1.049-1.486c0-0.622-0.361-1.153-0.882-1.413l0.003-0.004l-6.529-4.002 
     L61.98,75.79c-0.274-0.227-0.621-0.369-1.005-0.369c-0.238,0-0.461,0.056-0.663,0.149l-0.014-0.012 
     C57.115,76.847,53.64,77.561,50,77.561c-15.199,0-27.56-12.362-27.56-27.559c0-15.195,12.362-27.562,27.56-27.562 
     c14.322,0,26.121,10.984,27.434,24.967C77.428,57.419,73.059,63,69.631,63c-1.847,0-3.254-1.23-3.254-3.957 
     c0-0.527,0.176-1.672,0.264-2.111l4.163-19.918l-0.018,0c0.012-0.071,0.042-0.136,0.042-0.21c0-0.734-0.596-1.33-1.33-1.33h-7.23 
     c-0.657,0-1.178,0.485-1.286,1.112l-0.025-0.001l-0.737,3.549c-1.847-3.342-5.629-5.893-10.994-5.893 
     c-10.202,0-19.877,9.764-19.877,21.549c0,8.531,5.101,14.775,13.632,14.775c4.75,0,9.587-2.727,12.665-7.035l0.088,0.527 
     c0.615,3.342,9.843,7.576,15.121,7.576c7.651,0,16.617-5.156,16.617-19.932l-0.022-0.009C87.477,51.13,87.5,50.569,87.5,50.002z 
     M56.615,56.844c-1.935,2.727-5.101,5.805-9.763,5.805c-4.486,0-7.212-3.166-7.212-7.738c0-6.422,5.013-12.754,12.049-12.754 
     c3.958,0,6.245,2.551,7.124,4.486L56.615,56.844z"); 

Button button = new Button(); 
     button.setGraphic(svg); 

我试图把SVGPath组对象里面,然后我缩放该组,如下:

Group graphics = new Group(); 
graphics.getChildren().add(svg); 
graphics.getTransforms().add(new Scale(0.2, 0.2, 0 , 10)); 

Button button = new Button(); 
button.setGraphic(graphics); 

而且这确实会改变svg图像的大小。问题是按钮的大小不会相应地改变。它具有与图像从未重新缩放相同的尺寸。如果我设置按钮的最大尺寸,则无关紧要。我只能让按钮变大,但不能变小。

似乎在JavaFx中使用SVG图像的文档非常有限,所以如果任何人都可以帮助我,那将不胜感激。

更新: @ItachiUchiha指出,我可以重新调整使用setScaleX(..) and setScaleY(..)按钮,里面的SVG图像会得到它重新调整。但是,这样做似乎并未改变按钮的大小属性。这意味着包含该按钮的窗格仍将占用相同的空间,就像该按钮未被重新调整一样。并且在窗格上设置最大大小不会有帮助。另外,setScaleY和X方法重新调整所有按钮的大小,包括按钮的边框。这使得按钮与包含纯文本的按钮相比更加美观。我的窗格将包含带有纯文本内容的按钮和带有纯文本内容的按钮,并且它们都应该与其内容看起来相同。

更新2: @ ItachiUchiha的编辑回复使一切变得清晰。标记为答案。

+0

重新调整svg而不是按钮本身的任何原因? – ItachiUchiha

+0

我在问如何调整按钮本身的大小。尽管我需要调整svg图像的大小,因为我的原始图片太大了。我不能只有一半的图片显示在按钮中。但是如果有一种方法来调整按钮的大小,以便svg得到调整,那也可以。 –

回答

3

SVG图像可以重新调整大小并适合其容器。您可以重新调整按钮的大小,并且SVG图像将适合其边界。

Button button = new Button(); 
button.setGraphic(svg); 
// Re-size the button 
button.setScaleX(0.5); 
button.setScaleY(0.5); 

EDIT - 要根据父调整大小按钮。

如果您想根据父母重新调整按钮大小,并根据它缩放图像。您可以将图像的缩放属性绑定到Button的高度和宽度。每当Parent的大小发生变化时,也要更改按钮的prefSize。

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.HBox; 
import javafx.scene.shape.SVGPath; 
import javafx.stage.Stage; 

public class Main extends Application{ 

    private final int MIN_BUTTON_SIZE = 10; 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     HBox root = new HBox(); 
     root.setAlignment(Pos.CENTER); 
     SVGPath svg = new SVGPath(); 
     svg.setContent("M87.5,50.002C87.5,29.293,70.712,12.5,50,12.5c-20.712,0-37.5,16.793-37.5,37.502C12.5,70.712,29.288,87.5,50,87.5" + 
       "c6.668,0,12.918-1.756,18.342-4.809c0.61-0.22,1.049-0.799,1.049-1.486c0-0.622-0.361-1.153-0.882-1.413l0.003-0.004l-6.529-4.002" + 
     "L61.98,75.79c-0.274-0.227-0.621-0.369-1.005-0.369c-0.238,0-0.461,0.056-0.663,0.149l-0.014-0.012" + 
     "C57.115,76.847,53.64,77.561,50,77.561c-15.199,0-27.56-12.362-27.56-27.559c0-15.195,12.362-27.562,27.56-27.562" + 
     "c14.322,0,26.121,10.984,27.434,24.967C77.428,57.419,73.059,63,69.631,63c-1.847,0-3.254-1.23-3.254-3.957" + 
     "c0-0.527,0.176-1.672,0.264-2.111l4.163-19.918l-0.018,0c0.012-0.071,0.042-0.136,0.042-0.21c0-0.734-0.596-1.33-1.33-1.33h-7.23" + 
     "c-0.657,0-1.178,0.485-1.286,1.112l-0.025-0.001l-0.737,3.549c-1.847-3.342-5.629-5.893-10.994-5.893" + 
     "c-10.202,0-19.877,9.764-19.877,21.549c0,8.531,5.101,14.775,13.632,14.775c4.75,0,9.587-2.727,12.665-7.035l0.088,0.527" + 
     "c0.615,3.342,9.843,7.576,15.121,7.576c7.651,0,16.617-5.156,16.617-19.932l-0.022-0.009C87.477,51.13,87.5,50.569,87.5,50.002z" + 
     "M56.615,56.844c-1.935,2.727-5.101,5.805-9.763,5.805c-4.486,0-7.212-3.166-7.212-7.738c0-6.422,5.013-12.754,12.049-12.754" + 
     "c3.958,0,6.245,2.551,7.124,4.486L56.615,56.844z"); 

     Button buttonWithGraphics = new Button(); 
     buttonWithGraphics.setGraphic(svg); 

     // Bind the Image scale property to the buttons size 
     svg.scaleXProperty().bind(buttonWithGraphics.widthProperty().divide(100)); 
     svg.scaleYProperty().bind(buttonWithGraphics.heightProperty().divide(100)); 

     // Declare a minimum size for the button 
     buttonWithGraphics.setMinSize(MIN_BUTTON_SIZE, MIN_BUTTON_SIZE); 

     root.getChildren().addAll(buttonWithGraphics); 
     root.layoutBoundsProperty().addListener((observableValue, oldBounds, newBounds) -> { 
       double size = Math.max(MIN_BUTTON_SIZE, Math.min(newBounds.getWidth(), newBounds.getHeight())); 
       buttonWithGraphics.setPrefSize(size, size); 
      } 
     ); 

     Scene scene = new Scene(root); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 

中央图像是默认大小,所以此工程上减少以及增加按钮的大小。

enter image description here

注: - 您可以计算和使用不同的按钮高度和宽度值。

+0

谢谢!我只有两个问题:1:为什么'button.setPrefSize(..)'工作,如果我删除'button.setMinSize(..)'?我的意思是,'button.setMinSize(..)在设置'button.setPrefSize(..)时不会做任何事情,按钮可以占用尽可能多的空间,所以它为什么重要? 2:如果我想在保持图像比例的同时在矩形按钮内显示一个正方形svg图像,是否有任何方法可以实现此功能?因为在这个实现中,图像被拉伸。 –

+0

1.除非在外部指定,否则对于按钮,其最小尺寸是其首选尺寸。所以,我们需要定义'button.setMinSize(..)'来使按钮变得比它的正常尺寸小。 2.是的,你可以保持图像的缩放比例与按钮的大小成比例。 – ItachiUchiha

+0

查看我的上次更新:如果我使用此新图像,为什么不能这样工作?图片不会根据其“Button”调整大小。 –