作者:半暖半夏半流年 | 来源:互联网 | 2023-08-30 14:43
准备写一个简单的进销存软件,记录一下遇到的问题的和每天的工作。 初步的想法是用Spring Boot搭建后端,MyBatis进行数据库操作;软件运行在Windows上,使用JavaFx来进行图形界面编程,如果可行的话,使用些好看的开源控件库美化一下。Maven做依赖管理。开发工具使用idea,数据库直接建在我的云服务器上部署好的mysql上,远程连接进行操作。 有时间的话再加上会接着写安卓端、小程序端或者网页版。网页版可能性比较大,因为想看看若依框架
1
需求分析,建数据库。
2. JavaFx
2.0 项目结构
2.1 依赖
关于Javafx的各种讲解和介绍很多,看了很多有了大致了解,这里使用maven管理该项目,新建项目后,加入相关依赖主要是openjfx的依赖,其他的可以根据自己的需要进行添加,我这里主要加了okhttp的依赖进行网络请求,需要注意的是okhttp中需要剔除安卓部分。
org.openjfx
javafx-controls
13.0.2
org.openjfx
javafx-fxml
13.0.2
com.squareup.okhttp3
okhttp
4.7.2
compile
com.google.android
android
2.2 启动类
作为进销存系统,需要登陆界面和操作界面,所以将启动类进行了一些修改,start()和main()函数是保持不变的,将sence设置为静态数据,同时将FXMLLoader独立出来作为一个单独的函数。并另设一个setRoot函数,进行界面的切换。具体代码如下
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
Parent root = loadFXML("primary");
//初始化界面
scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("进销存系统");
stage.setResizable(false);//设置窗体不可改变大小
stage.show();
}
static void setRoot(String fxml) throws IOException {
Parent root = loadFXML(fxml);
if(fxml.equals("secondary")){
//转换到第二界面是可以进行一些其他操作
}
scene.setRoot(root);
}
public static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
}
2.3控件的使用
可以使用scene builder进行可视化的界面编写 使用主标签中使用fx:controller设置界面对应的控制器
2.3.1 button
在fxml文件中加入一个button标签,设置点击时对应调用的函数
在org.example.SecondaryController中具体实现addtoChuhuoList函数
2.3.2 choiceBox
choiceBox的设置操作和button类似,不同的是他作为下拉列表需要进行数据装配。这里装载到下拉列表中的全部是String。
private ChoiceBox choiceBox;//控件定义,对应fxml文件中设置的id
String[] s = new String[size];//定义字符串数组
//自行装载字符串数据
choiceBox.getItems().setAll(s);//完成装载,便可以正常显示
//需要更改下拉选项时,改变s中的值即可;
2.3.3 textField
private TextField t;
//获取文本框中输入的内容
String text = t.getValue().toString();
//清空
t.setText("");
2.3.4 tableView
fxml中控件布局的写法
每一个列和表格都有他的id,在具体设置时会用到。 这里使用临时类进行表格数据的储存和装载。
//定义数据列表
public static ObservableList chuhuo_liebiao_data = FXCollections.observableArrayList();
//在合适的地方定义对应实体类
public class linshiHuowu{
private final SimpleStringProperty tiaoma;
private final SimpleStringProperty changjia;
private final SimpleStringProperty xilie;
private final SimpleStringProperty pinming;
private final SimpleStringProperty xinghao;
private final SimpleIntegerProperty guige;
private final SimpleIntegerProperty shuliang;
private final SimpleIntegerProperty jiage;
}
//得到的数据新建linshiHuowu对象,并加入到数据列表中
linshiHuowu l = new linshiHuowu();
chuhuo_liebiao_data.add(l);
//数据装配
chuhuo_liebiao_tiaoma.setCellValueFactory(new PropertyValueFactory("tiaoma"));//括号中字段对应实体类linshiHuowu中的属性
chuhuo_liebiao_changjia.setCellValueFactory(new PropertyValueFactory("changjia"));
chuhuo_liebiao_xilie.setCellValueFactory(new PropertyValueFactory("xilie"));
chuhuo_liebiao_pinming.setCellValueFactory(new PropertyValueFactory("pinming"));
chuhuo_liebiao_xinghao.setCellValueFactory(new PropertyValueFactory("xinghao"));
chuhuo_liebiao_guige.setCellValueFactory(new PropertyValueFactory("guige"));
chuhuo_liebiao_jiage.setCellValueFactory(new PropertyValueFactory("jiage"));
chuhuo_liebiao_shuliang.setCellValueFactory(new PropertyValueFactory("shuliang"));
//设置删除按钮
chuhuo_liebiao_shanchu.setCellFactory((col)->{
TableCell cell = new TableCell<>(){
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
this.setText(null);
this.setGraphic(null);
if(!empty){
Button shanchuButton = new Button("删除");
this.setGraphic(shanchuButton);
shanchuButton.setOnMouseClicked((me)->{
linshiHuowu shanchuH = this.getTableView().getItems().get(this.getIndex());
//从数据列表中删除该数据
this.getTableView().getItems().remove(this.getIndex());
});
}
}
};
return cell;
});
chuhuo_liebiao.setItems(chuhuo_liebiao_data);
tableView主要参考以下文章JavaFX表格控件TableView高级应用:自动添加ID列、删除操作列、单元格内容个性化渲染 tableView的官方文档翻译 有机会写一篇专门的tableView的各种操作的文章。
2.3.5 弹窗