JavaFX关闭阶段不起作用

时间:2014-10-23 14:19:21

标签: java javafx

package com.gambling.client;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import org.controlsfx.dialog.Dialogs;

import com.gambling.client.over100.Over100;
import com.gambling.client.x2.X2;
import com.gambling.utility.Images;

@SuppressWarnings("deprecation")
public class GamblingClientController {

@FXML
private Button firstButton;

@FXML
private Button secondButton;

@FXML
private TextArea area;

@FXML
private Button thirdButton;

@FXML
private Text text;

public boolean isClicked;


public boolean garField;

@FXML
protected void onFirstButtion() {
    text.setText(null);
    new X2().start(new Stage());
    isClicked = false;
}

@FXML
protected void onSecondButton() {
    text.setText(null);
    new Over100().start(new Stage());
    isClicked = false;
}
@FXML
protected void textArea() {
    Stage garfieldStage = new Stage();
    String areaText = area.getText();
    if(areaText.contains("@red@")) {
        firstButton.setStyle("-fx-background-color: red;");
        secondButton.setStyle("-fx-background-color: red;");
        thirdButton.setStyle("-fx-background-color: red;");
    }
    if(areaText.contains("@blu@") || areaText.contains("@blue@")) {
        firstButton.setStyle("-fx-background-color: blue;");
        secondButton.setStyle("-fx-background-color: blue;");
        thirdButton.setStyle("-fx-background-color: blue;");
    }
    if(areaText.contains("@purple@") || areaText.contains("@pup@")) {
        firstButton.setStyle("-fx-background-color: purple;");
        secondButton.setStyle("-fx-background-color: purple;");
        thirdButton.setStyle("-fx-background-color: purple;");
    }
    if(areaText.contains("@gre@") || areaText.contains("@green@")) {
        firstButton.setStyle("-fx-background-color: green;");
        secondButton.setStyle("-fx-background-color: green;");
        thirdButton.setStyle("-fx-background-color: green;");
    }
    if(areaText.contains("@normal@") || areaText.contains("@nor@")) {
        firstButton.setStyle("-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;");
        secondButton.setStyle("-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;");
        thirdButton.setStyle("-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;");
    }
    if(areaText.contains("@black@") || areaText.contains("@bla@")) {
        firstButton.setStyle("-fx-background-color: black;");
        secondButton.setStyle("-fx-background-color: black;");
        thirdButton.setStyle("-fx-background-color: black;");
    }
    if(areaText.contains("@white@") || areaText.contains("@whi@")) {
        firstButton.setStyle("-fx-background-color: white;");
        secondButton.setStyle("-fx-background-color: white;");
        thirdButton.setStyle("-fx-background-color: white;");
    }
    if(areaText.contains("@pink@") || areaText.contains("@pi@") || areaText.contains("@pin@")) {
        firstButton.setStyle("-fx-background-color: pink;");
        secondButton.setStyle("-fx-background-color: pink;");
        thirdButton.setStyle("-fx-background-color: pink;");
    }
    if(areaText.contains("@org@") || areaText.contains("@orange@")) {
        firstButton.setStyle("-fx-background-color: orange;");
        secondButton.setStyle("-fx-background-color: orange;");
        thirdButton.setStyle("-fx-background-color: orange;");
    }
    if(areaText.contains("@textNull@") || areaText.contains("@textOff@")) {
        isClicked = false;
        text.setText(null);
    }
    if(areaText.contains("@hax@")) {
        text.setText("Don't use hax!! :(");
    }
    if(areaText.contains("@:)@") && !garField || areaText.contains("@garfield@") && !garField) {
        Images images = new Images(new Image("http://goo.gl/YsuGV"), Color.TRANSPARENT, StageStyle.TRANSPARENT, 300, 250);
        images.start(garfieldStage);
        garField = true;
    }
    if(areaText.contains("@garfieldOff@") || areaText.contains("@garfieldNull@")) {
        garField = false;
        garfieldStage.close();
        System.out.println("Closed");
    }
}

@FXML
protected void onThirdButton() {
    text.setText("More coming soon");
    if(!isClicked) {
        isClicked = true;
        return;
    }
    if(isClicked) {
        isClicked = false;
        text.setText(null);
        return;
    }
}
@FXML
protected void onMenuItem() {
    Dialogs.create().title("Info").message("You can type @color@ to change the color of buttons or you can use @normal@ to change everything back. And there are secret commands :). You use them by doing this @command@").showInformation();
}
}

我正在使用这个atm。但我有这个问题

if(areaText.contains("@:)@") && !garField || areaText.contains("@garfield@") && !garField) {
        Images images = new Images(new Image("http://goo.gl/YsuGV"), Color.TRANSPARENT, StageStyle.TRANSPARENT, 300, 250);
        images.start(garfieldStage);
        garField = true;
    }
    if(areaText.contains("@garfieldOff@") || areaText.contains("@garfieldNull@")) {
        garField = false;
        garfieldStage.close();
        System.out.println("Closed");
    }

它没有关闭,但它确实打印出来,所以.close()会被调用。

当我将garfieldStage置于方法之外并将其设为私有或公共或受保护或没有修饰符时,当我尝试重新启动时,它会给我这个错误

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3931)
    at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3877)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2006)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2468)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:197)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:147)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:227)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:544)
    at com.sun.glass.ui.View.notifyKey(View.java:954)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
    ... 28 more
Caused by: java.lang.IllegalStateException: Cannot set style once stage has been set visible
    at javafx.stage.Stage.initStyle(Stage.java:481)
    at com.gambling.utility.Images.start(Images.java:58)
    at com.gambling.client.GamblingClientController.textArea(GamblingClientController.java:119)
    ... 37 more

但它确实关闭了。

1 个答案:

答案 0 :(得分:0)

创建舞台并在initialize()方法中初始化其样式,然后只引用处理程序方法中的现有(和初始化)阶段:

公共类GamblingClientController {

private Stage garfieldStage ;

// ...

public void initialize() {
    garfieldStage = new Stage();
    garfieldStage.initStyle(...);
}

// ...

}

然后,在您尝试时,从处理程序方法中删除Stage garfieldStage = new Stage();以及Stage.initStyle(...)删除Images.start()来电。如果您在舞台上调用任何其他init...(...)方法,请将其移至控制器的initialize()方法。