我有一个基本的应用程序,有点像玩具盒,除了让您在屏幕上移动形状之外,它实际上没有做任何事情,如果您双击该形状,它会将其带入前台。还有一个菜单,菜单项为“新建”、“打开”、“保存”-
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class RandCMove extends Application
{
public static void main(String [] args)
{
launch(args);
}
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Menu fileMenu = new Menu("File");
MenuItem newMenuItem = new MenuItem("New");
MenuItem openMenuItem = new MenuItem("Open");
MenuItem saveMenuItem = new MenuItem("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
fileMenu.getItems().addAll(newMenuItem,openMenuItem,saveMenuItem,
new SeparatorMenuItem(),exitMenuItem);
menuBar.getMenus().add(fileMenu);
Rectangle rect = new Rectangle();
rect.setWidth(200);
rect.setHeight(200);
rect.setArcHeight(20);
rect.setArcWidth(20);
rect.setFill(Color.RED);
rect.setX(200);
rect.setY(100);
root.setTop(menuBar);
root.getChildren().add(rect);
Circle circle = new Circle(
300,300,100);
Text text = new Text(150,150,"Text");
Font phosphate = Font.font("Phosphate",150);
text.setFont(phosphate);
text.setTranslateY(circle.getBoundsInParent().getMinY()+10);
root.getChildren().add(text);
//Positions the circle under the rectangle
circle.setTranslateY(rect.getBoundsInParent().getMinY()+30);
root.getChildren().add(circle);
// Moves shapes depending on if the cursor on the particular shape
// Brings shape to the front using double click
root.setOnMouseMoved(e ->
{
if(rect.contains(e.getX(),e.getY()))
rect.setOnMouseDragged(f ->{
rect.setX(f.getX());
rect.setY(f.getY());
});
rect.setOnMouseClicked(f ->{
if(f.getClickCount() >= 2)
rect.toFront();
});
if(circle.contains(e.getX(),e.getY()))
circle.setOnMouseDragged(f->{
circle.setCenterX(f.getX());
circle.setCenterY(f.getY());
});
circle.setOnMouseClicked(f ->{
if(f.getClickCount() >= 2)
circle.toFront();
});
if(text.contains(e.getX(),e.getY()))
text.setOnMouseDragged(f ->{
text.setX(f.getX());
text.setY(f.getY());
});
text.setOnMouseClicked(f ->{
if(f.getClickCount() >= 2)
text.toFront();
});
});
Scene scene = new Scene(root,600,600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
我希望能够保存屏幕上形状的当前状态,并按按“打开”时的方式加载它们。
我似乎找不到任何有用的东西,任何人都可以提供一些指导或指出我正确的方向吗?任何帮助将不胜感激!
最佳答案
应用程序需要偏好和配置数据来适应不同用户和环境的需求。 java.util.prefs 包为应用程序提供了一种存储和检索用户和系统首选项以及配置数据的方法。数据持久存储在依赖于实现的后备存储中。有两棵独立的首选项节点树,一棵用于用户首选项,一棵用于系统首选项。
更多信息:
http://docs.oracle.com/javase/8/docs/technotes/guides/preferences/
关于java - 将形状保存到在 javafx 中的边框 Pane 上绘制的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603090/