- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个从 ToggleButton 扩展的自定义切换按钮。
问题是它不尊重给定的 ToggleGroup。我可以选择多个。但如果您设置默认类 ToogleButton
,它只允许我一次选择一个。
private void createAvatarButton(Avatar avatar) {
MPButton button = new MPButton(avatar.getPath());
button.setToggleGroup(avatarGroup);
button.setUserData(avatar);
button.setPrefHeight(100);
button.setPrefWidth(200);
button.textProperty().bind(I18N.getString(TAG.getKey(avatar.getName())));
tilePane.getChildren().add(button);
}
public class MPButton extends ToggleButton {
/**************************************************************************
*
* Private fields
*
**************************************************************************/
private String image;
/**************************************************************************
*
* Constructors
*
**************************************************************************/
/**
* Creates a new CheckComboBox instance with an empty list of choices.
*/
private MPButton() {
}
public MPButton(String image) {
initialize(image);
}
private void initialize(String image) {
this.image = image;
getStyleClass().setAll(DEFAULT_STYLE_CLASS);
setAccessibleRole(AccessibleRole.TOGGLE_BUTTON);
// alignment is styleable through css. Calling setAlignment
// makes it look to css like the user set the value and css will not
// override. Initializing alignment by calling set on the
// CssMetaData ensures that css will be able to override the value.
((StyleableProperty<Pos>)(WritableValue<Pos>)alignmentProperty()).applyStyle(null, Pos.CENTER);
setMnemonicParsing(true); // enable mnemonic auto-parsing by default
}
/***************************************************************************
* *
* Stylesheet Handling *
* *
**************************************************************************/
/** {@inheritDoc} */
@Override protected Skin<?> createDefaultSkin() {
return new MPButtonSkin(this, image);
}
/***************************************************************************
* *
* Stylesheet Handling *
* *
**************************************************************************/
private static final String DEFAULT_STYLE_CLASS = "toggle-button";
/** {@inheritDoc} */
@Override
public String getUserAgentStylesheet() {
return MPButton.class.getResource("/styles/component/c-mpbutton.css").toExternalForm();
}
}
public class MPButtonSkin extends BehaviorSkinBase<ToggleButton, BehaviorBase<ToggleButton>>{
// visuals
private final ToggleButton toggleButton;
private final StackPane stackPane;
private Blend blush;
private Glow selectGlow;
private ColorAdjust disabledAdjust, monochromeAdjust;
private ImageView iconImageView;
private Pane pane;
private static final String SYMBOL = "/img/main/symbol.png";
public MPButtonSkin(ToggleButton control, String image) {
super(control, new BehaviorBase<>(control, Collections.<KeyBinding> emptyList()));
toggleButton = new ToggleButton();
toggleButton.setText(control.getText());
toggleButton.alignmentProperty().setValue(Pos.BOTTOM_LEFT);
toggleButton.setStyle("-fx-background-color:transparent;");
stackPane = new StackPane();
toggleButton.setPadding(new Insets(0, 0, 4, 20));
pane = new Pane();
ResponsiveUtil.bindImageUrlCover(pane, getClass().getResource(image).toExternalForm());
HBox hbox = new HBox();
hbox.alignmentProperty().setValue(Pos.BOTTOM_LEFT);
Image iconImage = new Image(getClass().getResource(SYMBOL).toExternalForm());
iconImageView = new ImageView();
iconImageView.setFitHeight(15);
iconImageView.setFitWidth(15);
hbox.getChildren().add(iconImageView);
hbox.setPadding(new Insets(0, 0, 6, 0));
// Color Adjust
disabledAdjust = new ColorAdjust();
disabledAdjust.setBrightness(-0.7);
selectGlow = new Glow(2.7f);
monochromeAdjust = new ColorAdjust();
monochromeAdjust.setSaturation(-1.0);
monochromeAdjust.setBrightness(0.5);
blush = new Blend(BlendMode.MULTIPLY, monochromeAdjust,
new ColorInput(0, 0, pane.getWidth(), pane.getHeight(), Color.GOLD));
toggleButton.selectedProperty().addListener((o, ov, nv) -> {
if (nv) {
blush.setTopInput(new ColorInput(0, 0, pane.getWidth(), pane.getHeight(), Color.GOLD));
pane.setEffect(blush);
} else {
pane.setEffect(selectGlow);
}
});
toggleButton.disabledProperty().addListener((ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean nv) -> {
if (nv) {
pane.setEffect(disabledAdjust);
iconImageView.setImage(iconImage);
} else {
pane.setEffect(null);
iconImageView.setImage(null);
}
});
addWidthProperty(control);
addHeightProperty(control);
toggleButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
stackPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
pane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
stackPane.getChildren().addAll(pane, hbox, toggleButton);
stackPane.alignmentProperty().setValue(Pos.BOTTOM_LEFT);
getChildren().add(stackPane);
}
private void addWidthProperty(ToggleButton control) {
pane.prefWidthProperty().bind(control.prefWidthProperty());
stackPane.prefWidthProperty().bind(control.prefWidthProperty());
}
private void addHeightProperty(ToggleButton control) {
pane.prefHeightProperty().bind(control.prefHeightProperty());
stackPane.prefWidthProperty().bind(control.prefWidthProperty());
}
/**************************************************************************
*
* Overriding public API
*
* @return double
**************************************************************************/
@Override protected double computeMinWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
return stackPane.minWidth(height);
}
@Override protected double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
return stackPane.minHeight(width);
}
@Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
return stackPane.prefWidth(height);
}
@Override protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
return stackPane.prefHeight(width);
}
@Override protected double computeMaxWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
return getSkinnable().prefWidth(height);
}
@Override protected double computeMaxHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
return getSkinnable().prefHeight(width);
}
}
最佳答案
问题出现在 MPButtonSkin 的构造函数中。您每次都会创建一个新的 ToggleButton
:
public MPButtonSkin(ToggleButton control, String image) {
super(control, new BehaviorBase<>(control, Collections.<KeyBinding> emptyList()));
toggleButton = new ToggleButton();
toggleButton.setText(control.getText());
toggleButton.alignmentProperty().setValue(Pos.BOTTOM_LEFT);
toggleButton.setStyle("-fx-background-color:transparent;");
// ...
}
问题是新的 ToggleButton
没有引用相同的 ToggleGroup
(默认设置为 null)。您可以在构造新的 ToggleButton
后手动添加它:
// ...
toggleButton = new ToggleButton();
toggleButton.setToggleGroup(control.getToggleGroup()); // add this line
// ...
关于Javafx 扩展 ToggleButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772923/
我正在尝试自定义我的 ToggleButtons,以便在选中时以绿色表示"is",在未选中时以红色表示“否”。 我创建了以下样式,它位于我的样式资源字典中。
我是 Android 开发的新手,所以如果这是一个“菜鸟”问题,我提前表示歉意。 使用 RelativeLayout,我有一个 ID 为 reminderToggle 的 ToggleButton:
我想为 ToggleButton 创建一个动画“切换”样式(就像在智能手机上一样)。 动画本身不是问题,当我点击它时,开关看起来不错并且动画。但它也会在加载并打开时进行动画处理。 当我加载我的 Vie
我有这段代码可以以编程方式按下按钮,但我想要一个适用于 ToggleButton 的版本。 public static void PushButton(Button b) { ButtonA
我为 ToggleButton 创建了一个 ContentTemplate 和 Style,它看起来就像你从 Microsoft 的 WebMatrix 中知道的那样。 但是,并非总是可以单击,也不会
我创建了一个从 ToggleButton 扩展的自定义切换按钮。 问题是它不尊重给定的 ToggleGroup。我可以选择多个。但如果您设置默认类 ToogleButton,它只允许我一次选择一个。
我正在为一个小游戏制作一个计算器,一切正常,直到我添加了一个切换按钮。该应用程序在启动时崩溃,但我找不到任何错误。 JAVA代码: . . . int colorOff = getRes
我希望我的 ToggleButton 在检查一段时间后不被选中。我正在使用 Timer 和 TimerTask,但当我尝试执行此操作时,我的应用程序总是强制关闭。这是代码: public class
这是怎么回事,如何解决? 按下 MyToggleButton。现在看起来像是立即按下(选中)。 同时 MyToggleButton_Checked 处理程序禁用 MyToggleButton。 现在按
切换按钮上的正常背景颜色和边框颜色是什么?示例: i.toggle11.setOnAction(e->{ if(i.toggle11.isSelected()){
ToggleButton 的背景突出其底部边框。 谁能告诉我我做错了什么? 我在 SceneBuilder 中为 ToggleButton 设置了 id。 CSS: #blue-unselected
我想在 JavaFX 中创建一个切换按钮,它将执行一个循环,并且仅当用户再次单击该按钮时才会停止;在 Activity 期间,循环将不断更改 TextField 中的文本... 这是 ToggleBu
触摸 ToggleButton 时,它的背景颜色没有改变。切换确实发生了,触摸时不断变化的文本证实了这一点。但颜色始终保持白色,即未选中状态。我做错了什么? 切换按钮: 它的背景是可绘制的 togg
我使用 3 个 ToggleButtons(A、B、C)来过滤数组的内容(使用 AND 条件)并在 ListView 中显示结果。 例如如果 A 为真而 B、C 为假... ListView 将只显示
我想将 7 个切换按钮组织成金字塔形,如下所示: ---b--- --b-b-- -b---b- b-----b 其中 b 代表切换按钮,- 代表空格。我也 wnat 整个金字塔来填充它的 paren
我有一个 CustomButton 类(扩展 LinearLayout),我在其中膨胀一个包含 ToggleButton 的布局(实际上这更复杂,但我在这里简化了问题)。 public class C
是的,我可以用 2 张图片创建 ToggleButton(打开,关闭)但是我想创建一个有 3-5 张图片的 ToggleButton。 例如,它什么时候关闭,我点击: 关闭图片 中图 在图片上 它什么
我是安卓新手。我正在创建一个使用切换按钮的应用程序。我希望切换按钮在按钮处于选中状态时执行一些任务,并在按钮未选中时执行其他任务。而且我希望切换按钮即使在用户关闭应用程序(通过按下后退开关)并返回时也
我有一个这样构造的切换按钮: self.bouton = gtk.ToggleButton() self.bouton.connect("clicked", self.fonct) 在我的程序中,我正
这是我的代码 private void buttonClickone(object sender, RoutedEventArgs e) { if (WhichV == -1) {
我是一名优秀的程序员,十分优秀!