1

可視性プロパティに従って、コンポジット内のボタンを再描画したいと思います。合成して、そこにあるボタンに従ってサイズを変更し、次のコードを使用して合成を更新しています。 問題:以下のコードは正常に機能しますが、ボタンがコンポジットで再配置されることはありません 。助けてください。再配置するコードに欠けているものはありますか?

public void redraw_buttons() {
    int offsetButton = 5;
            int buttonHeight =28*3;

            for (Control kid : compositeButtons.getChildren()) {
                if (kid.getClass() == Button.class) {
                    if(kid.getVisible() == true){       
                        kid.setLocation(0, 0);//layout(true, true);
                        kid.setSize(50,60);
                        kid.redraw();
                        kid.pack();
                        compositeButtons.redraw();
                        kid.setRedraw(true);
                        kid.setBounds(10, offsetButton, 259, buttonHeight);
                        kid.redraw();
                        offsetButton = offsetButton + buttonHeight; 
                    }}
                        }
            compositeButtons.redraw();
        }

また、コンポジットでボタンを作成するために、次のコードを使用しています

//コンポジットの初期化---

compositeButtons = new Composite(shell, SWT.BORDER);
            compositeButtons.setLayout(null);
            FormData fd_compositeButtons = new FormData();
            fd_compositeButtons.top = new FormAttachment(lblUserLoggedinOnServer, 6);
            fd_compositeButtons.bottom = new FormAttachment(textRecentLog, -6);
            fd_compositeButtons.right = new FormAttachment(textRecentLog, 0, SWT.RIGHT);
            fd_compositeButtons.left = new FormAttachment(0, 30);
            compositeButtons.setLayoutData(fd_compositeButtons);

            compositeButtons.layout(true, true);
                    getShell().layout(true, true);

//コンポジットにボタンを追加-

int offsetButton = 5;
            int buttonHeight =28*3;

            MakeAppointmentRequestButton = new Button(compositeButtons, SWT.NONE);
            MakeAppointmentRequestButton.setBounds(10, offsetButton, 259, buttonHeight);
            //MakeAppointmentRequestButton.layout(true, true);
            MakeAppointmentRequestButton.setRedraw(true);
            MakeAppointmentRequestButton.setText("Make Appointment");

            offsetButton = offsetButton + buttonHeight; 

            GotoAppointmentRequestButton = new Button(compositeButtons, SWT.NONE);
            GotoAppointmentRequestButton.setBounds(10, offsetButton, 259, buttonHeight);//(10, offsetButton, 259, 28*3);
            GotoAppointmentRequestButton.setRedraw(true);
            GotoAppointmentRequestButton.setText("Goto Appointment");

            offsetButton = offsetButton + buttonHeight; 

            CancelAppointmentRequestButton= new Button(compositeButtons, SWT.NONE);
            CancelAppointmentRequestButton.setBounds(10, offsetButton, 259, buttonHeight);//(10,  28*3+5, 259, 28*3);
            CancelAppointmentRequestButton.setRedraw(true);
            CancelAppointmentRequestButton.setText("Cancel Appointment");

            offsetButton = offsetButton + buttonHeight;

さまざまなリンクがpack()、redraw()プロパティも試しているのを見てきました。ただし、問題:カスタム関数redraw_buttons()を呼び出しても、ボタンがコンポジットで再配置されることはありません。

助けてください。再配置するコードに欠けているものはありますか?これを行う方法を提案してください。私はSWTを初めて使用します。

ありがとう..

******* 編集新規更新 ********

エラーの原因は

1.)redraw_buttons()メインスレッドで関数が呼び出されませんでした。

_appLoader.display.getDefault().asyncExec(new Runnable() {
    public void run() {
_appLoader.MyObj.redraw_buttons();
}
});
// This gave access to main thread.

2.)複合フレームはredraw_buttons()でサイズ変更されませんでした。

// Code: On Which I am still working upon.

ここに画像の説明を入力してください シェルのサイズを変更する必要がありますか?plsは提案します。

ご協力いただきありがとうございます。

4

2 に答える 2

3

これがあなたが望むことをするか、少なくともそれを達成する方法をあなたに示すべきいくつかのコードです:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, true));

    // Stretch first label horizontally
    Label upperLeft = new Label(shell, SWT.NONE);
    upperLeft.setText("Label 1 name");
    upperLeft.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false));

    // Stretch second label horizontally but align to the right
    Label upperRight = new Label(shell, SWT.NONE);
    upperRight.setText("time");
    upperRight.setLayoutData(new GridData(SWT.END, SWT.BEGINNING, true, false));

    // Stretch button comp in both directions
    Composite buttonComp = new Composite(shell, SWT.BORDER);
    buttonComp.setLayout(new GridLayout(1, true));
    GridData buttonData = new GridData(SWT.FILL, SWT.FILL, true, true);
    buttonData.horizontalSpan = 2;
    buttonComp.setLayoutData(buttonData);

    Button one = new Button(buttonComp, SWT.PUSH);
    one.setText("Button 1");
    one.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    Button two = new Button(buttonComp, SWT.PUSH);
    two.setText("Button 2");
    two.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    Button three = new Button(buttonComp, SWT.PUSH);
    three.setText("Button 3");
    three.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    // Stretch third label horizontally
    Label lowerLeft = new Label(shell, SWT.NONE);
    lowerLeft.setText("Label 2");
    lowerLeft.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false));

    // Stretch fourth label horizontally
    Label lowerRight = new Label(shell, SWT.NONE);
    lowerRight.setText("Label 3");
    lowerRight.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false));

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

起動時の外観は次のとおりです。

ここに画像の説明を入力してください

サイズ変更後:

ここに画像の説明を入力してください

于 2013-02-06T09:51:36.797 に答える
1

それ以外の

(kid.getClass() == Button.class)

いつも使う

(kid instanceof Button)
于 2013-02-06T08:03:57.300 に答える