我必须将不透明度仅设置到消息框的外部,但是当我更改外部的不透明度时,消息框都会更改。
以下是代码:
package com.home.fxmessage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.stage.Stage;
public class MessageController {
@FXML
protected void handleExitButtonAction(ActionEvent event) {
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
}
package com.home.fxmessage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.stage.Stage;
public class MessageController {
@FXML
protected void handleExitButtonAction(ActionEvent event) {
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
}
package com.home.fxmessage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class AppLaunchController {
@FXML
protected void handleMessageOnAction(ActionEvent event) {
try {
FXMLLoader popupLoader = new FXMLLoader(getClass().getResource(
"/com/home/views/appMessage.fxml"));
Stage popupStage = popupWindow(popupLoader, event);
popupStage.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Stage popupWindow(FXMLLoader popupLoader, ActionEvent event)
throws Exception {
StackPane popupStackPane = (StackPane) popupLoader.load();
Stage popupStage = new Stage();
popupStage.initModality(Modality.WINDOW_MODAL);
popupStage.initOwner((Stage) ((Node) event.getSource()).getScene()
.getWindow());
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
popupStage.setWidth(stage.getWidth() - 14);
popupStage.setHeight(stage.getHeight() - 37);
popupStage.setX(stage.getX() + 7);
popupStage.setY(stage.getY() + 30);
popupStage.sizeToScene();
Scene popupScene = new Scene(popupStackPane);
popupScene.setFill(null);
popupStage.initStyle(StageStyle.TRANSPARENT);
popupStage.setScene(popupScene);
return popupStage;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.home.fxmessage.AppLaunchController">
<children>
<Button fx:id="btnMessage" layoutX="237.0" layoutY="175.0" mnemonicParsing="false" onAction="#handleMessageOnAction" prefHeight="25.0" prefWidth="134.0" text="Message" />
<Button layoutX="237.0" layoutY="42.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="134.0" text="Text" />
</children>
</Pane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import java.net.*?>
<StackPane styleClass="window-transparent" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.home.fxmessage.MessageController">
<children>
<Pane maxHeight="200.0" maxWidth="373.0" minHeight="142.0" minWidth="200.0" prefHeight="200.0" prefWidth="373.0" styleClass="popup-container">
<children>
<Button layoutX="351.0" mnemonicParsing="false" onAction="#handleExitButtonAction" text="X" />
<TextField layoutX="67.0" layoutY="15.0" />
<TextField layoutX="68.0" layoutY="40.0" />
<TextField layoutX="68.0" layoutY="65.0" />
<TextField layoutX="68.0" layoutY="90.0" />
<TextField layoutX="68.0" layoutY="115.0" />
</children>
</Pane>
</children>
<stylesheets>
<URL value="@style.css" />
</stylesheets>
</StackPane>
的style.css
.popup-container {
-fx-effect: dropshadow(two-pass-box, derive(#000, -30%), 4, 0.5, 0.5, 0);
-fx-background-color: #fff;
-fx-border-color:#303030;
-fx-border-width: 1;
}
.window-transparent{
-fx-background-color: #000000;
-fx-opacity:0.6;
}
我已添加&#34; -fx-opacity:1.0;&#34;到&#34; popup-container&#34;但消息框不透明度不会发生变化。那么有没有办法只设置外部不透明度?