2016-03-01 47 views
0

我该如何制作适用于每个按钮的句柄,因此我不必为每个按钮制作几乎相同的句柄?对1个控制器句柄的多个FXML变量

所以我有9个接触FXML文件,每一个看起来是这样的:

<Button fx:id="id1" mnemonicParsing="false" onAction="#handleContactEmailButtonAction" prefHeight="60.0" prefWidth="380.0" style="-fx-background-color: white;"> 
    <graphic> 
     <VBox style="-fx-background-color: white; -fx-border-color: white;"> 
      <children> 
       <Label prefHeight="30.0" prefWidth="334.0" text="Jane Doe" /> 
       <Label prefHeight="17.0" prefWidth="349.0" text="[email protected]" textAlignment="CENTER" /> 
      </children> 
     </VBox> 
    </graphic> 
</Button> 

这是控制器:

@FXML 
private Button id1; 
@FXML 
private Button id2; 
@FXML 
private Button id3; 
@FXML 
private Button id4; 
@FXML 
private Button id5; 
@FXML 
private Button id6; 
@FXML 
private Button id7; 
@FXML 
private Button id8; 
@FXML 
private Button id9; 


@FXML 
protected void handleContactEmailButtonAction(ActionEvent event) { 

    try { 
     Desktop desktop = Desktop.getDesktop(); 
     String message = "mailto:"+emailvariable+"?subject=Music%20Bookings"; 
     URI uri = URI.create(message); 
     desktop.mail(uri); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

因此,如果按钮与FX:ID = “ID1”被点击,然后电子邮件变量变为相关标签中的电子邮件,并且全部使用一个控制器?

任何和所有的帮助表示赞赏,谢谢!

回答

0

创建一个地图,您可以从事件源获取的按钮,如:

@FXML 
private Button id1; 
@FXML 
private Button id2; 
@FXML 
private Button id3; 
@FXML 
private Button id4; 
@FXML 
private Button id5; 
@FXML 
private Button id6; 
@FXML 
private Button id7; 
@FXML 
private Button id8; 
@FXML 
private Button id9; 

private Map<Button, String> emailVariables; 

@Override 
public void initialize(URL location, ResourceBundle resources) 
{ 
    emailVariables = new HashMap<Button, String>(); 
    emailVariables.put(id1, "[email protected]"); 
    emailVariables.put(id2, "[email protected]"); 
    emailVariables.put(id3, "[email protected]"); 
    emailVariables.put(id4, "[email protected]"); 
    emailVariables.put(id5, "[email protected]"); 
    emailVariables.put(id6, "[email protected]"); 
    emailVariables.put(id7, "[email protected]"); 
    emailVariables.put(id8, "[email protected]"); 
    emailVariables.put(id9, "[email protected]"); 
} 

@FXML 
protected void handleContactEmailButtonAction(ActionEvent event) 
{ 

try 
{ 
    Desktop desktop = Desktop.getDesktop(); 
    String message = "mailto:" + emailvariables.get((Button) event.getSource()) + "?subject=Music%20Bookings"; 
    URI uri = URI.create(message); 
    desktop.mail(uri); 

} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 
} 

代码没有进行测试。

0

考虑创建一个自定义组件:

package application; 

import java.awt.Desktop; 
import java.io.IOException; 
import java.net.URI; 

import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 

public class EmailButton extends Button { 

    @FXML 
    private Label nameLabel ; 
    @FXML 
    private Label addressLabel ; 

    private final StringProperty name = new SimpleStringProperty(); 
    private final StringProperty emailAddress = new SimpleStringProperty(); 

    public EmailButton() throws IOException { 

     FXMLLoader loader = new FXMLLoader(getClass().getResource("emailButton.fxml")); 
     loader.setRoot(this); 
     loader.setController(this); 
     loader.load(); 
    } 

    public void initialize() { 
     nameLabel.textProperty().bind(name); 
     addressLabel.textProperty().bind(emailAddress); 
    } 

    @FXML 
    private void sendEmail() { 
     try { 
      Desktop desktop = Desktop.getDesktop(); 
      String message = "mailto:"+getEmailAddress()+"?subject=Music%20Bookings"; 
      URI uri = URI.create(message); 
      desktop.mail(uri); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public StringProperty nameProperty() { 
     return name ; 
    } 

    public final String getName() { 
     return nameProperty().get(); 
    } 

    public final void setName(String name) { 
     nameProperty().set(name); 
    } 

    public StringProperty emailAddressProperty() { 
     return emailAddress ; 
    } 

    public final String getEmailAddress() { 
     return emailAddressProperty().get(); 
    } 

    public final void setEmailAddress(String emailAddress) { 
     emailAddressProperty().set(emailAddress); 
    } 
} 

和emailButton.fxml:在您的主FXML

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 


<fx:root xmlns:fx="http://javafx.com/fxml/1" type="Button" mnemonicParsing="false" onAction="#sendEmail" prefHeight="60.0" prefWidth="380.0" style="-fx-background-color: white;"> 

    <graphic> 
     <VBox style="-fx-background-color: white; -fx-border-color: white;"> 
      <children> 
       <Label fx:id="nameLabel" prefHeight="30.0" prefWidth="334.0" /> 
       <Label fx:id="addressLabel" prefHeight="17.0" prefWidth="349.0" textAlignment="CENTER" /> 
      </children> 
     </VBox> 
    </graphic> 
</fx:root> 

现在,你可以这样做:

<?import application.EmailButton?> 

<!-- ... --> 

<EmailButton name="Jane Doe" emailAddress="[email protected]"/> 
<EmailButton name="..." emailAddress="..." /> 
<EmailButton name="..." emailAddress="..."/>