HBox 内的左对齐按钮(如果重要的话,在 pagnition 内)

问题描述 投票:0回答:1

我在空闲时间学习 JavaFX,并认为简单的日程应用程序将是一个很好的实践。我当前的目标是输出 ArrayList 的所有成员,并为每个成员提供按钮,允许将任务标记为已完成或编辑任务。然而,我在按钮放置方面遇到了困难,我有一个包含标签和两个按钮的 HBox,我可以让它们间隔开,但由于任务是不同大小的字符串,我无法让按钮彼此对齐。有什么办法让它们的位置基于字符串的左侧,而不是字符串后面的右侧吗?

这是我当前的代码:

@FXML
    public Node createPage(int pageIndex){
        ListView<Event> eventView = new ListView<>();
        int fromIndex = pageIndex * maxPerPage;
        int toIndex = Math.min(fromIndex + maxPerPage, schedule.size());
        List<Event> eventString = schedule.subList(fromIndex, toIndex);
        //testing buttons
        eventView.setCellFactory(param -> new ListCell<Event>(){
            @Override
            protected void updateItem(Event item, boolean empty){
                super.updateItem(item,empty);
                if(empty || item == null){
                    setText(null);
                    setGraphic(null);
                } else{
                    Label label = new Label(item.display());

                    Button editButton = new Button ("Edit");

                    Button completedButton = new Button ("Completed");

                    editButton.setOnAction(event ->{
                        System.out.println("done");
                    });
                    
                    completedButton.setOnAction(event ->{
                        System.out.println("done");
                    });
                    
                    HBox eventHbox = new HBox(label);

                    HBox buttonHbox = new HBox(editButton,completedButton);

                    buttonHbox.setSpacing(50);

                    buttonHbox.setAlignment(Pos.CENTER_RIGHT);

                    HBox.setMargin(buttonHbox,new Insets(0,0,0,200));
                    
                    HBox hbox = new HBox(eventHbox,buttonHbox);
                    
                    setGraphic(hbox);
                }
            }

        });

最终我还需要弄清楚如何将按钮对齐的元素发送到按下按钮时的函数中(如果有人有相关提示)

我尝试使用 BASELINE_RIGHT 而不是 CENTER_RIGHT,如果我不再更改 setMargin() 中的传递或者喜欢 max 或其他东西,它会让我滚动查看它

编辑:: 试图找出 TableView 但没有得到它,对于想要照片的人来说,(想要的数据是用所有相同大小的数据创建的,理想情况下按钮占位符不依赖于数据长度)

这是我目前拥有的:

这就是我想要的

java javafx scenebuilder
1个回答
0
投票

我问了一个类似的问题,更集中于一种只用一个

HBox
来做到这一点的方法。 makki saud alghamdi 的答案至少为您提供了部分可能的解决方案。该答案涉及使用 Region
 对象作为标签和按钮之间的空间,以填充空白,有效地将按钮推到右侧。

关键代码行是:

// Mind the gap between label and buttons. Region spacer = new Region ( ); HBox.setHgrow ( spacer , Priority.ALWAYS ); HBox rowBox = new HBox ( label , spacer , editButton , completedButton );
这是一个完整的示例应用程序。

package work.basil.example.exfx; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.Event; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; import java.io.IOException; import java.time.LocalDate; import java.util.List; public class HelloApplication extends Application { @Override public void start ( Stage stage ) throws IOException { record Event( String title , LocalDate when ) { } ObservableList < Event > events = FXCollections.observableList ( List.of ( new Event ( "A bit of text" , LocalDate.now ( ) ) , new Event ( "Even more text than that." , LocalDate.now ( ).plusDays ( 1 ) ) , new Event ( "Lots and lots and lots of text for a very long line." , LocalDate.now ( ).plusDays ( 2 ) ) ) ); ListView < Event > eventView = new ListView <> ( events ); eventView.setCellFactory ( param -> new ListCell < Event > ( ) { @Override protected void updateItem ( Event eventItem , boolean empty ) { super.updateItem ( eventItem , empty ); if ( empty || eventItem == null ) { setText ( null ); setGraphic ( null ); } else { Label label = new Label ( eventItem.toString ( ) ); Button editButton = new Button ( "Edit" ); Button completedButton = new Button ( "Completed" ); editButton.setOnAction ( event -> { System.out.println ( "edit" ); } ); completedButton.setOnAction ( event -> { System.out.println ( "completed" ); } ); // Mind the gap between label and buttons. Region spacer = new Region ( ); HBox.setHgrow ( spacer , Priority.ALWAYS ); HBox rowBox = new HBox ( label , spacer , editButton , completedButton ); rowBox.setAlignment ( Pos.BASELINE_LEFT ); HBox.setHgrow ( label , Priority.ALWAYS ); rowBox.setSpacing ( 7 ); setGraphic ( rowBox ); } } } ); BorderPane pane = new BorderPane ( ); pane.setCenter ( eventView ); Scene scene = new Scene ( pane , 700 , 500 ); stage.setScene ( scene ); stage.setTitle ( "Schedule Example" ); stage.show ( ); } public static void main ( String[] args ) { launch ( ); } }
还有截图。

© www.soinside.com 2019 - 2024. All rights reserved.