草庐IT

java - 删除 JavaFX 按钮填充?

coder 2024-03-27 原文

我正在构建一个看起来非常简单的计算器,但我无法弄清楚这些按钮周围的填充是从哪里来的。以下是我构建流程 Pane 的方式:

private FlowPane addFlowPaneRightSide() {

    FlowPane flow = new FlowPane();
    //flow.setPadding(new Insets(0, 0, 0, 0));
    flow.setVgap(0);
    flow.setHgap(0);
    flow.setPrefWrapLength(WIDTH_OF_CENTER / 3); // width of function buttons
    flow.setStyle("-fx-background-color: 978c87;");

    // setup arrays to hold the buttons and images for the right column
    Button operatorButtons[] = new Button[NUM_OP_BUTTONS];
    ImageView operatorImages[] = new ImageView[NUM_OP_BUTTONS];

    for (int i=0; i < NUM_OP_BUTTONS; i++) {
        operatorImages[i] = new ImageView(
                new Image(Calculator.class.getResourceAsStream(
                "images/orange-"+(i)+".png")));
        operatorButtons[i] = new Button();
        operatorButtons[i].setGraphic(operatorImages[i]);
        operatorButtons[i].setId("orange-"+(i));
        flow.getChildren().add(operatorButtons[i]);
    }

    return flow;
}

当我只是将图像放在流程 Pane 中时它工作正常但是当我开始在循环中创建按钮时它给了我这个:

我的 CSS:

/* 
    Document   : stylesheet.css
    for Calculator project in JavaFX
*/

.root{
    -fx-font-size: 14pt;
    -fx-font-family: "Tahoma";
}

.button{
    -fx-text-fill: #006464;
    -fx-skin: "com.sun.javafx.scene.control.skin.ButtonSkin";
    /*-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;*/
    -fx-background-color: transparent;
    -fx-background-insets: 0 0 0 0, 0, 0, 0;
    -fx-background-radius: 0 0 0 0, 0, 0, 0;
    -fx-border-width: 0 0 0 0, 0, 0, 0;
}

.button:focused {
    -fx-color: -fx-focused-base;
    /*-fx-background-color: -fx-focus-color, -fx-outer-border, -fx-inner-border, -fx-body-color;*/
    -fx-background-color: transparent;
    -fx-background-insets: 0 0 0 0, 0, 0, 0;
    -fx-background-radius: 0 0 0 0, 0, 0, 0;
    -fx-border-width: 0 0 0 0, 0, 0, 0;
}

最后是整个程序:

package calculator;


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text; 

/**
 *
 * @author Jim Lohse
 */
public class Calculator extends Application {

    public final int CALC_WIDTH = 500;
    public final int CALC_HEIGHT = 642;
    public final int NUM_BUTTONS = 15;
    public final int NUM_OP_BUTTONS = 5;
    public final int WIDTH_OF_CENTER = 354;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

        @Override
    public void start(Stage stage) {

// Use a border pane as the root for scene
        BorderPane border = new BorderPane();

        HBox hbox = addHBox();
        border.setTop(hbox);

        border.setRight(addFlowPaneRightSide());

        border.setCenter(addFlowPaneCenter());

        Scene scene = new Scene(border, CALC_WIDTH, CALC_HEIGHT);
        scene.getStylesheets().add("calculator/stylesheet.css");
        stage.setScene(scene);
        stage.setTitle("Calculator");
        stage.setResizable(false);
        stage.show();
    }

/*
 * Creates an HBox with two buttons for the top region
 */

    private HBox addHBox() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);   // Gap between nodes
        hbox.setStyle("-fx-background-color: #336699;");

        Button buttonCurrent = new Button("Current");
        buttonCurrent.setPrefSize(100, 20);

        Button buttonProjected = new Button("Projected");
        buttonProjected.setPrefSize(100, 20);

        hbox.getChildren().addAll(buttonCurrent, buttonProjected);

        return hbox;
    }

/*
 * Creates a horizontal flow pane with the orange operations buttons
 */
    private FlowPane addFlowPaneRightSide() {

        FlowPane flow = new FlowPane();
        //flow.setPadding(new Insets(0, 0, 0, 0));
        flow.setVgap(0);
        flow.setHgap(0);
        flow.setPrefWrapLength(WIDTH_OF_CENTER / 3); // width of function buttons

        // setup arrays to hold the buttons and images for the right column
        Button operatorButtons[] = new Button[NUM_OP_BUTTONS];
        ImageView operatorImages[] = new ImageView[NUM_OP_BUTTONS];

        for (int i=0; i < NUM_OP_BUTTONS; i++) {
            operatorImages[i] = new ImageView(
                    new Image(Calculator.class.getResourceAsStream(
                    "images/orange-"+(i)+".png")));
            operatorButtons[i] = new Button();
            operatorButtons[i].setGraphic(operatorImages[i]);
            operatorButtons[i].setId("orange-"+(i));
            flow.getChildren().add(operatorButtons[i]);
        }

        return flow;
    }

    /*
 * Creates a horizontal flow pane with the orange operations buttons
 */
    private FlowPane addFlowPaneCenter() {

        FlowPane flow = new FlowPane();
        //flow.setPadding(new Insets(0, 0, 0, 0));
        flow.setVgap(0);
        flow.setHgap(0);
        flow.setPrefWrapLength(WIDTH_OF_CENTER); // width of function buttons

        Button centerButtons[] = new Button[NUM_BUTTONS];
        ImageView centerImages[] = new ImageView[NUM_BUTTONS];
        for (int i=0; i < NUM_BUTTONS; i++) {
            centerImages[i] = new ImageView(
                    new Image(Calculator.class.getResourceAsStream(
                    "images/button-"+(i)+".png")));
            centerButtons[i] = new Button();
            centerButtons[i].setGraphic(centerImages[i]);
            centerButtons[i].setId("button-"+(i));
            flow.getChildren().add(centerButtons[i]);
        }

        return flow;
    }
}

最佳答案

我没有您正在使用的图像的副本,所以我看不到它应该是什么样子。但我会尝试使用正方形的图片。

这让我:

现在,根据您的描述,我所看到的按钮之间没有实际的填充。我认为您可能会说标签区域本身存在一些填充,这会导致您出现问题。这很容易修复。

我解决这个问题的方法是在制作按钮时向每个按钮添加一行代码 (blah[i].setPadding(Insets.EMPTY))。

for (int i=0; i < NUM_OP_BUTTONS; i++) {
    operatorImages[i] = new ImageView(
            new Image(Java.class.getResourceAsStream(
                    "art" + File.separator + "Square.png")));
    operatorButtons[i] = new Button();
    operatorButtons[i].setGraphic(operatorImages[i]);
    operatorButtons[i].setPadding(Insets.EMPTY);
    operatorButtons[i].setId("orange-"+(i));
    flow.getChildren().add(operatorButtons[i]);
}

for (int i=0; i < NUM_BUTTONS; i++) {
    centerImages[i] = new ImageView(
            new Image(Java.class.getResourceAsStream(
                    "art" + File.separator + "Square.png")));
    centerButtons[i] = new Button();
    centerButtons[i].setGraphic(centerImages[i]);
    centerButtons[i].setPadding(Insets.EMPTY);
    centerButtons[i].setId("button-"+(i));
    flow.getChildren().add(centerButtons[i]);
}

这应该可以解决您的小问题。

编辑:只是想提一下,您看到的一小部分空白是图像本身的一部分,而不是填充。

关于java - 删除 JavaFX 按钮填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338844/

有关java - 删除 JavaFX 按钮填充?的更多相关文章

  1. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  4. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  5. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  6. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  7. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  8. ruby - 如何安全地删除文件? - 2

    在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?

  9. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  10. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

随机推荐