[英]How do I get user input in a GUI using text field?
我正在一個需要GUI的項目中。 如何使用JTextField從用戶獲取兩個給定字符串的輸入並顯示輸出?
我的代碼:
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
String s ;
System.out.println("Enter the Barcode:");
s = user_input.next();
String z;
System.out.println("Enter the Serial Number:");
z = user_input.next();
Random r = new Random();
int low = 1;
int high = 21;// Set the number of random methods
int rand = r.nextInt(high - (low - 1)) + low;
}
不要將GUI與控制台混合使用。
如果您不知道如何創建GUI,也沒有Swing的實際經驗,並且想學習,您將需要學習事件驅動的模型以及事件和偵聽器的工作原理,因為它們是GUI編程的基礎。 您可以從Swing教程開始
如果您只是在運行控制台程序,並且想從圖形界面獲取用戶輸入,也許您應該研究一下JOptionPane
類的簡單內容。 您可以查看JOptionPane API 。 你可以做類似的事情
String input = JOptionPane.showInputDialog( null, "What's your answer to this question?"); System.out.println(input);
您可以在如何制作對話框中查看如何制作更復雜的對話框
您可以使用Netbeans IDE及其GUI構建器空間,並拖放所需的每個組件,例如按鈕,標簽,文本字段,列表,組合框,...
正如你看到的:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.