2013-03-15 17 views
1

在下面的代碼中,我有一個工具欄,並且我添加了各種大小的按鈕。我想要的按鈕是正方形和所有相同的大小。因此,基本上找到所有按鈕的最長寬度或高度,並將所有其他按鈕寬度和高度設置爲此大小。然而,按鈕可以改變大小,所以我想要一個綁定。我無法弄清楚 - 任何人都知道如何做到這一點?在JavaFX中設置工具欄按鈕方形和所有相同的大小2

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ToolBar; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ToolBarButtonTest extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 
     BorderPane borderPane = new BorderPane(); 
     Scene scene = new Scene(borderPane, 500, 500); 

     ToolBar toolBar = new ToolBar(); 

     Button button1 = new Button("s"); 
     Button button2 = new Button("ss"); 
     Button button3 = new Button("sss"); 

     toolBar.getItems().addAll(button1, button2, button3); 

     borderPane.setTop(toolBar); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

謝謝,尼克。

回答

2

在這裏,你走了。此示例將使用CSS將按鈕形狀轉換爲方形,並安裝驗證偵聽器來跟蹤對按鈕的佈局更改並相應地更新寬度。

public class SquareButtons extends Application { 
@Override 
    public void start(final Stage stage) throws Exception { 
    /* Assume these are your toolbar elements */ 
    final Button[] buttons = new Button[]{ 
      new Button("Short"), 
      new Button("Slightly longer"), 
      new Button("Very very very long button") 
    }; 

    /* This would, of course, belong in a style sheet - it turns the buttons square */ 
    for (Button b : buttons) 
     b.setStyle("-fx-background-radius: 0"); 

    /* This will set the pref width/height of all your buttons to the maximum of the pref width/height of the larget one */ 
    final InvalidationListener listener = new InvalidationListener() { 
     public void invalidated(final Observable observable) { 
      double size = 0; 
      for (Button b : buttons) { 
       size = Math.max(size, b.prefWidth(Integer.MAX_VALUE)); 
       size = Math.max(size, b.prefHeight(Integer.MAX_VALUE)); 
      } 

      for (Button b : buttons) { 
       b.setPrefWidth(size); 
       b.setPrefHeight(size); 
      } 
     } 
    }; 

    for (Button b : buttons) 
     b.widthProperty().addListener(listener); 

    final ToolBar toolbar = new ToolBar(); 
    toolbar.getItems().addAll(buttons); 

    final Scene scene = new Scene(toolbar); 
    stage.setScene(scene); 
    stage.setWidth(800); 
    stage.setHeight(200); 
    stage.show(); 
    } 

    public static void main(String[] args) { 
    Application.launch(args); 
    } 
} 

更新:沒有完全閱讀的問題,看到評論。

+1

我認爲OP的意思是「方形」,因爲按鈕的高度和寬度都是相同的。如果您在InvalidationListener中設置寬度的相同位置添加了「b.setPrefHeight(width)」,那麼您的示例將更接近意圖。我不認爲他打算讓按鈕的角落變成方形。 – clartaq 2013-03-17 03:55:40

+0

你當然是對的 - 我的不好。我已經更新了相應的答案,儘管方形文本按鈕只是醜陋的imho。 – sarcan 2013-03-17 10:01:22

+0

我的意思是平方,因爲在寬度和高度是相同的大小。我希望這可以通過綁定api來實現,但這是完美的,所以非常感謝答案。 – Boomah 2013-03-17 17:13:03

0

由於沒有人回覆,我將傳遞一個部分解決方案。我是JavaFX的noob,但由於父母負責佈置子節點,所以我將派生一個ToolBar類的修改版本並覆蓋一些佈局代碼。以下是對您的示例進行的修改,但有點不便。

package toolbarbuttontest; 

import javafx.application.Application; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ToolBar; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ToolBarButtonTest extends Application { 

    @Override 
    public void start(Stage stage) { 
     BorderPane borderPane = new BorderPane(); 
     Scene scene = new Scene(borderPane, 500, 500); 

     Button btn1 = new Button("short"); 
     Button btn2 = new Button("between"); 
     Button btn3 = new Button("pretty long"); 

     SquareButtonToolBar toolBar = new SquareButtonToolBar(); 
     toolBar.getItems().addAll(btn1, btn2, btn3); 

     borderPane.setTop(toolBar); 

     stage.setScene(scene); 
     stage.show(); 
     toolBar.requestLayout(); 
    } 

    // A derivative of the ToolBar class that will resize all buttons to be 
    // the same size (based on the length of the longest button label) and 
    // square. 
    class SquareButtonToolBar extends ToolBar { 

     @Override 
     protected void layoutChildren() { 
      double minPrefSize = calculatePrefChildSize(); 
      for (Node n : getItems()) { 
       if (n instanceof Button) { 
        ((Button) n).setPrefWidth(minPrefSize); 
        ((Button) n).setPrefHeight(minPrefSize); 
       } 
      } 
      super.layoutChildren(); 
     } 

     private double calculatePrefChildSize() { 
      double minPrefSize = 0.0d; 
      for (Node n : getItems()) { 
       if (n instanceof Button) { 
        minPrefSize = Math.max(minPrefSize, n.prefWidth(-1)); 
       } 
      } 
      return minPrefSize; 
     } 

    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

SquareButtonToolBar類通過按鈕的首選寬度和高度設置爲所述最長按鈕的長度,從而使所有的按鈕平方和相同的尺寸將覆蓋佈局代碼。 kludge是start方法底部toolBar.requestLayout()的調用。如果沒有這個調用,即使在程序啓動時按鈕的寬度都是按照需要顯示的,在窗口重新調整大小之前,高度不能正確顯示。不知道我在想什麼。如果您找到答案,請發佈更新。

更新:修改爲一個完整的工作,但與一個討厭的kludge的例子。

相關問題