我有一个SVG文件,我正在尝试使用javafx将其集成到按钮中,但我成功了,但是它保持了很大的尺寸,而乞求该文件是这样的
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M8 256c0 137 111 248 248 248s248-111 248-248S393 8 256 8 8 119 8 256zM256 40c118.7 0 216 96.1 216 216 0 118.7-96.1 216-216 216-118.7 0-216-96.1-216-216 0-118.7 96.1-216 216-216zm-32 88v64H120c-13.2 0-24 10.8-24 24v80c0 13.2 10.8 24 24 24h104v64c0 28.4 34.5 42.8 54.6 22.6l128-128c12.5-12.5 12.5-32.8 0-45.3l-128-128c-20.1-20-54.6-5.8-54.6 22.7zm160 128L256 384v-96H128v-64h128v-96l128 128z"/></svg>
所以我认为必须更改其比例,因此我添加了特定的宽度和高度
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="150px" height="150px">...
我使用了git中的SVGLoader,并编写了这段代码以查看结果
package tutoFX;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import net.javainthebox.caraibe.svg.SVGContent;
import net.javainthebox.caraibe.svg.SVGLoader;
public class SVGLoaderSample extends Application {
@Override
public void start(Stage stage) {
SVGContent content = SVGLoader.load(getClass().getResource("arrow-alt-circle-right10.svg").toString());
// create a button and set the graphics node
Button button = new Button();
button.setGraphic(content);
// add the button to the scene and show the scene
HBox layout = new HBox(button);
HBox.setMargin(button, new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.setTitle("SVGLoader Sample");
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
enter image description here
但并不像预期的那样,你们中有些人可能会说使用
// Scale the image and wrap it in a Group to make the button
// properly scale to the size of the image
content.setScaleX(0.1);
content.setScaleY(0.1);
是的,它可以更改图标的大小,但可以使节点更大
enter image description here
感谢帮助
最佳答案
我建议您使用import javafx.scene.Group;
package tutoFX;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import net.javainthebox.caraibe.svg.SVGContent;
import net.javainthebox.caraibe.svg.SVGLoader;
public class SVGLoaderSample extends Application {
@Override
public void start(Stage stage) {
SVGContent content = SVGLoader.load(getClass().getResource("arrow-alt-circle-right10.svg").toString());
// Scale the image and wrap it in a Group to make the button
// properly scale to the size of the image
content.setScaleX(0.1);
content.setScaleY(0.1);
Group graphic = new Group(content);
// create a button and set the graphics node
Button button = new Button();
button.setGraphic(graphic);
// add the button to the scene and show the scene
HBox layout = new HBox(button);
HBox.setMargin(button, new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.setTitle("SVGLoader Sample");
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}