① 首先看效果图
点击上传商品
点击上传照片
点击所要上传的商品照片
点击添加后,数据库中会得到图片路径
② 上代码
一些窗口label和textbox布局还有按钮功能设置
package Second;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import sample.character.goods;
import sample.dao.GoodsDao;
import sample.util.DbUtil;
import sample.util.StringUtil;
import javax.swing.*;
import java.io.File;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
public class GoodsAddForm implements EventHandler<ActionEvent> {
DbUtil dbUtil=new DbUtil();
private String uNo;
private String identity;
private String gName;
public GoodsAddForm(String identity,String uNo,String gName) {
this.identity = identity;
this.uNo=uNo;
this.gName=gName;
}
GoodsDao goodsDao=new GoodsDao();
final TextField gN=new TextField();
final TextField gP=new TextField();
final TextField gK=new TextField();
final TextField LJ=new TextField();
final Label labelgN=new Label("商品名称:");
final Label labelgP=new Label("商品价格:");
final Label labelgK=new Label("商品类型:");
final Label P_Lj=new Label("图片路径:");
final Label labelsC=new Label("上传商品照片:");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void handle(ActionEvent event) {
/*新建一个网格面板
* */
gN.setPromptText("输入商品名称");
gP.setPromptText("输入商品价格(元)");
gK.setPromptText("输入商品类型");
LJ.setPromptText("图片路径");
Stage stage = new Stage();
GridPane rootGP=new GridPane();
rootGP.setAlignment(Pos.CENTER);
rootGP.setHgap(10);
rootGP.setVgap(10);
rootGP.setPadding(new Insets(10,10,10,10));
rootGP.add(labelgN,0,0);
rootGP.add(labelgP,0,2);
rootGP.add(labelgK,0,4);
rootGP.add(gN,1,0);
rootGP.add(gP,1,2);
rootGP.add(gK,1,4);
rootGP.add(LJ,1,6);
rootGP.add(P_Lj,0,6);
rootGP.add(labelsC,0,7);
final Button TJ=new Button("添加");
final Button CZ=new Button("重置");
final Button sC=new Button("上传照片");
/*重置操作*/
CZ.setOnAction(e -> {
restA();
});
/*关闭窗口*/
TJ.setOnAction(e -> {
GoodAdd();
stage.close();
System.out.println(df.format(new Date()));
});
//中间代码为下面图片上传功能
:
:
:
rootGP.add(TJ,0,9);
rootGP.add(CZ,1,9);
rootGP.add(sC,1,7);
/*创建一个面板*/
BorderPane rootPane = new BorderPane();
rootPane.setCenter(rootGP);
Scene scene = new Scene(rootPane, 500, 450);
stage.setScene(scene);
stage.setResizable(false);//不可修改界面大小
rootGP.setStyle("-fx-background-color: cornsilk; ");
stage.setTitle("上传商品");
stage.getIcons().add(new Image("/sample/img/11.jpg"));
stage.setScene(scene);
stage.centerOnScreen();
stage.show();
}
/*重置方法*/
private void restA() {
Clear();
}
private void Clear()
{
this.gN.setText("");
this.gK.setText("");
this.gP.setText("");
this.LJ.setText("");
}
主要的上传图片功能
/*
*
* 图片上传功能,点击sC按钮执行
*
* */
sC.setOnAction(new EventHandler<ActionEvent>() {
@Override
/*上传本地*/
public void handle(ActionEvent event) {
Stage stage=new Stage();
FileChooser fc=new FileChooser();
final String[] a = {new String()};
fc.setTitle("上传图片");
fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("图片类型","*.jpg","*.png","*.jpeg"));
fc.setInitialDirectory(new File("D:\\Seconderary\\src\\sample\\img")); //文件目录
List<File> list=fc.showOpenMultipleDialog(stage);
list.forEach(new Consumer<File>() {
@Override
public void accept(File file) {
a[0] = file.getAbsolutePath();
System.out.println(a[0]);
final ImageView imageView = new ImageView(
new Image("file:\\"+a[0],200,200,false,false)
);
System.out.println("file:\\"+a[0]);
final Button tp=new Button("",imageView);
rootGP.add(tp,2,7);
}
});
LJ.setText(a[0]);
//fc.showOpenDialog(stage);
}
}
);
数据库的连接问题
private void GoodAdd() {
String gN1 = this.gN.getText();
String gP1 = this.gP.getText();
String gk1 = this.gK.getText();
String xh1=this.uNo;
String LJ1= this.LJ.getText();
String date= this.df.toString();
//判断是否为空
if (StringUtil.isEmpty(gN1)) {
JOptionPane.showMessageDialog(null, "商品名称不能为空!");
}
if (StringUtil.isEmpty(gP1)) {
JOptionPane.showMessageDialog(null, "商品价格不能为空!");
}
goods good=new goods(gN1, gP1 ,gk1 ,xh1,LJ1,date);
Connection con=null;
try {
con=dbUtil.getCon();
int n= 0;
n = goodsDao.add(con,good);
if (n==1){ JOptionPane.showMessageDialog(null, "商品上传成功!");}
else{
JOptionPane.showMessageDialog(null, "商品上传失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try{
dbUtil.closeCon(con);//关闭数据库
} catch (Exception e){
e.printStackTrace();
}
}
}
(数据库要自己建,字段问题要与数据库一致,否则连接错误)