TWaver With JavaFX – JAVA編程語言程序開發技術文章

JavaFX Script被拋棄以後,很久不關心JavaFX瞭,偶然發現JavaFX已經發展到2.2,而且已經綁定到JDK7中,看來Oracle對它還是蠻重視的。 看到有人在論壇提問TWaver有沒有JavaFX版本,於是去Oracle網站上逛瞭一下,發現可以將JavaFX集成進Swing程序中,真是福音啊,立馬動手寫瞭個Demo,集成起來還是很方便的。
運行效果
 
代碼分析
窗體中間是個很典型的Network,上下各是一個JavaFX容器,上面容器中包含一個Slider,滑動時可以改變TWaver選中Node的RenderColor,下面的容器中包含一些Text,選中Network中的Node並拖動的時候在Text組件上顯示Node的name和location。 在這個Demo裡,程序架構還是Swing,JavaFX提供瞭一個名為JFXPanel的組件可以嵌入到Swing容器中,JFXPanel內可以放JavaFX的組件,需要註意,Swing和JavaFX的線程不同,官方的原文是:"JavaFX data should be accessed only on the JavaFX User thread","Swing data should be changed only on the EDT",所以如果要在Swing中修改JavaFX的數據,應該使用Platform#runLater包裝,本例中,拖動Node時修改JavaFX的Text組件的相關代碼:

 1 network.getDataBox().addElementPropertyChangeListener(new PropertyChangeListener() {
 2             @Override
 3             public void propertyChange(PropertyChangeEvent evt) {
 4                 if("location".equals(evt.getPropertyName())){
 5                     Element element= (Element) evt.getSource();
 6                     final String name=element.getName();
 7                     final double x=element.getX();
 8                     final double y=element.getY();
 9                     Platform.runLater(new Runnable() {
10                         @Override
11                         public void run() {
12                             nameText.setText(name);
13                             locationText.setText("X:"+x+"   Y:"+y);
14                         }
15                     });
16                 }
17             }
18         });

同樣,如果要在JavaFX中修改Swing數據,也要用SwingUtilities#invokeLater包裝,本例中,滑動Slider修改選中Node的Render Color代碼如下:

 1 slider.valueProperty().addListener(new ChangeListener<Number>() {
 2             public void changed(ObservableValue<? extends Number> ov,
 3                                 Number old_val,final Number new_val) {
 4                 SwingUtilities.invokeLater(new Runnable() {
 5                     @Override
 6                     public void run() {
 7                         Iterator it=network.getSelectionModel().selection();
 8                         while (it.hasNext()){
 9                             Element element= (Element) it.next();
10                             element.putRenderColor(new java.awt.Color(new_val.intValue()));
11                         }
12                     }
13                 });
14             }
15         });

最後附上全部代碼:

  1 public class Test1 {
  2     static TNetwork network;
  3     static Text nameText;
  4     static Text locationText;
  5     static  Slider slider;
  6     private static void initAndShowGUI() {
  7         initNetwork();
  8         JFrame frame = new JFrame("TWaver With JavaFX");
  9         frame.getContentPane().setLayout(new BorderLayout());
 10
 11         frame.setSize(500, 300);
 12         frame.setVisible(true);
 13         frame.getContentPane().add(network);
 14
 15         final JFXPanel controlFXPanel = new JFXPanel();
 16         frame.add(controlFXPanel,BorderLayout.NORTH);
 17
 18         final JFXPanel statusFXPanel = new JFXPanel();
 19         frame.add(statusFXPanel,BorderLayout.SOUTH);
 20
 21         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 22         network.getDataBox().addElementPropertyChangeListener(new PropertyChangeListener() {
 23             @Override
 24             public void propertyChange(PropertyChangeEvent evt) {
 25                 if("location".equals(evt.getPropertyName())){
 26                     Element element= (Element) evt.getSource();
 27                     final String name=element.getName();
 28                     final double x=element.getX();
 29                     final double y=element.getY();
 30                     Platform.runLater(new Runnable() {
 31                         @Override
 32                         public void run() {
 33                             nameText.setText(name);
 34                             locationText.setText("X:"+x+"   Y:"+y);
 35                         }
 36                     });
 37                 }
 38             }
 39         });
 40         Platform.runLater(new Runnable() {
 41             @Override
 42             public void run() {
 43                 initFX(statusFXPanel,controlFXPanel);
 44             }
 45         });
 46     }
 47     private static void initNetwork(){
 48         network=new TNetwork();
 49         twaver.Node node1=new Node();
 50         node1.setName("node1");
 51         twaver.Node node2=new Node();
 52         node2.setLocation(200,100);
 53         node2.setName("node2");
 54         Link link=new Link(node1,node2);
 55         network.getDataBox().addElement(node1);
 56         network.getDataBox().addElement(node2);
 57         network.getDataBox().addElement(link);
 58
 59     }
 60     private static void initFX(JFXPanel statusFXPanel,JFXPanel controlFXPanel) {
 61         Scene scene = createStatusScene();
 62         statusFXPanel.setScene(scene);
 63         statusFXPanel.validate();
 64
 65         Scene controlScene=createControlScene();
 66         controlFXPanel.setScene(controlScene);
 67         controlFXPanel.validate();
 68
 69     }
 70     private static Scene createControlScene(){
 71         Group  root  =  new  Group();
 72         Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
 73         HBox hbox=new HBox();
 74         slider =new Slider(0,16777215,0);
 75         slider.setPrefHeight(30);
 76         slider.setPrefWidth(300);
 77         slider.valueProperty().addListener(new ChangeListener<Number>() {
 78             public void changed(ObservableValue<? extends Number> ov,
 79                                 Number old_val, final Number new_val) {
 80                 SwingUtilities.invokeLater(new Runnable() {
 81                     @Override
 82                     public void run() {
 83                         Iterator it = network.getSelectionModel().selection();
 84                         while (it.hasNext()) {
 85                             Element element = (Element) it.next();
 86                             element.putRenderColor(new java.awt.Color(new_val.intValue()));
 87                         }
 88                     }
 89                 });
 90             }
 91         });
 92         Label label=new Label("顏色:");
 93         label.setPrefHeight(30);
 94         hbox.getChildren().add(label);
 95         hbox.getChildren().add(slider);
 96         root.getChildren().add(hbox);
 97         return scene;
 98     }
 99     private static Scene createStatusScene() {
100         Group  root  =  new  Group();
101         Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
102         VBox vbox=new VBox();
103         vbox.setPrefWidth(300);
104         vbox.setPrefHeight(20);
105         HBox hbox=new HBox();
106         nameText=new Text();
107         hbox.getChildren().add(new Label("  Name:"));
108         hbox.getChildren().add(nameText);
109         hbox.getChildren().add(new Text("   "));
110         locationText=new Text();
111         hbox.getChildren().add(new Label("Location:"));
112         hbox.getChildren().add(locationText);
113         vbox.getChildren().add(hbox);
114         root.getChildren().add(vbox);
115         return (scene);
116     }
117
118     public static void main(String[] args) {
119         SwingUtilities.invokeLater(new Runnable() {
120             @Override
121             public void run() {
122                 initAndShowGUI();
123             }
124         });
125     }
126 }

發佈留言