Java 在一个hbox中提供两个元素的独立对齐
我试图在javafx中的一个HBox中获得两个元素,一个按钮和一个标签,以拥有它们自己的单独对齐方式。到目前为止,我的代码是:Java 在一个hbox中提供两个元素的独立对齐,java,javafx,alignment,hbox,borderpane,Java,Javafx,Alignment,Hbox,Borderpane,我试图在javafx中的一个HBox中获得两个元素,一个按钮和一个标签,以拥有它们自己的单独对齐方式。到目前为止,我的代码是: Button bt1= new Button("left"); bt1.setAlignment(Pos.BASELINE_LEFT); Label tst= new Label("right"); tst.setAlignment(Pos.BASELINE_RIGHT); BorderPane barLayout = new BorderPane(); HBox
Button bt1= new Button("left");
bt1.setAlignment(Pos.BASELINE_LEFT);
Label tst= new Label("right");
tst.setAlignment(Pos.BASELINE_RIGHT);
BorderPane barLayout = new BorderPane();
HBox bottomb = new HBox(20);
barLayout.setBottom(bottomb);
bottomb.getChildren().addAll(bt1, tst);
默认情况下,hbox将两个元素推到左侧,彼此相邻
borderpane布局现在对于我的项目来说是必要的,但就目前而言,有没有办法强制标签tst保持在hbox的最右侧,而bt1保持在最左侧
如果-fx样式表的东西是这样工作的,我也可以使用css。您需要将左侧节点添加到锚平面,并使锚平面水平增长
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
*
* @author Sedrick
*/
public class JavaFXApplication33 extends Application {
@Override
public void start(Stage primaryStage)
{
BorderPane bp = new BorderPane();
HBox hbox = new HBox();
bp.setBottom(hbox);
Button btnLeft = new Button("Left");
Label lblRight = new Label("Right");
AnchorPane apLeft = new AnchorPane();
HBox.setHgrow(apLeft, Priority.ALWAYS);//Make AnchorPane apLeft grow horizontally
AnchorPane apRight = new AnchorPane();
hbox.getChildren().add(apLeft);
hbox.getChildren().add(apRight);
apLeft.getChildren().add(btnLeft);
apRight.getChildren().add(lblRight);
Scene scene = new Scene(bp, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
您需要将左侧节点添加到锚链烷,并使锚链烷水平生长
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
*
* @author Sedrick
*/
public class JavaFXApplication33 extends Application {
@Override
public void start(Stage primaryStage)
{
BorderPane bp = new BorderPane();
HBox hbox = new HBox();
bp.setBottom(hbox);
Button btnLeft = new Button("Left");
Label lblRight = new Label("Right");
AnchorPane apLeft = new AnchorPane();
HBox.setHgrow(apLeft, Priority.ALWAYS);//Make AnchorPane apLeft grow horizontally
AnchorPane apRight = new AnchorPane();
hbox.getChildren().add(apLeft);
hbox.getChildren().add(apRight);
apLeft.getChildren().add(btnLeft);
apRight.getChildren().add(lblRight);
Scene scene = new Scene(bp, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
当您调用
按钮上的setAlignment()
或Label
时,请根据JavaDoc:
指定标签中的文字和图形的显示方式
当标签内有空白时对齐
所以它只是按钮
或标签
中文本的一个位置。但您需要的是将按钮
或标签
包装在某个容器中(比如HBox
),并使其填满所有可用空间(HBox.setHgrow(…,Priority.ALWAYS)
):
当您在按钮上调用setAlignment()
或标签时,请根据JavaDoc:
指定标签中的文字和图形的显示方式
当标签内有空白时对齐
所以它只是按钮
或标签
中文本的一个位置。但您需要的是将按钮
或标签
包装在某个容器中(比如HBox
),并使其填满所有可用空间(HBox.setHgrow(…,Priority.ALWAYS)
):