JavaFX实现图片显示,文本输入,表格显示

测试开发了一个javaFx写的支持文本输入,pdf文件转成图片显示,可以进行图标显示的demo,其中用Eclipse实现,代码结构如下图所示:

JavaFX实现图片显示,文本输入,表格显示_第1张图片

Main.java代码如下所示:

package application;	
import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.fxml.FXML;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.TextField;import javafx.scene.layout.StackPane;import javafx.scene.paint.Color;import javafx.stage.Stage;public class Main extends Application {@Overridepublic void start(Stage primaryStage) {try {Parent root = FXMLLoader.load(getClass().getResource("/application/MyScence.fxml"));primaryStage.setTitle("我的第一个fx应用");primaryStage.setScene(new Scene(root));primaryStage.show();} catch(Exception e) {e.printStackTrace();}}// @Override// public void start(Stage primaryStage) {// Button btn = new Button();// btn.setText("Say 'Hello World'");// btn.setOnAction(new EventHandler() {// @Override// public void handle(ActionEvent event) {// System.out.println("Hello World!");// }// });//// StackPane root = new StackPane();// root.getChildren().add(btn);// Scene scene = new Scene(root, 300, 250,Color.BLACK);// // //// primaryStage.setTitle("Hello World!");// //primaryStage.setFullScreen(true);// primaryStage.setScene(scene);// primaryStage.show();// }public static void main(String[] args) {launch(args);}}
 布局文件如下所示: 
  











    
MyController.java如下:

package application;


import java.awt.ItemSelectable;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import pdfboxTest.PdfBoxTest;

public class MyController implements Initializable {
	@FXML
	private Button myButton;

	@FXML
	private TextField myTextFile;

	@FXML
	private ImageView myImageView;
	
	@FXML
	private BorderPane myBorderPane;	

	private Boolean isDragStart = false;
	
	private double startX;
	private double startY;
	private double traslateX;
	private double traslateY;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		myTextFile.setVisible(true);
		myTextFile.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler() {

			@Override
			public void handle(KeyEvent event) {
				event.consume();
				KeyCode code = event.getCode();
				if (code == KeyCode.ENTER) {
					try {
						System.out.println(myTextFile.getText());
						//myImageView.setVisible(false);
//					   TextField tx = new TextField(myTextFile.getText());
//					   myBorderPane.setCenter(tx);
						//插入表格
						TableView tv = new TableView();
						ObservableList Task = FXCollections.observableArrayList();
						Task.add(new Task("雪龙号",1,"10-1完工"));
					    Task.add(new Task("南极号", 2, "10-2完工"));	
					    tv.setItems(Task);
					    						
						TableColumn shipCol = new TableColumn("ship");
						shipCol.setCellValueFactory(new PropertyValueFactory<>("ship"));
						TableColumn  numCol = new TableColumn("num"); 
						numCol.setCellValueFactory(new PropertyValueFactory<>("num"));
						TableColumn planCol = new TableColumn("plan");
						planCol.setCellValueFactory(new PropertyValueFactory<>("plan"));
						
						tv.getColumns().setAll(shipCol,numCol,planCol);						
						myBorderPane.setCenter(tv);
						
						myTextFile.clear();
						new Robot().keyPress(java.awt.event.KeyEvent.VK_RIGHT);//什么意思

						// XXX javafx has no robot for key type...
					} catch (Exception e1) {

					}
					event.consume();
				}
			}
		});
	}

	public void showDataTime()  {
		System.out.println("按钮被点击啦啊啊啊啊!");

		Date now = new Date();

		DateFormat df = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
		String dateTimeString = df.format(now);
		BufferedImage image = null;
		try {
			image = PdfBoxTest.getImageFromPdf("H:/test.pdf");
			//ImageIO.write(image, "png", new File("D:/test.png"));
			System.out.println("image写入完毕");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		WritableImage wImage = new WritableImage(image.getWidth(),image.getHeight());
		System.out.println("image.getWidth()"+image.getWidth()+"  image.getHeight()"+image.getHeight());
		SwingFXUtils.toFXImage(image, wImage);//什么作用
		myTextFile.setText(dateTimeString);//设置文本框默认显示时间
	
		myImageView.setFitHeight(600);
		myImageView.setFitWidth(900);
		myImageView.setImage(wImage);
		
	}

	public void keyInputAction() {
		System.out.println("键盘事件被监听到了");
		myTextFile.requestFocus();
		String str = myTextFile.getText();
		CharSequence cs = myTextFile.getCharacters();//getCharacters是返回的什么?

		System.out.println("cs:" + cs);
		System.out.println(str);
		myTextFile.clear();
	}

	public void onScoller(ScrollEvent event) {

		System.out.println("滚轮事件触发");
		System.out.println(event.getDeltaX());
		System.out.println(event.getDeltaY());
		if (event.getDeltaY() > 0) {
			myImageView.setScaleX(myImageView.getScaleX() * 1.2);
			myImageView.setScaleY(myImageView.getScaleY() * 1.2);
			event.consume();
			// myImageView.resize(500, 500);
			System.out.println("image resized!");
			// ScaleTransition st =
			// ScaleTransitionBuilder.create().byX(1.5).byY(1.5).node(myImageView).duration(Duration.millis(500)).build();
		} else {
			myImageView.setScaleX(myImageView.getScaleX() * 0.8);
			myImageView.setScaleY(myImageView.getScaleY() * 0.8);
		}
	}

	public void onMousePressed(MouseEvent event) {
		System.out.println("onMousePressed:x=" + event.getX() + ";y=" + event.getY());
		startX = event.getSceneX();
		startY = event.getSceneY();
	    traslateX = myImageView.getTranslateX();
		traslateY = myImageView.getTranslateY();
	}

	public void onMouseMoved(MouseEvent event) {
		System.out.println("onMouseMoved:x=" + event.getX() + ";y=" + event.getY());
	}

	@FXML
	public void onMouseRelease(MouseEvent event) {
		System.out.println("onMouseRelease:x=" + event.getX() + ";y=" + event.getY());
		isDragStart = false;
		event.consume();
	}

	@FXML
	public void OnDragDetected(MouseEvent event) throws InterruptedException {
		System.out.println("OnDragDetected" + event.getX() + ";y=" + event.getY());
		//填充内容,触发OnDragDetected事件
		Dragboard db = myImageView.startDragAndDrop(TransferMode.ANY);
		ClipboardContent content = new ClipboardContent();
		content.putString("hahahahh");
		db.setContent(content);
//		startX = event.getSceneX();
//		startY = event.getSceneY();
		event.consume();
	}

	public void onDragOver(DragEvent event) {
		System.out.println("onDragOver:" + (event.getSceneX()-startX) + ";y=" + (event.getSceneY()-startY));
//		traslateX = event.getSceneX()-startX;
//		traslateY = event.getSceneY()-startY;
		myImageView.setTranslateX(traslateX+event.getSceneX()-startX);
		myImageView.setTranslateY(traslateY+event.getSceneY()-startY);
	}

}
Task类

package application;

public class Task{
	private String ship;
	private Integer num;
	private String plan;
	Task(String ship,Integer num, String plan){
		this.ship= ship;
		this.num = num;
		this.plan = plan;
	}
	public String getShip() {
		return ship;
	}
	public void setShip(String ship) {
		this.ship = ship;
	}
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
	public String getPlan() {
		return plan;
	}
	public void setPlan(String plan) {
		this.plan = plan;
	}
	
  
}
pdf转图片的实现:

package pdfboxTest;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.rendering.PDFRenderer;

public class PdfBoxTest {

	public static void main(String[] args) throws InvalidPasswordException, IOException {
		PDDocument doc = PDDocument.load(new File("H:/test.pdf"));
		PDFRenderer renderer = new PDFRenderer(doc);
		int pageCount = doc.getNumberOfPages();
		System.out.println(pageCount);
		for(int i=0;i






你可能感兴趣的:(javaFx)