SWT:激活shell.layout()时不需要的文本字段跳转

时间:2014-08-11 09:39:45

标签: java swt textfield

我在SWT中创建了这个GUI,所以当单击一个按钮时,程序会运行一个函数,然后在标签中显示结果。为了正确显示标签文本,我需要激活shell.layout()功能,以便GUI显示我的新标签 但那会引起另一个问题。每次激活shell.layout()函数时,文本字段会跳到GUI的左上角,我不知道为什么,或者如何让它消失。 这是我的代码:

import java.awt.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.swt.widgets.Table;

import java.util.*;

//import DosManager.doSmanager;

import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.graphics.Point;;
public class WinApp2 {
    private DataBindingContext m_bindingContext;

    protected Shell shell;
    private Text destenation_ip;
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
    private Label lblDestenationPortIp;
    private Label lblNewLabel;
    private Text amount_of_packets;
    private Label lblNewLabel_1;
    private Text source_ip;
    private Text destenation_port;
    private Text source_port;
    private Label lblNewLabel_2;
    private Label label;
    private Label lblNewLabel_4;
    private Label lblNewLabel_5;
    private Label lblNewLabel_6;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                try {
                    WinApp2 window = new WinApp2();
                    window.open();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        shell = new Shell(display);
        shell.setBackgroundMode(SWT.INHERIT_FORCE);
        formToolkit.setBackground(null);
        createContents();
        shell.open();
        shell.layout();

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

    /**
     * Create contents of the window.
     */
    protected void createContents() {

        shell.layout();
        shell.setBackgroundMode(SWT.INHERIT_FORCE);
        shell.setSize(450, 307);
        shell.setMinimumSize(450, 307);
        shell.setText("LAN Destroyer");
        shell.setLayout(new FormLayout());
        destenation_ip = new Text(shell, SWT.BORDER);
        FormData fd_destenation_ip = new FormData();
        destenation_ip.setLayoutData(fd_destenation_ip);
        lblDestenationPortIp = new Label(shell, SWT.NONE);
        lblDestenationPortIp.setOrientation(SWT.RIGHT_TO_LEFT);
        FormData fd_lblDestenationPortIp = new FormData();
        fd_lblDestenationPortIp.top = new FormAttachment(destenation_ip, 3, SWT.TOP);
        fd_lblDestenationPortIp.left = new FormAttachment(0, 351);
        lblDestenationPortIp.setLayoutData(fd_lblDestenationPortIp);
        //lblDestenationPortIp.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
        lblDestenationPortIp.setText("IP \u05E9\u05DC \u05D9\u05E2\u05D3:");

        lblNewLabel = new Label(shell, SWT.NONE);
        fd_lblDestenationPortIp.right = new FormAttachment(lblNewLabel, 0, SWT.RIGHT);
        lblNewLabel.setAlignment(SWT.RIGHT);
        FormData fd_lblNewLabel = new FormData();
        fd_lblNewLabel.right = new FormAttachment(100, -10);
        lblNewLabel.setLayoutData(fd_lblNewLabel);
        lblNewLabel.setText(":\u05DB\u05DE\u05D5\u05EA");

        amount_of_packets = new Text(shell, SWT.BORDER);
        fd_destenation_ip.right = new FormAttachment(amount_of_packets, 0, SWT.RIGHT);
        fd_destenation_ip.left = new FormAttachment(amount_of_packets, 0, SWT.LEFT);
        FormData fd_amount_of_packets = new FormData();
        fd_amount_of_packets.right = new FormAttachment(lblNewLabel, -68);
        fd_amount_of_packets.top = new FormAttachment(lblNewLabel, -3, SWT.TOP);
        amount_of_packets.setLayoutData(fd_amount_of_packets);

        Button nonstop_send = new Button(shell, SWT.CHECK);
        fd_amount_of_packets.left = new FormAttachment(0, 186);
        nonstop_send.setAlignment(SWT.RIGHT);
        nonstop_send.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(nonstop_send.getSelection()){
                    amount_of_packets.setEnabled(false);

                }
                else {
                    amount_of_packets.setEnabled(true);
                }

            }
        });
        FormData fd_nonstop_send = new FormData();
        fd_nonstop_send.top = new FormAttachment(lblNewLabel, 0, SWT.TOP);
        fd_nonstop_send.right = new FormAttachment(amount_of_packets, -6);
        nonstop_send.setLayoutData(fd_nonstop_send);
        nonstop_send.setText("\u05E9\u05DC\u05D7 \u05DC\u05DC\u05D0 \u05D4\u05E4\u05E1\u05E7\u05D4");

        lblNewLabel_1 = new Label(shell, SWT.NONE);
        lblNewLabel_1.setOrientation(SWT.RIGHT_TO_LEFT);
        fd_lblNewLabel.top = new FormAttachment(lblNewLabel_1, 6);
        lblNewLabel_1.setAlignment(SWT.RIGHT);
        FormData fd_lblNewLabel_1 = new FormData();
        fd_lblNewLabel_1.top = new FormAttachment(lblDestenationPortIp, 6);
        fd_lblNewLabel_1.right = new FormAttachment(100, -10);
        lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
        lblNewLabel_1.setText("IP \u05E9\u05DC \u05DE\u05E7\u05D5\u05E8:");

        source_ip = new Text(shell, SWT.BORDER);
        fd_destenation_ip.bottom = new FormAttachment(source_ip);
        fd_lblNewLabel_1.left = new FormAttachment(source_ip, 29);
        FormData fd_source_ip = new FormData();
        fd_source_ip.top = new FormAttachment(0, 58);
        fd_source_ip.left = new FormAttachment(0, 186);
        fd_source_ip.right = new FormAttachment(100, -114);
        source_ip.setLayoutData(fd_source_ip);      
        destenation_port = new Text(shell, SWT.BORDER);
        fd_nonstop_send.left = new FormAttachment(destenation_port, 3, SWT.LEFT);
        FormData fd_destenation_port = new FormData();
        fd_destenation_port.bottom = new FormAttachment(destenation_ip, 0, SWT.BOTTOM);
        fd_destenation_port.top = new FormAttachment(0, 32);
        fd_destenation_port.left = new FormAttachment(0, 40);
        fd_destenation_port.width = 78;
        destenation_port.setLayoutData(fd_destenation_port);

        source_port = new Text(shell, SWT.BORDER );
        fd_destenation_port.right = new FormAttachment(source_port, 0, SWT.RIGHT);
        FormData fd_source_port = new FormData();
        fd_source_port.left = new FormAttachment(0, 40);
        fd_source_port.top = new FormAttachment(lblNewLabel_1, -3, SWT.TOP);
        source_port.setLayoutData(fd_source_port);
        lblNewLabel_2 = new Label(shell, SWT.NONE);
        FormData fd_lblNewLabel_2 = new FormData();
        fd_lblNewLabel_2.left = new FormAttachment(destenation_port, 6);
        fd_lblNewLabel_2.top = new FormAttachment(destenation_ip, 3, SWT.TOP);
        lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
        lblNewLabel_2.setText(":\u05E4\u05D5\u05E8\u05D8");

        label = new Label(shell, SWT.NONE);
        label.setText(":\u05E4\u05D5\u05E8\u05D8");
        FormData fd_label = new FormData();
        fd_label.top = new FormAttachment(lblNewLabel_2, 6);
        fd_label.left = new FormAttachment(source_port, 6);
        label.setLayoutData(fd_label);

        Button btnAttack = new Button(shell, SWT.NONE);
        btnAttack.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                //check if inserted data is valid
                Text[] texts = new Text[5];
                texts[0] = destenation_ip;
                texts[1] = source_ip;
                texts[2] = destenation_port;
                texts[3] = source_port;
                if(amount_of_packets.isEnabled())
                    texts[4] = amount_of_packets;
                else //if amount of packets isn't enabled, don't even check it.
                {
                    texts[4] = new Text(shell,SWT.BORDER);
                    texts[4].setText("0");
                }


                shell.layout(false);    

            }
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
        btnAttack.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.NORMAL));
        btnAttack.setToolTipText("!\u05D4\u05E4\u05DC \u05D0\u05EA \u05D4\u05E8\u05E9\u05EA");
        FormData fd_btnAttack = new FormData();
        fd_btnAttack.left = new FormAttachment(destenation_ip, 0, SWT.LEFT);
        btnAttack.setLayoutData(fd_btnAttack);
        btnAttack.setText("Attack!");

        Label lblNewLabel_3 = new Label(shell, SWT.NONE);
        FormData fd_lblNewLabel_3 = new FormData();
        fd_lblNewLabel_3.right = new FormAttachment(100, -10);
        fd_lblNewLabel_3.top = new FormAttachment(lblNewLabel, 17);
        lblNewLabel_3.setLayoutData(fd_lblNewLabel_3);
        lblNewLabel_3.setText(":\u05E4\u05E8\u05D5\u05D8\u05D5\u05E7\u05D5\u05DC");

        Combo protocol_combo = new Combo(shell, SWT.READ_ONLY);
        protocol_combo.add("TCP");
        protocol_combo.add("UDP");
        protocol_combo.addSelectionListener(new SelectionListener()
        {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub
                if(protocol_combo.getText().equals("TCP"))
                    lblNewLabel_5.setVisible(true);
                else
                    lblNewLabel_5.setVisible(false);
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

            } 
        });

        FormData fd_protocol_combo = new FormData();
        fd_protocol_combo.top = new FormAttachment(lblNewLabel_3, 0, SWT.TOP);
        fd_protocol_combo.right = new FormAttachment(lblNewLabel_3, -6);
        protocol_combo.setLayoutData(fd_protocol_combo);
        formToolkit.paintBordersFor(protocol_combo);

        lblNewLabel_4 = new Label(shell, SWT.NONE);
        FormData fd_lblNewLabel_4 = new FormData();
        fd_lblNewLabel_4.top = new FormAttachment(amount_of_packets, 6);
        fd_lblNewLabel_4.bottom = new FormAttachment(100, -79);
        fd_lblNewLabel_4.right = new FormAttachment(protocol_combo, -6);
        lblNewLabel_4.setLayoutData(fd_lblNewLabel_4);

        lblNewLabel_5 = new Label(shell, SWT.WRAP | SWT.RIGHT);
        fd_btnAttack.top = new FormAttachment(lblNewLabel_5, 22);
        lblNewLabel_5.setAlignment(SWT.CENTER);
        lblNewLabel_5.setOrientation(SWT.RIGHT_TO_LEFT);
        FormData fd_lblNewLabel_5 = new FormData();
        fd_lblNewLabel_5.left = new FormAttachment(0, 91);
        fd_lblNewLabel_5.top = new FormAttachment(lblNewLabel_3, 0, SWT.TOP);
        fd_lblNewLabel_5.right = new FormAttachment(protocol_combo, -6);
        lblNewLabel_5.setLayoutData(fd_lblNewLabel_5);
        fd_lblNewLabel_5.width=100;
        lblNewLabel_5.setText("\u05D6\u05D4\u05D9\u05E8\u05D5\u05EA! \u05E9\u05D9\u05DE\u05D5\u05E9 \u05D1\u05E4\u05E8\u05D5\u05D8\u05D5\u05E7\u05D5\u05DC TCP \u05D9\u05D0\u05D8 \u05D0\u05EA \u05E7\u05E6\u05D1 \u05E9\u05DC\u05D9\u05D7\u05EA \u05D4\u05E4\u05E7\u05D8\u05D9\u05DD!");
        lblNewLabel_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
        lblNewLabel_5.setVisible(false);
        lblNewLabel_6 = new Label(shell,SWT.WRAP | SWT.RIGHT );
        lblNewLabel_6.setLayoutData(new FormData());
        lblNewLabel_6.setOrientation(SWT.RIGHT_TO_LEFT);
        lblNewLabel_6.setAlignment(SWT.RIGHT);

        m_bindingContext = initDataBindings();

    }
    protected DataBindingContext initDataBindings() {
        DataBindingContext bindingContext = new DataBindingContext();
        //
        return bindingContext;
    }
}

每次按下''Attack''按钮,并且shell.layout()功能激活,其中带有'0'(零)字符的文本字段会跳起来。怎么把它搞定?

0 个答案:

没有答案