0

Im using windowbuilder to build GUI for my application. I have created a SWT based shell layout for my GUI. I have a textbox in the GUI. I want to get Integer values from the textbox when used as the GUI. I see that the textbox has option only to get Object or Text. Is there any option to get Integer from textbox? If so, how do I do it?

The code I have from the Source of windowbuilder is as follows:

import org.eclipse.swt.SWT;
import otf.EnterLeaveHandler;
public class test {

    protected Shell shell;
    private Text text;
    private final FormToolkit formToolkit = new FormToolkit(
            Display.getDefault());

    public static void main(String[] args) {
        try {
            test window = new test();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    protected void createContents() {
        shell = new Shell();
        shell.setSize(545, 419);
        shell.setText("SWT Application");
        shell.setLayout(null);

        Button btnNewButton = new Button(shell, SWT.NONE);
        btnNewButton.setBounds(213, 243, 75, 25);
        btnNewButton.setText("Ok");

        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setBounds(190, 87, 137, 15);
        lblNewLabel.setText("Enter function name");

        text = new Text(shell, SWT.BORDER);
        text.setBounds(213, 129, 76, 21);


    final Label lblNewLabel_1 = formToolkit.createLabel(shell, "New Label", SWT.NONE);
    lblNewLabel_1.setBounds(72, 184, 55, 15);

    final EnterLeaveHandler ne = new EnterLeaveHandler();

    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDoubleClick(MouseEvent e) {

            Integer getvalue = Integer.parseInt(text.getText());
            ne.cpuid_filter(getvalue);

        }
    });
    }


 }
4

1 回答 1

1

您必须从以下内容解析它String

try
{
    int actualValue = Integer.parseInt(text.getText());

    System.out.println(actualValue);
}
catch (NumberFormatException e)
{
    System.out.println("Not a number");
}

请注意,NumberFormatException如果无法将文本转换为整数,这可能会引发 a。

于 2013-07-08T12:05:37.450 回答