2015年12月10日 星期四

B4J 3:程式架構分析

一.程式分析:

1.說明目前這個 Form 的寬和高
#Region  Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
#End Region


2.程序的全域定義 (這只限於本Form的定義)
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private Button1 As Button
Private TextField1 As TextField
Private Label1 As Label
End Sub

3.這支程式啟動及相關載入設定值
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
MainForm.RootPane.LoadLayout("Lay01") 'Load the layout file.
MainForm.Show
TextField1.Text =""
End Sub

4.點擊 Button1 的事件處理
Sub Button1_Action
If IsNumber(TextField1.Text ) Then
If TextField1.Text>=0 And TextField1.Text<=100 Then
Label1.Text ="0-100數值!!"
Else If TextField1.Text>=101 And TextField1.Text<=999 Then
Label1.Text ="101-999數值!!"
Else If TextField1.Text>=1000 Then
Label1.Text ="數值大於等於1000"
End If
Else
Label1.Text ="請輸入數值!!"
End If
 
End Sub

二.檔案存放分析:
依本程式共有 2 檔  2 目錄 :
*.b4j   --- 程式碼
*.meta (依官方的說法) 可建立附加的文件(採用META-INF格式)目錄
Files ---存放UI設計檔區
[Objects] 存放核心檔了
1.轉換 java 後的 *.jar
2.scr  表示 B4J 內容程序將 VB 轉譯為java 語法 所存放的位置
3.bin 目錄 -- 這個就不理它了 !


三.分析B4J轉譯 java 的程式碼    (底下看完,就應該知道 B4J 怎運作VB to java 了)
[不過,Layout 就是精華了! 這部分官方就編譯過了,不需研究了]


package b4j.example;


import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.debug.*;

public class main extends javafx.application.Application{
public static main mostCurrent = new main();

public static BA ba;
static {
ba = new  anywheresoftware.b4j.objects.FxBA("b4j.example", "b4j.example.main", null);
ba.loadHtSubs(main.class);
        if (ba.getClass().getName().endsWith("ShellBA")) {

ba.raiseEvent2(null, true, "SHELL", false);
ba.raiseEvent2(null, true, "CREATE", true, "b4j.example.main", ba);
}
}
    public static Class getObject() {
return main.class;
}


    public static void main(String[] args) {
    launch(args);
    }
    public void start (javafx.stage.Stage stage) {
        try {
            anywheresoftware.b4j.objects.FxBA.application = this;
   anywheresoftware.b4a.keywords.Common.setDensity(javafx.stage.Screen.getPrimary().getDpi());
            BA.Log("Program started.");
            initializeProcessGlobals();
            anywheresoftware.b4j.objects.Form frm = new anywheresoftware.b4j.objects.Form();
            frm.initWithStage(ba, stage, 600, 400);
            ba.raiseEvent(null, "appstart", frm, (String[])getParameters().getRaw().toArray(new String[0]));
        } catch (Throwable t) {
            BA.printException(t, true);
            System.exit(1);
        }
    }
public static anywheresoftware.b4a.keywords.Common __c = null;
public static anywheresoftware.b4j.objects.JFX _fx = null;
public static anywheresoftware.b4j.objects.Form _mainform = null;
public static anywheresoftware.b4j.objects.ButtonWrapper _button1 = null;
public static anywheresoftware.b4j.objects.TextInputControlWrapper.TextFieldWrapper _textfield1 = null;
public static anywheresoftware.b4j.objects.LabelWrapper _label1 = null;
public static String  _appstart(anywheresoftware.b4j.objects.Form _form1,String[] _args) throws Exception{
 //BA.debugLineNum = 14;BA.debugLine="Sub AppStart (Form1 As Form, Args() As String)";
 //BA.debugLineNum = 15;BA.debugLine="MainForm = Form1";
_mainform = _form1;
 //BA.debugLineNum = 16;BA.debugLine="MainForm.SetFormStyle(\"UNIFIED\")";
_mainform.SetFormStyle("UNIFIED");
 //BA.debugLineNum = 17;BA.debugLine="MainForm.RootPane.LoadLayout(\"Lay01\") 'Load the l";
_mainform.getRootPane().LoadLayout(ba,"Lay01");
 //BA.debugLineNum = 18;BA.debugLine="MainForm.Show";
_mainform.Show();
 //BA.debugLineNum = 19;BA.debugLine="TextField1.Text =\"\"";
_textfield1.setText("");
 //BA.debugLineNum = 20;BA.debugLine="End Sub";
return "";
}
public static String  _button1_action() throws Exception{
 //BA.debugLineNum = 22;BA.debugLine="Sub Button1_Action";
 //BA.debugLineNum = 23;BA.debugLine="If IsNumber(TextField1.Text ) Then";
if (anywheresoftware.b4a.keywords.Common.IsNumber(_textfield1.getText())) {
 //BA.debugLineNum = 24;BA.debugLine="If TextField1.Text>=0 And TextField1.Text<=100 T";
if ((double)(Double.parseDouble(_textfield1.getText()))>=0 && (double)(Double.parseDouble(_textfield1.getText()))<=100) {
 //BA.debugLineNum = 25;BA.debugLine="Label1.Text =\"0-100數值!!\"";
_label1.setText("0-100數值!!");
 }else if((double)(Double.parseDouble(_textfield1.getText()))>=101 && (double)(Double.parseDouble(_textfield1.getText()))<=999) {
 //BA.debugLineNum = 27;BA.debugLine="Label1.Text =\"101-999數值!!\"";
_label1.setText("101-999數值!!");
 }else if((double)(Double.parseDouble(_textfield1.getText()))>=1000) {
 //BA.debugLineNum = 29;BA.debugLine="Label1.Text =\"數值大於等於1000\"";
_label1.setText("數值大於等於1000");
 };
 }else {
 //BA.debugLineNum = 32;BA.debugLine="Label1.Text =\"請輸入數值!!\"";
_label1.setText("請輸入數值!!");
 };
 //BA.debugLineNum = 35;BA.debugLine="End Sub";
return "";
}

private static boolean processGlobalsRun;
public static void initializeProcessGlobals() {
 
    if (main.processGlobalsRun == false) {
   main.processGlobalsRun = true;
try {
       main._process_globals();

        } catch (Exception e) {
throw new RuntimeException(e);
}
    }
}public static String  _process_globals() throws Exception{
 //BA.debugLineNum = 6;BA.debugLine="Sub Process_Globals";
 //BA.debugLineNum = 7;BA.debugLine="Private fx As JFX";
_fx = new anywheresoftware.b4j.objects.JFX();
 //BA.debugLineNum = 8;BA.debugLine="Private MainForm As Form";
_mainform = new anywheresoftware.b4j.objects.Form();
 //BA.debugLineNum = 9;BA.debugLine="Private Button1 As Button";
_button1 = new anywheresoftware.b4j.objects.ButtonWrapper();
 //BA.debugLineNum = 10;BA.debugLine="Private TextField1 As TextField";
_textfield1 = new anywheresoftware.b4j.objects.TextInputControlWrapper.TextFieldWrapper();
 //BA.debugLineNum = 11;BA.debugLine="Private Label1 As Label";
_label1 = new anywheresoftware.b4j.objects.LabelWrapper();
 //BA.debugLineNum = 12;BA.debugLine="End Sub";
return "";
}
}

沒有留言:

張貼留言