簡體   English   中英

我希望圖形用戶界面接受整數

[英]I want my graphical user interface to accept an integer

您好,我的工作遇到麻煩。 我希望圖形用戶界面能夠以“插入名稱編號”的形式填充一條語句。

例如,“ Insert Whiz 105 ”這樣的命令

我已經對Java進行了編程,以便在用戶輸入中以及字符串中使用“插入”字詞。 但是,我不知道如何使Java在一條語句中同時檢測字符串和整數。

當用戶在GUI中同時輸入String和Integer時,如何創建一個允許我將其分開的語句? 例如,我沒有為String和Integers創建兩個輸入,而是使用一個輸入來引用這兩個輸入。

這是我的代碼,請根據您的回答。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
   This class is used to demonstrate
   the operations in the LinkedList1 class.
*/

public class LinkedList1Demo extends JFrame
{    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private LinkedList<String> names;


    private JTextArea  listView;
    private JTextField cmdTextField;
    private JTextField resultTextField;

    public LinkedList1Demo()
    {
       names = new LinkedList<String>(); 
       listView = new JTextArea();
       cmdTextField = new JTextField();
       resultTextField = new JTextField();       

       // Create a panel and label for result field
       JPanel resultPanel = new JPanel(new GridLayout(1,2));
       resultPanel.add(new JLabel("Command Result"));
       resultPanel.add(resultTextField);
       resultTextField.setEditable(false);
       add(resultPanel, BorderLayout.PAGE_START);

       // Put the textArea in the center of the frame
       add(listView);
       listView.setEditable(false);
       listView.setBackground(Color.WHITE);
       listView.setPreferredSize(new Dimension(200, 25));



       // Create a panel and label for the command text field
       JPanel cmdPanel = new JPanel(new GridLayout(1,2));
       cmdPanel.add(new JLabel("Command Name:"));
       cmdPanel.add(cmdTextField);
       add(cmdPanel, BorderLayout.PAGE_END);  
       cmdTextField.addActionListener(new CmdTextListener());
       cmdPanel.setPreferredSize(new Dimension(200, 25));

       // Set up the frame
       setTitle("Linked List Demo");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       pack();
       setVisible(true);       
    }

    /**
       Private class that responds to the command that 
       the user types into the command entry text field.
    */

    private class CmdTextListener
            implements ActionListener
    {            
        public void actionPerformed(ActionEvent evt)
        {
            String cmdText = cmdTextField.getText();

            Scanner sc = new Scanner(cmdText);

            String cmd = sc.next();

            if (cmd.equals("insert"))
            {
                if (sc.hasNextInt())
                {
                    // add index element
                    int index = sc.nextInt();
                    String element = sc.next();
                    names.add(index, element);                
                }
                else
                {  
                    // add element
                    String element = sc.next();
                    names.add(element);                
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("remove"))
            {
                if (sc.hasNextInt())
                {
                    // remove index
                    int index = sc.nextInt();
                    Object obj = names.remove(index);
                    String res = obj.toString();
                    resultTextField.setText(res);              
                }
                else
                {
                    // remove element
                    String element = sc.next();
                    boolean res = names.remove(element);
                          String resText = String.valueOf(res);
                    resultTextField.setText(resText);
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("isempty"))
            {
                    String resText = String.valueOf(names.isEmpty());
                resultTextField.setText(resText);
                return;
            }
            if (cmd.equals("size"))
            {
                   String resText = String.valueOf(names.size());
               resultTextField.setText(resText);
               return;
            }

        }
    }

    /**
       The main method creates an instance of the 
       LinkedList1Demo class which causes it to 
       display its window.
    */

    public static void main(String [ ] args)
    {
        new LinkedList1Demo();

    }

}

我想請您更好地解釋您的要求。 但是,如果您確定只得到一串字符串和一個整數,那么一個快速的主意是通過使用標記器或最好使用split方法來分隔字符串。

然后,您遍歷每個單詞並嘗試使用“ Integer.parseInt()”方法。 如果引發NumberFormatException,則它是一個字符串,應將其附加到字符串對象。 否則,它是一個整數,您可以這樣威脅它。

使用String方法split拆分String。

String input = /* pull from the text box */.
String[] tokens = input.split("\\s");
if (tokens.length == 3)
{
     String command = tokens[0];
     String intValue= tokens[2];
}

您可以嘗試如下操作:

String cmd = sc.next();

try {

    int number = Integer.parseInt(cmd);

} catch (NumberFormatException e) {
     if (cmd.equals("insert"))
            {
                if (sc.hasNextInt())
                {
                    // add index element
                    int index = sc.nextInt();
                    String element = sc.next();
                    names.add(index, element);                
                }
                else
                {  
                    // add element
                    String element = sc.next();
                    names.add(element);                
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("remove"))
            {
                if (sc.hasNextInt())
                {
                    // remove index
                    int index = sc.nextInt();
                    Object obj = names.remove(index);
                    String res = obj.toString();
                    resultTextField.setText(res);              
                }
                else
                {
                    // remove element
                    String element = sc.next();
                    boolean res = names.remove(element);
                          String resText = String.valueOf(res);
                    resultTextField.setText(resText);
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("isempty"))
            {
                    String resText = String.valueOf(names.isEmpty());
                resultTextField.setText(resText);
                return;
            }
            if (cmd.equals("size"))
            {
                   String resText = String.valueOf(names.size());
               resultTextField.setText(resText);
               return;
            }
}

這將嘗試將輸入解析為整數。 如果是數字,則它將成功,否則將失敗,並輸入catch塊並作為字符串進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM