我正在努力隐藏和展示标签。好像我使用hide()方法,应用程序线程关闭。因此,当我使用我的解雇功能时,我无法将可见性切换为真。我尝试了不同的东西,但无法弄清问题所在。
这是我的舞台课。
public class NotificationStage extends Application implements MessageReciever {
static Stage primaryStage;
private static Logger log = LoggerFactory.getLogger( NotificationStage.class );
@Override
public void start( final Stage primaryStage ) throws Exception {
MasterConsumer.getMasterConsumer().addMessageReciever( this );
log.info( "Notification added to MessageReciever." );
log.debug( "initiated starting Stage" );
NotificationStage.primaryStage = primaryStage;
Pane root = (Pane) FXMLLoader.load( getClass().getClassLoader().getResource( "Notification.fxml" ) );
primaryStage.setScene( new Scene( root ) );
primaryStage.initStyle( StageStyle.UNDECORATED );
primaryStage.setAlwaysOnTop( true );
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
primaryStage.setX( ( primaryScreenBounds.getMinX() + primaryScreenBounds.getWidth() ) - 394 );
primaryStage.setY( ( primaryScreenBounds.getMinY() + primaryScreenBounds.getHeight() ) - 102 );
log.debug( "stage initiliazed" );
primaryStage.show();
}
public static void dismiss() {
log.debug( "try to dismiss stage" );
if ( isTrayShowing() ) {
primaryStage.hide();
log.debug( "dismissed stage" );
}
}
public static boolean isTrayShowing() {
return NotificationStage.primaryStage.isShowing();
}
public static void show() {
log.debug( "try to show stage" );
if ( !isTrayShowing() ) {
NotificationStage.primaryStage.show();
log.debug( "showing stage" );
}}
这是我的控制器类。
public class Notification implements Initializable, MessageReciever {
private static Logger log = LoggerFactory.getLogger( Notification.class );
String infourl = "/info.png";
String warningurl = "/warning.png";
String errorurl = "/error.png";
final String HeroldStartMessage = "Herold started.";
@FXML
Rectangle rectangleColor;
@FXML
ImageView imageIcon;
@FXML
Label topicLabel;
@FXML
Label messageLabel;
@FXML
Label closeLabel;
@FXML
AnchorPane controlAnchor;
public void messageRecieved( final Message message, final Topic topic ) {
if ( message.getLevel().contains( "info" ) ) {
log.info( "message level contains info" );
Notification.this.rectangleColor.setFill( Paint.valueOf( "#1b2db5" ) );
Notification.this.imageIcon.setImage( new Image( Notification.this.infourl ) );
log.info( "Color and Image has been changed." );
} else if ( message.getLevel().contains( "warning" ) ) {
log.info( "message level contains warning" );
Notification.this.rectangleColor.setFill( Paint.valueOf( "#f8790b" ) );
Notification.this.imageIcon.setImage( new Image( Notification.this.warningurl ) );
log.info( "Color and Image has been changed." );
} else if ( message.getLevel().contains( "error" ) ) {
log.info( "message level contains error" );
Notification.this.rectangleColor.setFill( Paint.valueOf( "#ff0000" ) );
Notification.this.imageIcon.setImage( new Image( Notification.this.errorurl ) );
log.info( "Color and Image has been changed." );
} else {
//
}
log.info( "Try to change text" );
NotificationStage.show();
Platform.runLater( new Runnable() {
@Override
public void run() {
Notification.this.topicLabel.setText( message.getHeader() );
Notification.this.messageLabel.setText( message.getText() );
log.info( "text was changed" );
}
} );
}
@Override
public void initialize( final URL location, final ResourceBundle resources ) {
this.closeLabel.setOnMouseClicked( e -> NotificationStage.dismiss() );
this.rectangleColor.setFill( Paint.valueOf( "#1b2db5" ) );
this.imageIcon.setImage( new Image( this.infourl ) );
this.topicLabel.setText( "Herold" );
this.messageLabel.setText( this.HeroldStartMessage );
MasterConsumer.getMasterConsumer().addMessageReciever( this );
}
}