1

ListGrid は常に Final でなければならないことに気付きました。私にとっては、別のクラスからプロパティを変更したいので、静的オブジェクトが最適です。

私のプロジェクトでは、明確なコードを作成するために、いくつかのクラスを作成しました。

  1. DataGrid extends ListGrid新しいオブジェクトのプロパティを設定し、データを入力するクラス。@Override メソッドは、グリッド オブジェクトにボタンを追加します。

  2. PopupWindow extend WindowListGrid の編集ボタンをクリックしたときに Window オブジェクトを作成するために使用されるクラス。ウィンドウには、新しいデータを追加できるテキストボックスと送信ボタンがあります。[送信] ボタンの OnClick イベントは、データを MySQL サーバーに書き込み、グリッドを実際のデータ(MySQL からのクエリ)で更新する必要があります。これは私が実装できない部分です。

  3. Entry Point クラス onModuleLoad には、次のグリッド コードがあります。

    final DataGrid grid_far = new DataGrid(); grid_far.setGridData();

私はJavaが初めてです。おそらく、それほど多くのクラスを使用するべきではありません。エントリポイントクラスonModuleLoad()にすべてを入れてください。

PopupWindow で Window クラスを拡張する場合、Entry Point クラスから onModuleLoad() メソッドを実行する Button OnClick を宣言すると、Web ページが複製されますか? 私はこれを行うことを意味します:

 EntryPointClass ep = new EntryPointClass();
 ep.onModuleLoad();
4

1 に答える 1

0
  • 「ListGrid は常に Final でなければなりません」

これは必要ありません。ListGrid 変数を非最終として作成することは可能です。
ListGrid 変数が final として宣言されているサンプルを見たことがあるはずですが、それは別の理由によるものです。

たとえば、非最終ローカル変数 (メソッド内で宣言されたもの) を匿名内部クラスで使用することはできません。
したがって、内部クラスからローカル変数にアクセスするには、それらを final として宣言する必要があります。
SmartGWT/Swing/などで。内部クラスは、イベント処理などのさまざまなコールバック機能を実装するために使用されます。

public class Screen {

    ListGrid grid1 = new ListGrid();
    TextItem text1 = new TextItem("text1", "Text 1");

    public void initialize() {
        // normally its not required to create subclasses of ListGrid/Button/Window/etc.
        // unless a significant change in their behavior is needed

        ListGrid grid2 = new ListGrid();
        // setup grid properties
        // set grid fields

        TextItem text2 = new TextItem("text2", "Text 2");

        final ListGrid grid3 = new ListGrid();
        final TextItem text3 = new TextItem("text3", "Text 3");

        IButton button = new IButton("Edit");
        button.addClickHandler(new ClickHandler() { // this is declaring an anonymous inner class
            public void onClick(ClickEvent clickEvent) { // this method is a member of that anonymous inner class
                // out of three ListGrid and thee TextItem instances, only following can be accessed in this method/class
                //   grid1, text1 (these are not local variables, inner class can access outer class members without any issue)
                //   grid3, text3 (as they are final, even though local variables)
            }
        });

        // that does not mean, grid2 and text2 can not be used, they can be, just not inside an anonymous inner class
        // e.g.-
        DynamicForm form = new DynamicForm();
        form.setFields(text2);

        VLayout layout = new VLayout();
        layout.addMember(grid2);
    }
}

内部クラスでのローカル変数の使用の詳細については、次のリンクを確認してください
内部クラスとローカル変数
Java のローカル最終変数に関する質問

  • 「別のクラスからプロパティを変更したいので、静的オブジェクトが最適です」

オブジェクト間で通信するには、静的変数を使用するよりも優れた方法があります。

  • 「あまり多くのクラスを使用するべきではないかもしれません。エントリ ポイント クラス onModuleLoad() にすべてを入れてください」

onModuleLoad() のコードを最小限に抑えることをお勧めします。
必要なクラスの数は、実装しようとしているものによって異なります。

  • "エントリーポイント"

GWT がアプリケーションを作成するために実行を引き渡す場所であるため、EntryPoint 実装を削除することはできません。
そのために onModuleLoad() が GWT/JavaScript エンジンによって呼び出されます。
コードで呼び出すことはできません。

コード サンプルを含むSmartGWTショーケースをご覧ください。
詳細については、 SmartGWT APIを参照してください。
同じ結果を得るために UI を作成する方法は複数あります。

SmartGWT でデータを送受信するためのサーバーとの通信は、それ自体がトピックです。

可能な実装ガイドライン。

public class EntryPointClass implements EntryPoint {
    public void onModuleLoad() {
        ApplicationScreen screen = new ApplicationScreen();

        HStack drawArea = new HStack();
        drawArea.setWidth100();
        drawArea.setHeight100();
        drawArea.addMember(screen.getComponents());
        drawArea.draw();
    }
}

public class ApplicationScreen { // this class does not need to extend from a widget
    public Canvas getComponents() {
        // a method that prepares the interface
        // using a from+grid type layout, without a popup window

        ListGrid grid = getListGrid();

        DynamicForm form = getDynamicForm(grid); // have to pass grid in order to add/update records on button events

        VLayout layout = new VLayout();
        layout.addMember(form);
        layout.addMember(grid);

        return layout;
    }

    private DynamicForm getDynamicForm(final ListGrid grid) { // have to declare grid as final to access from event handler inner classes
        final TextItem text1 = new TextItem("text1", "Text 1");  // have to declare as final for same reason

        ButtonItem saveButton = new ButtonItem("Save");
        saveButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                // use text1, grid and other components to save form values and refresh grid
            }
        });

        // creating and configuring form
        DynamicForm form = new DynamicForm();
        form.setWidth100();
        form.setFields(text1, saveButton);

        return form;
    }

    private ListGrid getListGrid() {
        // preparing grid fields
        ListGridField field1 = new ListGridField("field1", "Field 1");

        // creating and configuring grid
        ListGrid grid = new ListGrid(); // not final, does not need to be
        grid.setWidth100();
        grid.setFields(field1);

        return grid;
    }
}
于 2013-05-01T21:04:20.733 に答える