JavaFX - 尝试创建我自己的自定义按钮 class
JavaFX - Trying to create my own custom button class
我正在尝试创建具有特定样式的按钮 class。
public class RoundBTN extends Button{
public RoundBTN(String name){
Button roundButton = new Button(name);
roundButton.setStyle("-fx-background-color: #20B2AA; -fx-background-radius: 15px; -fx-text-fill: #ffffff");
getChildren().add(roundButton);
}
}
然后当我转到我的应用程序时 class 尝试构建一个新按钮:
@Override
public void start(Stage stage) throws Exception {
StackPane layout = new StackPane();
layout.setPadding(new Insets(5));
layout.getChildren().add(new RoundBTN("test"));
stage.setScene(new Scene(layout,200,200));
stage.show();
}
当我 运行 程序时,我得到一个没有样式的空普通按钮。
抱歉这个菜鸟问题,但我无法让它工作。
您正在 class RoundBTN
的构造函数中创建一个新的 Button
。这根本不会改变 RoundBTN
。
在classRoundBTN
的构造函数中你需要做的第一件事是调用superclass构造函数。然后你不创建一个新的 Button
而只是设置样式。
import javafx.scene.control.Button;
public class RoundBTN extends Button {
public RoundBTN(String name){
super(name);
setStyle("-fx-background-color: #20B2AA; -fx-background-radius: 15px; -fx-text-fill: #ffffff");
}
}
但如果您只想更改样式,则无需扩展 class Button
,只需创建一个常规 Button
并设置其样式即可。
Button b = new Button("test");
b.setStyle("-fx-background-color: #20B2AA; -fx-background-radius: 15px; -fx-text-fill: #ffffff");
我正在尝试创建具有特定样式的按钮 class。
public class RoundBTN extends Button{
public RoundBTN(String name){
Button roundButton = new Button(name);
roundButton.setStyle("-fx-background-color: #20B2AA; -fx-background-radius: 15px; -fx-text-fill: #ffffff");
getChildren().add(roundButton);
}
}
然后当我转到我的应用程序时 class 尝试构建一个新按钮:
@Override
public void start(Stage stage) throws Exception {
StackPane layout = new StackPane();
layout.setPadding(new Insets(5));
layout.getChildren().add(new RoundBTN("test"));
stage.setScene(new Scene(layout,200,200));
stage.show();
}
当我 运行 程序时,我得到一个没有样式的空普通按钮。
抱歉这个菜鸟问题,但我无法让它工作。
您正在 class RoundBTN
的构造函数中创建一个新的 Button
。这根本不会改变 RoundBTN
。
在classRoundBTN
的构造函数中你需要做的第一件事是调用superclass构造函数。然后你不创建一个新的 Button
而只是设置样式。
import javafx.scene.control.Button;
public class RoundBTN extends Button {
public RoundBTN(String name){
super(name);
setStyle("-fx-background-color: #20B2AA; -fx-background-radius: 15px; -fx-text-fill: #ffffff");
}
}
但如果您只想更改样式,则无需扩展 class Button
,只需创建一个常规 Button
并设置其样式即可。
Button b = new Button("test");
b.setStyle("-fx-background-color: #20B2AA; -fx-background-radius: 15px; -fx-text-fill: #ffffff");