我正在尝试使用javafx创建一个简单的数据入口应用程序,并且在添加场景控件时遇到了问题。即使在将控件添加到场景之前,显示也会丢失其填充色!只需实例化控件就会破坏它。
我在Windows上的Oracle Java 8上运行,但是我在Windows上尝试了OpenJDK 8,在Linux上尝试了OpenJDk / OpenJFX 13,它们的行为都相同。我将代码剥离给了小熊,以重现问题。
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class BasicTest extends Application {
@Override
public void start(Stage primaryStage) {
Rectangle r = new Rectangle();
r.setWidth(200);
r.setHeight(50);
r.setfill(Color.BLUE);
r.setStroke(Color.WHITE);
r.setStrokeWidth(2);
Text t = new Text();
t.setText("Confirm");
t.setfill(Color.WHITE);
t.setfont(Font.font("null",40));
StackPane sp = new StackPane(r);
sp.getchildren().add(t);
sp.setMaxWidth(200);
t.setTranslateY(-2);
Label b = new Label("Click me");//Comment this line out after first run
Scene scene = new Scene(sp,300,200);
scene.setfill(Color.BLUE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
带有标签的注释后,场景背景为蓝色,因此我得到带有白色轮廓的白色“确认”。只需添加标签构造函数,场景背景就会变灰。