2014-12-07 200 views
4

我正在構建一個非常簡單的外觀計算器,我無法弄清楚這些按鈕周圍的填充是從哪裏來的。以下是我正在構建的流動窗格:刪除JavaFX按鈕填充?

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; 
} 

當我只是把圖像中,我開始在循環創建按鈕它工作得很好,但只要流動窗格中它給了我這樣的:

Just imagine a Yosemite Calculator but with about 10-ish pixels between each button on the calculator.

我的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; 
    } 
} 
+0

哦,在做這個帖子的時候,我改變了CALC_WIDTH = 475,沒有區別,那就是按鈕的寬度,所以不是這樣。 – JimLohse 2014-12-07 02:15:57

+0

今晚沒有接受者,前往睡覺。對於它的價值,計算器應該是固定的大小,所以我不擔心調整它或這個項目的按鈕。感謝您的任何建議! – JimLohse 2014-12-07 04:27:48

回答

8

我沒有您使用的圖像的副本,所以我看不到它應該是什麼樣子。但我會嘗試一個廣場的照片。

這讓我:

This here

現在,有一個從我所看到的你描述按鍵之間沒有實際的填充。我想你可能會說的是,標籤區域本身有一些填充物會導致你的問題。這很容易解決。

Fixed version

如何固定,這是通過添加的代碼行(等等[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]); 
} 

這應該可以解決你的小問題。

編輯:只是想提及,你看到的空白的一小部分是圖像本身的一部分,而不是填充。

+0

謝謝,我在我的帖子中有這個鏈接:http://i.imgur.com/91fHMOc.jpg和填充是在下半部分,有趣的是它不會在你的版本中出現。讓我再看一眼,稍微回到這裏。 – JimLohse 2014-12-08 04:15:00

+2

你真是太棒了謝謝!!!!!用.setPadding(Insets.EMPTY)添加行就行了! – JimLohse 2014-12-08 04:33:23