我的小应用程序包含2个按钮,每个按钮都有一个addActionListener
入口和其他东西但是使用哪个监听器?
我后来添加到事件处理程序的那个,但为什么?
以下是我的代码的一些代码:
public class LoginStarter extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start(Stage primaryStage) {
TextField usernameField = new TextField();
usernameField.setLayoutX(426);
usernameField.setLayoutY(230);
usernameField.setPromptText("Username");
usernameField.setMinSize(148, 24);
usernameField.setMaxSize(148, 24);
PasswordField passwordField = new PasswordField();
passwordField.setLayoutX(426);
passwordField.setLayoutY(260);
passwordField.setMinSize(148, 24);
passwordField.setMaxSize(148, 24);
passwordField.setPromptText("Password");
Pane gp = new Pane();
gp.getChildren().add(minimiseBtn());
gp.getChildren().add(xBtn());
gp.getChildren().add(usernameField);
gp.getChildren().add(passwordField);
gp.setId("pane");
Scene scene = new Scene(gp, 1000, 550);
scene.getStylesheets().addAll(
this.getClass().getResource("gui.css").toExternalForm());
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setTitle("Corp Commander v0.0.1");
primaryStage.setScene(scene);
primaryStage.show();
}
public Button minimiseBtn() {
Button minimiseBtn = new Button();
minimiseBtn.setText("");
minimiseBtn.setMaxSize(20, 20);
minimiseBtn.setMinSize(20, 20);
minimiseBtn.setId("minimiseBtn");
minimiseBtn.setLayoutX(956);
minimiseBtn.setLayoutY(2);
minimiseBtn.setOnAction(minimiseBtnListener());
return minimiseBtn;
}
private EventHandler<ActionEvent> minimiseBtnListener() {
EventHandler<ActionEvent> eh = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println("minimise");
}
};
return eh;
}
public Button xBtn() {
Button xBtn = new Button();
xBtn.setText("");
xBtn.setMaxSize(20, 20);
xBtn.setMinSize(20, 20);
xBtn.setId("xBtn");
xBtn.setLayoutX(978);
xBtn.setLayoutY(2);
xBtn.setOnAction(xbtnListener());
return xBtn;
}
private EventHandler<ActionEvent> xbtnListener() {
EventHandler<ActionEvent> eh = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println("exit");
System.exit(0);
}
};
return eh;
}
}
此CSS用于:
@CHARSET "ISO-8859-1";
#pane{
-fx-background-image: url("background_image.jpg");
-fx-background-repeat: stretch;
-fx-background-size: 1000 550;
-fx-background-position: center center;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
opacity: 0.95;
}
#xBtn{
-fx-background-image: url("exitBtn2.png");
-fx-background-repeat: stretch;
-fx-background-size: 20 20;
-fx-background-position: center center;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
-fx-background-color: transparent;
}
#minimiseBtn{
-fx-background-image: url("minusBtn2.png");
-fx-background-repeat: stretch;
-fx-background-size: 20 20;
-fx-background-position: center center;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
-fx-background-color: transparent;
}