2013-07-08 66 views
0

我可是從文本框獲取整數。我爲我的GUI創建了一個基於SWT的外殼佈局。我在GUI中有一個文本框。用作GUI時,我想從文本框中獲取整數值。我發現文本框只有選項才能獲得對象或文本。有沒有選擇從文本框中獲取Integer?如果是這樣,我該怎麼做?使用的WindowBuilder建立GUI我的應用程序的WindowBuilder

我從的WindowBuilder的源擁有的代碼如下:

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); 

     } 
    }); 
    } 


} 
+1

爲什麼土特產品不只是分析文本,以「整」? – mabbas

回答

1

你將不得不從String解析它:

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

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

請注意,這可能會引發一個NumberFormatException如果該文本不能被轉換爲整數。

+0

感謝您的回覆! – user2358330

+0

@ user2358330不客氣。請接受並提出我的回答。 – Baz

+0

我現在有疑問。是否有可能從windowbuilder源代碼中的其他類調用方法? – user2358330

相關問題