玩javaFX记

玩javaFX记

想为我的脚本解析程序做个ui界面,正好最近又在玩java FX,于是动手写了段脚本。
 
唉,不写不知道,这个javaFX用起来还真是不容易啊,到最后也没办法达到我想要的功能,总是出现莫名其妙的错误。但愿是我掌握得不精通,不然,就得java FX工程师费心了。
 
javaFX的确可以简化程序编写的时间,尤其是布置通用UI部件时,不过干嘛不干脆设计一个配套的UI编辑器呢?
 
另外,javaFX的还有一系列自己独特的语法,也颇为费事,例如在支持标准的java语法if(if (condition) {})时,还支持if condition then的格式,真够麻烦的。
 
还有,用Eclipse编辑时,虽然用了专用的插件,还是不够智能,不能支持Format,语法提示也不够智能,只能希望以后版本改进了。
 
最后,附上我今天写的脚本,是改了别人写好的javaFX编辑器,加上我想要的功能,大家可以参考一下。
 
package browser;
import javafx.ui.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.StringBuilder;
import java.lang.System;
import com.royelee.script.*;
class BrowserModel {
    attribute fileName: String;
    attribute fileContents: String;
    attribute recentFileNames: String*;
    attribute lineWrap: Boolean;
    attribute wordWrap: Boolean;
    operation loadFile(file: File);
    operation exit();
}
attribute BrowserModel.fileName = null;
trigger on BrowserModel.fileName[oldValue] = newValue {
    if (oldValue <> null and oldValue <> newValue) {
        delete recentFileNames[. == newValue];
        insert oldValue as first into recentFileNames;
        delete recentFileNames[indexof . > 8];
    }
}
operation BrowserModel.exit() {
    System.exit(0);
}
operation BrowserModel.loadFile(file: File) {
    var reader = new BufferedReader(new FileReader(file));
    var builder = new StringBuilder();
    while (true) {
        var line = reader.readLine();
        if (line == null) {
            break;
        }
        builder.append(line);
        builder.append(‘n’);
    }
    reader.close();
    this.fileName = file.canonicalPath;
    this.fileContents = builder.toString();
}
var model = new BrowserModel;
var machine = new Machine();
var parser = new XMLParser(machine);
Frame {
    var: win
    width: 550
    height: 350
    visible: true
    onClose: operation() { model.exit(); }
    function makeTitle(fileName: String) =
        "Roye Script Editor {if fileName.length() <> 0 then " – {fileName}" else ”}";
    title: bind makeTitle(model.fileName)
    content: RootPane {
        menubar: MenuBar {
            menus: [
                Menu {
                    var keyStrokes:KeyStroke = [_1,_2,_3,_4,_5,_6,_7,_8,_9]
                    text: "File"
                    mnemonic: F
                    items: bind [
                        MenuItem {
                            text: "Open"
                            mnemonic: O
                            accelerator: {
                                modifier: CTRL
                                keyStroke: O
                            }
                            action: operation() {
                                var fc = FileChooser {
                                 fileFilters: {
                      filter: operation(f:File) {
                          return f.isDirectory() or f.name.endsWith(".xml");
                         }
                         description: "Roye Script Files (*.xml)"
                        }
                                    action: operation(file: File) {
                                        model.loadFile(file);
                                    }
                                };
                                fc.showOpenDialog(win);
                            }
                        },
                        if sizeof model.recentFileNames > 0
                        then MenuSeparator {}
                        else [],
                        foreach (f in model.recentFileNames)
                            MenuItem {
                                mnemonic: bind keyStrokes[indexof f]
                                text: bind "{indexof f + 1} {f}"
                                action: operation() {
                                    model.loadFile(new File(f));      
                                }
                            },
                        MenuSeparator {},
                        MenuItem {
                            text: "Exit"
                            mnemonic: X
                            accelerator: {
                                modifier: CTRL
                                keyStroke: Q
                            }
                            action: operation() { model.exit(); }
                        },
                    ]
                },
                Menu {
                    text: "Action"
                    mnemonic: A
                    items: [
                        MenuItem {
                            text: "Start"
                            mnemonic: S
                            accelerator: {
                                modifier: CTRL
                                keyStroke: S
                            }
                            action: operation() {
                            }
                        },                       
                    ]
                },
                Menu {
                    text: "View"
                    mnemonic: E
                    items: [
                        CheckBoxMenuItem {
                            text: "Word Wrap"
                            mnemonic: W
                            selected: bind model.wordWrap
                        },
                        CheckBoxMenuItem {
                            text: "Line Wrap"
                            mnemonic: L
                            selected: bind model.lineWrap
                        },
                    ]
                },
            ]
        }
        content: BorderPanel {
         border: EmptyBorder {
          top: 10
          left: 10
          bottom: 10
          right: 10
         }
         center:
    TextArea {
              wrapStyleWord: bind model.wordWrap
              lineWrap: bind model.lineWrap
     text: bind model.fileContents
          },
         bottom:
          GridPanel{
           border: EmptyBorder {
            top: 10
            left: 10
            bottom: 10
            right: 10
           }
        rows: 3
       columns: 1
        cells:[
         TextField{
          text: ""
          action: operation(){
          }
         },
         Button{
          verticalTextPosition: CENTER
                      horizontalTextPosition: LEADING          
          text: "start"
          action: operation() {
           System.out.println(model.fileContents);
        System.out.println(model.wordWrap);
        System.out.println(model.lineWrap);
       ] ]>

One thought on “玩javaFX记

  1. wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold -229684052640909

Leave a Reply

Your email address will not be published. Required fields are marked *