- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将自定义工具栏添加到通用 TitledPane 中。我的代码似乎可以工作(下面是最小的示例),但我在布局方面遇到问题。具体来说:我使用 setGraphic() 添加工具栏,但它似乎具有固定宽度,而我希望它能够展开,这样我就可以将按钮刷新到右侧,同时将标题保持在左侧。
(我无法发布图像,所以我将恢复为 ASCII 艺术)这是下面代码的实际结果:
+------------------------------------+
| > Node 1 [a][b][c][d] |
+------------------------------------+
| > Node |
+------------------------------------+
虽然我想得到类似的东西:
+------------------------------------+
| > Node 1 [a][b][c][d] |
+------------------------------------+
| > Node |
+------------------------------------+
我可以伪造结果设置明确的 BorderPane 宽度,但它不会跟随调整大小!
<小时/>这里开始代码:
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TitledPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Test extends Application {
private Node loadMinitool(String title) {
try {
URL u = getClass().getResource("Minitool.fxml");
FXMLLoader l = new FXMLLoader(u);
Node n = (Node) l.load();
Minitool mtc = (Minitool) l.getController();
mtc.setTitle(title);
return n;
} catch (Exception e) {
System.err.println("Unable to load 'Minitool.fxml': "+e.getMessage());
}
return null;
}
public Parent createContent() {
TitledPane t1 = new TitledPane(null, new Button("Button"));
t1.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
t1.setGraphic(loadMinitool("Node 1"));
TitledPane t2 = new TitledPane("Node 2", new Text("String"));
TitledPane t3 = new TitledPane("Node 3", new Rectangle(120, 50,
Color.RED));
Accordion accordion = new Accordion();
accordion.getPanes().add(t1);
accordion.getPanes().add(t2);
accordion.getPanes().add(t3);
accordion.setMinSize(100, 100);
accordion.setPrefSize(200, 400);
return accordion;
}
@Override
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(createContent()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
关联的 FXML 是:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.*?>
<BorderPane maxWidth="1.7976931348623157E308" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.voith.hyconmde.ui.util.minitool.Minitool">
<left>
<Label fx:id="title" maxWidth="1.7976931348623157E308" text="Title goes here" BorderPane.alignment="CENTER_LEFT" />
</left>
<center>
<Pane fx:id="filler" maxWidth="1.7976931348623157E308" minWidth="0.0" prefHeight="16.0" prefWidth="200.0" />
</center>
<right>
<HBox>
<Button fx:id="add" graphicTextGap="0.0" onAction="#addAction" styleClass="btnToolbar" />
<Button fx:id="del" graphicTextGap="0.0" onAction="#delAction" styleClass="btnToolbar" />
<Button fx:id="sub" graphicTextGap="0.0" onAction="#subAction" styleClass="btnToolbar" />
<Button fx:id="dup" graphicTextGap="0.0" onAction="#dupAction" styleClass="btnToolbar" />
</HBox>
</right>
<stylesheets>
<URL value="@Minitool.css" />
</stylesheets>
</BorderPane>
Controller 是:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class Minitool {
@FXML private ResourceBundle resources;
@FXML private URL location;
@FXML private Button add;
@FXML private Button del;
@FXML private Button dup;
@FXML private Button sub;
@FXML private Label title;
@FXML void addAction(ActionEvent event) {
}
@FXML void delAction(ActionEvent event) {
}
@FXML void dupAction(ActionEvent event) {
}
@FXML void subAction(ActionEvent event) {
}
@FXML void initialize() {
assert add != null : "fx:id=\"add\" was not injected: check your FXML file 'Minitool.fxml'.";
assert del != null : "fx:id=\"del\" was not injected: check your FXML file 'Minitool.fxml'.";
assert dup != null : "fx:id=\"dup\" was not injected: check your FXML file 'Minitool.fxml'.";
assert sub != null : "fx:id=\"sub\" was not injected: check your FXML file 'Minitool.fxml'.";
assert title != null : "fx:id=\"title\" was not injected: check your FXML file 'Minitool.fxml'.";
}
public void setTitle(String title) {
this.title.setText(title);
}
public void setWidth() {
if (parent == null) {
Parent p = minitool.getParent();
if (p != null)
p = p.getParent();
if (p instanceof TitledPane) {
parent = (TitledPane) p;
parent.widthProperty().addListener(new ChangeListener<Object>() {
@Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
if (newValue instanceof Number) {
Number n = (Number) newValue;
double d = n.doubleValue();
setFiller(d);
}
}
});
setFiller(parent.getWidth());
}
}
}
protected void setFiller(double d) {
double w = d - title.getWidth() - 180; // XXX: this value has been hand-trimmed!
if (w < 0)
w = 0;
filler.setPrefWidth(w);
}
}
最佳答案
您需要做一些基本的数学计算。请对以下代码进行修改:
1)在fxml文件中向HBox添加属性:
<HBox alignment="CENTER_RIGHT" HBox.hgrow="ALWAYS">
结果是,当标题宽度有额外的空间(通过调整大小)时,该 HBox 将优先展开,并且其子项将始终向右(中心)对齐。
2)现在我们需要这样计算剩余的标题宽度
REMAINING_WIDTH = TITLE_TOTAL_WIDTH - TITLE_TEXT_WIDTH - ARROW_BUTTON_WIDTH - RIGHT_LEFT_PADDINGS
计算出的宽度将是我们的工具按钮 Pane (即 HBox)的首选宽度。
public Parent createContent() {
String titleText = "Node 1";
TitledPane t1 = new TitledPane(null, new Button("Button"));
t1.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
t1.setGraphic(loadMinitool(titleText));
double titleTextWidth = computeTextWidth(t1.getFont(), titleText, 0);
double arrowButtonWidth = 14; // I have given a static value here otherwise
// it must be calculated by
// t1.lookup(".arrow-button").getLayoutBounds().getWidth()
// after the primary stage has been shown. Namely after primaryStage.show(); line.
double paddings = 20; // right (10) and left (10) paddings defined in
// caspian.css for title of the pane. These values can also be obtained
// by lookup.
double total = titleTextWidth + arrowButtonWidth + paddings;
HBox toolButtons = (HBox) ((BorderPane) t1.getGraphic()).getRight();
toolButtons.prefWidthProperty().bind(t1.widthProperty().subtract(total));
TitledPane t2 = new TitledPane("Node 2", new Text("String"));
TitledPane t3 = new TitledPane("Node 3", new Rectangle(120, 50,
Color.RED));
Accordion accordion = new Accordion();
accordion.getPanes().add(t1);
accordion.getPanes().add(t2);
accordion.getPanes().add(t3);
accordion.setMinSize(100, 100);
accordion.setPrefSize(200, 400);
return accordion;
}
computeTextWidth()
代码取自com.sun.javafx.scene.control.skin.Utils
,仅供引用:
private double computeTextWidth(Font font, String text, double wrappingWidth) {
Text helper = new Text();
helper.setText(text);
helper.setFont(font);
// Note that the wrapping width needs to be set to zero before
// getting the text's real preferred width.
helper.setWrappingWidth(0);
double w = Math.min(helper.prefWidth(-1), wrappingWidth);
helper.setWrappingWidth((int) Math.ceil(w));
return Math.ceil(helper.getLayoutBounds().getWidth());
}
您的其他代码保持不变。
关于resize - JavaFX 2 TitledPane 图形扩展到全尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17771190/
如何将浮点 10 字节十六进制字符串(Delphi 中的扩展数据类型)转换为 C# 数据类型? 例如:00 00 00 00 00 00 00 80 ff 3f 在 Delphi 1 最佳答案 涉及同
我正在阅读 this page上面写着 If one of the strings has an encoding prefix and the other doesn't, the one that
我有以下代码(作为示例),我想对其进行调整,以使功能区扩展到整个 xrange,如 geom_hline()做。功能区指示哪些值在可接受的范围内。在我的实际应用程序中,有时没有上限或下限,因此 hli
去年,斯科特·格思里 stated “如果您想要对执行的 SQL 进行绝对控制,您实际上可以覆盖 LINQ to SQL 使用的原始 SQL”,但我找不到描述可扩展性方法的文档。 我想修改以下 LIN
我尝试在我的应用中创建一个抽屉导航。我有 3 个当前选项(查看、声明、报告),当然这 3 个 Activity 已扩展到 fragment。在View 中,它有一个next 按钮,该按钮将转到另一个页
很抱歉,如果我的问题被重复,但我无法(或不知道如何谷歌)找到答案。我想使用 rxlifecycle-navi 库来将 rxjava 绑定(bind)到生命周期,所以我需要扩展 NaviActivity
有这个html: aaa 做的时候: $('#hi1').sayHi(); 我想收到一条“嗨”提醒 有人知道这是怎么做到的吗? 最佳答案 了解更多关于jQuery plugin authoring
我有一个 UIImageView 和一个 UITextView。当点击 UITextView 时,我需要 UITextView 在 UIImageView 上展开。我不知道该怎么做。 从这里 为此 最
以下语句: URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class uclc = ucl.get
以下语句: URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class uclc = ucl.get
$的意思是什么!在 shell 或 shell 脚本中?我正在尝试理解具有以下内容的脚本。 local@usr> a=1 local@usr> echo $a 1 local@usr> echo $!
我正在 GWT 中设计一个简单的应用程序,并且在客户端有不同的 View 。每个 View 都从 SimplePanel 扩展而来,并将根据需要添加到 RootPanel。 我的第一个 View 是
我尝试在 SQL Azure 选项卡中从 S2 扩展到 S3,但 S3 似乎不可用,尽管它已被引入。另外,我还有大量可用的 DTU,即 1900 左右。是否无法扩展到 S3?我是否需要导出数据库,然后
我有一个带有 JS、html 和 css 的自定义视频播放器。我的问题的关键是我没有预料到将其从一个视频扩展到两个视频,我希望重构它,以便我可以在一个页面上播放多个视频。我尝试将所有内容重写为 for
我当前正在开发的应用程序需要可扩展的实时通信。我们一直在研究并尝试 Firebase 实时数据库和 firestore。看来Firebase实时数据库更加成熟并且经过测试,而firestore仍处于测
当 pod 被 daemonset 控制时,pod 会出现一些错误,状态会是 CrashLoopBackOff ,我想删除这些 Pod 但不想删除 DaemonSet。 所以我想把daemonset缩
我有 Jframe 表单并创建了从中绘制图形的类,为此我需要将该 void 类扩展为 jframe 表单 package grafiktest; import java.awt.Color; impo
在布局和尺寸策略方面,我从来都不是太出色,所以这可能是一个简单的答案,但就是这样。 当我向 QTabWIdget 添加新选项卡并将小部件设置为 QSplitter 时,拆分器将使用给定的全部空间量。但
我在将我的 S4 对象转换回列表时遇到了一些问题。例如,以下嵌套的 S4 类: setClass("nssItem", representation(value = "numeric
在阅读了 DDD 的一些页面后,我了解了存储库模式,然后在应用程序中成功使用了它(我真的很喜欢它);然后我注意到 EntityManager 是 generic-repository-like,但是当
我是一名优秀的程序员,十分优秀!