【问题标题】:Javafx Button size, EmptyBorder, and Read txt File into TextAreaJavafx 按钮大小、EmptyBorder 和将 txt 文件读入 TextArea
【发布时间】:2017-07-05 05:52:31
【问题描述】:

我正在尝试切换到 javafx 而不是 swing,但尝试找到执行确切任务的方法有点困难。

  1. 我正在尝试让按钮宽度填充整个场景,并在您调整场景大小时进行相应调整。
  2. 在文本区域和按钮周围设置一个小的空白边框。
  3. 获取读取纯文本文件并替换当前文本区域(不追加)的方法。

    package gui;
    
    mport javafx.application.Application;
    mport javafx.scene.Scene;
    mport javafx.scene.control.Button;
    mport javafx.stage.Stage;
    mport javafx.scene.layout.*;
    mport javafx.scene.control.TextArea;
    
    public class Main extends Application{
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            primaryStage.setTitle("TextArea Experiment 1");
    
            TextArea textArea = new TextArea();
            // Which TextArea method would I call to set a plain 
            // text file into the text area ?      
            BorderPane border = new BorderPane();
            border.setCenter(textArea);
            //border.setBorder(new EmptyBorder(10,10,10,10));
            // Is there a method like this in JavaFx ?
            GridPane grid = new GridPane();
            border.setBottom(grid);
    
            double screensize = border.getMaxWidth();
            Button option1 = new Button("Button 1");
            Button option2 = new Button("Button 2");
            Button option3 = new Button("Button 3");
            // how can I get the buttons to be max scene size and 
            //adjust dynamically to scene dimensions ? 
            option1.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
            //option1.setPrefWidth(Double.MAX_VALUE);
            System.out.println(screensize);
            grid.add(option1, 0,1);
            grid.add(option2,0,2);
            grid.add(option3,0,3);
    
    
            Scene scene = new Scene(border, 200, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
    
        }
    
        public static void main(String[] args) {
               Application.launch(args);
        }
    }
    

【问题讨论】:

  • 你在一个线程中问了太多问题。尝试每个线程问一个问题。

标签: button javafx textarea border


【解决方案1】:

首先我建议看看这个教程:http://code.makery.ch/library/javafx-8-tutorial/ 如果你想构建一个结构良好的 javafx 应用程序,你必须创建一个 .fxml 文件、一个 Controller 类和(一些)model 类(西)。

但对于这些要点,这里有答案。

  1. 如果你想设置Region的大小你必须使用.setPrefSize(double,double)方法,如果你想设置动态你必须使用例如myButton.prefSizeProperty().bind(anyRegionYouWantToBindTo.widthProprty())

  2. 我不太明白你想要什么,我想你想使用一些样式,然后你可以写一个.css 文件然后将它arr 到textAreastyleClass

  3. 从文件中获取文本而不是使用textArea.appendText(String) 后,您必须使用textArea.setText(String)

我认为这些是解决您问题的方法,但我强烈建议您阅读有关 javafx 的教程。所以玩得开心:)

【讨论】:

    【解决方案2】:

    重写你的问题,只问这个答案的问题。然后,您可以在他们自己的线程上询问其他问题。

        GridPane grid = new GridPane();
        grid.setMaxWidth(Double.MAX_VALUE);//Make sure the GridPane MaxWidth is set to MAX_VALUE. you can use grid.gridLinesVisibleProperty().set(true); to get an idea of the GRIDPANES current borders
    
        border.setBottom(grid);
        grid.gridLinesVisibleProperty().set(true);
        Button option1 = new Button("Button 1");
        option1.setMaxWidth(Double.MAX_VALUE);//Set button one MaxWidth to MAX_VALUE
    
        Button option2 = new Button("Button 2");
        option2.setMaxWidth(Double.MAX_VALUE);//Set button two MaxWidth to MAX_VALUE
    
        Button option3 = new Button("Button 3");
        option3.setMaxWidth(Double.MAX_VALUE);//Set button three MaxWidth to MAX_VALUE
    
        //Add ColumnConstraints and set the width to 100%.
        ColumnConstraints columnConstraint = new ColumnConstraints();
        columnConstraint.setPercentWidth(100);
        grid.getColumnConstraints().add(0, columnConstraint);
    
        grid.add(option1, 0, 1);
        grid.add(option2, 0, 2);
        grid.add(option3, 0, 3);
    

    【讨论】:

    • 我个人建议Gluon SceneBuilder。我个人认为它使这样的事情变得更容易。
    猜你喜欢
    • 1970-01-01
    • 2020-02-09
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多