我使用javafx场景构建器创建了接口的fxml文件。 现在我需要在界面上获得按钮位置,但是当我调用按钮方法来获取X,Y坐标时,返回的值是0.0 !! 谢谢你提前。
FXML代码
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.NewInvoceController">
<children>
<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox prefHeight="153.0" prefWidth="1161.0">
<children>
<GridPane fx:id="invoiceCustomerGrid" prefHeight="150.0" prefWidth="514.0" HBox.hgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" />
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="NEVER" />
</rowConstraints>
<children>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<Label fx:id="customerManager" onMouseClicked="#openCustomerWin" text="Ragione Sociale/Cognome Nome">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField prefHeight="26.0" prefWidth="223.0" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets />
</HBox.margin></TextField>
<Button fx:id="customerButton" mnemonicParsing="false" onAction="#openCustomerList" prefHeight="26.0" prefWidth="20.0" text="B" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
</Button>
</children>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="2">
<children>
<Label text="P.I.">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
<Label text="C.F.">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
</children>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="3">
<children>
<Label text="Indirizzo" HBox.hgrow="ALWAYS">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
</children>
</HBox>
<HBox prefHeight="71.0" prefWidth="516.0" GridPane.rowIndex="4">
<children>
<Label text="Cap">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField minWidth="-Infinity" prefHeight="26.0" prefWidth="60.0" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
<Label text="Città">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField HBox.hgrow="ALWAYS">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
</TextField>
<Label text="Provincia">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField minWidth="-Infinity" prefHeight="26.0" prefWidth="30.0">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
</children>
</HBox>
<HBox spacing="5.0">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<children>
<Label text="Fattura N°">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField prefHeight="26.0" prefWidth="58.0" HBox.hgrow="NEVER">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
</TextField>
<Label text="Data">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Label>
<DatePicker prefHeight="26.0" prefWidth="123.0" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="5.0" right="5.0" top="5.0" />
</HBox.margin>
</DatePicker>
</children>
</HBox>
</children>
</GridPane>
<Separator orientation="VERTICAL" prefHeight="200.0" />
<GridPane fx:id="invoiceMoreGrid" prefHeight="121.0" prefWidth="640.0" HBox.hgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="NEVER" />
<RowConstraints minHeight="10.0" prefHeight="140.0" valignment="CENTER" vgrow="ALWAYS" />
</rowConstraints>
<children>
<HBox alignment="TOP_CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" text="Oggetto" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Button>
<TextField HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<VBox prefHeight="150.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<HBox prefHeight="25.0" prefWidth="319.0">
<children>
<Button mnemonicParsing="false" text="Pagamento">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Button>
</children>
</HBox>
<TextArea prefHeight="90.0" prefWidth="310.0" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin></TextArea>
</children>
</VBox>
<VBox prefHeight="150.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<HBox prefHeight="25.0" prefWidth="319.0">
<children>
<Button mnemonicParsing="false" text="Note" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Button>
</children>
</HBox>
<TextArea prefHeight="90.0" prefWidth="310.0" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin></TextArea>
</children>
</VBox>
</children>
</HBox>
</children>
</GridPane>
</children>
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</HBox>
<Separator prefWidth="200.0" />
<HBox prefHeight="25.0" prefWidth="1055.0">
<children>
<Label text="Cod.">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField prefHeight="26.0" prefWidth="110.0" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
<Label text="Descr.">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
<Label text="Prz. Un.">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField prefHeight="26.0" prefWidth="95.0">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
</TextField>
<Label text="Qta.">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<TextField prefHeight="26.0" prefWidth="46.0" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TextField>
<Button mnemonicParsing="false" text="Add">
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="13.0" />
</font>
</Button>
</children>
</HBox>
<TableView prefHeight="200.0" VBox.vgrow="ALWAYS">
<columns>
<TableColumn prefWidth="115.0" text="Codice" />
<TableColumn prefWidth="697.0" text="Descrizione" />
<TableColumn prefWidth="130.0" text="Prz. Un." />
<TableColumn prefWidth="63.0" text="Qta" />
<TableColumn prefWidth="106.0" text="Tot. Imp." />
<TableColumn prefWidth="58.0" text="\% Iva" />
</columns>
</TableView>
<Separator prefWidth="200.0" />
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
....
JAVA代码
public class NewInvoceController extends Window implements Initializable,ChangeListener<Boolean> {
@FXML
Label customerManager;
@FXML
Button customerButton;
@FXML
ListView<String> listCustomer;
@FXML
VBox vobxNewInvoce;
Popup popup;
@Override
public void initialize(URL location, ResourceBundle resources) {
focusedProperty().addListener(this);
assert customerButton != null : "fx:id=\"customerButton\" was not injected: check your FXML file 'simple.fxml'.";
System.out.println("PARENT" + customerButton.getLayoutX());
}
public void openCustomerList(ActionEvent event){
CustomPopUpWindowsList popup = new CustomPopUpWindowsList(((Node)(event.getSource())).getScene().getWindow(),customerButton);
popup.newCustomPopUpWindowsList();
}
public void openCustomerWin(MouseEvent mouseEvent){
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("/GUI/fxml/customer.fxml"));
Stage stage = new Stage();
stage.setTitle("My New Stage Title");
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node)(mouseEvent.getSource())).getScene().getWindow());
stage.setResizable(false);
stage.show();
//hide this current window (if this is whant you want
//((Node)(event.getSource())).getScene().getWindow().hide();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
if (newValue)
{
System.out.println("Textfield on focus");
}
else
{
System.out.println("Textfield out focus");
}
}
}
答案 0 :(得分:2)
您好,感谢您的回复。 我用以下代码解决了它。
Button bt;
Bounds localBounds = bt.localToScene(bt.getBoundsInLocal());
double popupX = bt.getScene().getWindow().getX() + localBounds.getMaxX();
double popupY = bt.getScene().getWindow().getY() + localBounds.getMaxY();
我希望代码可以对其他人有所帮助。