存储多个文本字段输入。

huangapple go评论69阅读模式
英文:

Storing multiple textfield inputs

问题

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

public class GUI implements ActionListener {

    private static JLabel orderLabel;
    private static JTextField orderText;
    private static JButton confirmButton;

    public GUI(){
        JFrame window = new JFrame();
        JPanel panel = new JPanel();
        window.setSize(350, 200);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.add(panel);
        window.setTitle("Place your order");
        panel.setLayout(null);

        orderLabel = new JLabel("Your order:");
        orderLabel.setBounds(10, 20, 80, 25);
        panel.add(orderLabel);

        orderText = new JTextField();
        orderText.setBounds(100, 22, 165, 25);
        orderText.addActionListener(this);
        panel.add(orderText);

        confirmButton = new JButton("Confirm order");
        confirmButton.setBounds(90, 100, 170, 30);
        confirmButton.addActionListener(this);
        panel.add(confirmButton);

        window.setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        List<String> list = new ArrayList<String>();
        String input = orderText.getText();
        String temp = input.next();
    }
}
英文:

I've been working on project trying to store inputs inside an array via user inputs on the GUI.
The issue right now is I am unable to find something similar to the Scanner method's "input.next".

Thanks for any help that comes by

Here's what the GUI class that gets executed from my Main look like

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class GUI implements ActionListener {
//Private the variables to make them accessible to other methods
private static JLabel orderLabel;
private static JTextField orderText;
private static JButton confirmButton;
public  GUI(){
JFrame window = new JFrame();
JPanel panel = new JPanel();
window.setSize(350, 200);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.add(panel);
window.setTitle(&quot;Place your order&quot;);
panel.setLayout(null);
//Creating label for the order text field:
orderLabel = new JLabel(&quot;Your order:&quot;); //Creates the label memory
orderLabel.setBounds(10, 20, 80, 25); //Creates bounds for the label
panel.add(orderLabel); //Adds the label to the window
//Creating the text field
orderText = new JTextField(); //Creates text field memory
orderText.setBounds(100, 22, 165, 25); //Creates text field bounds
orderText.addActionListener(this);
panel.add(orderText); //Adds the text field to the window
//Creating the button to confirm order
confirmButton = new JButton(&quot;Confirm order&quot;);
confirmButton.setBounds(90, 100, 170, 30);
confirmButton.addActionListener(this);//Links the button to the action listener method inside this file
panel.add(confirmButton);
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
String input = orderText.getText();
String temp = input.next();
}
}

答案1

得分: 0

如果您想对JTextField的内容使用Scanner#next()方法,只需使用Scanner对象来实现,例如:

List<String> list = new ArrayList<>();
Scanner input = new Scanner(orderText.getText());
while (input.hasNext()) {
    list.add(input.next());
}

当然,您希望在JTextField中提供的所有内容都以空格为分隔符,例如:

Bread Milk Sugar Coffee

这将显示为类似以下的列表:

Bread
Milk
Sugar
Coffee

当然,这可能还不够好,因为许多所需的项可能包含多个单词,例如:

French Fries Whole Wheat Bread 2% Milk

这将显示为类似以下的列表:

French
Fries
Whole
Wheat
Bread
2%
Milk

这不是真正想要的结果。在这种特殊情况下,您可以使用Scanner#useDelimiter()方法来指定在将所需项键入JTextField时要使用的分隔符。因此,如果您决定使用分号(;)作为键入的项之间的分隔符,并且您输入了以下内容到JTextField:

French Fries; Whole Wheat Bread; 2% Milk

列表将更像是:

French Fries
Whole Wheat Bread
2% Milk

代码将如下所示:

List<String> list = new ArrayList<>();
Scanner input = new Scanner(orderText.getText());
input.useDelimiter("\\s{0,};\\s{0,}");
while (input.hasNext()) {
    list.add(input.next());
}

要将此放入您的(在某种程度上可运行的)应用程序中:

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

public class GUI extends JFrame implements ActionListener {
    // ...
    // (以下是您提供的完整代码,没有翻译)
}
英文:

If you want to use the Scanner#next() method against the contents of a JTextField then just use a Scanner object to do that, for example:

List&lt;String&gt; list = new ArrayList&lt;&gt;();
Scanner input = new Scanner(orderText.getText());
while (input.hasNext()) {
list.add(input.next());
}

Of course you would want everything supplied within the JTextField to be delimited with a whitespace, for example:

Bread Milk Sugar Coffee

which would play out as a list looking something like this:

Bread
Milk
Sugar
Coffee

This of course may not be good enough since a lot of required items would contain more than one word, for example:

French Fries Whole Wheat Bread 2% Milk

which would play out as a list looking something like this:

French
Fries
Whole
Wheat
Bread
2%
Milk

Not what is really wanted. In this particular case you can use the Scanner#useDelimiter() method to specify the delimiter you plan to use when typing the desired items into the JTextField. So, if you decided to use a semicolon (;) as a delimiter between typed in items and you entered into the JTextField:

French Fries; Whole Wheat Bread; 2% Milk

The list would look more like:

French Fries
Whole Wheat Bread
2% Milk

The code would be:

List&lt;String&gt; list = new ArrayList&lt;&gt;();
Scanner input = new Scanner(orderText.getText());
input.useDelimiter(&quot;\\s{0,};\\s{0,}&quot;);
while (input.hasNext()) {
list.add(input.next());
}

To put this into your (sort of) runnable application:

import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
//Private the variables to make them accessible to other methods
private static JLabel orderLabel;
private static JTextField orderText;
private static JButton confirmButton;
public GUI() {
initializeForm();
}
private void initializeForm() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(350, 200);
setAlwaysOnTop(true);
setTitle(&quot;Place your order&quot;);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(null);
//Creating label for the order text field:
orderLabel = new JLabel(&quot;Your order:&quot;); //Creates the label memory
orderLabel.setBounds(10, 20, 80, 25); //Creates bounds for the label
panel.add(orderLabel); //Adds the label to the window
//Creating the text field
orderText = new JTextField(); //Creates text field memory
orderText.setActionCommand(&quot;orderText&quot;);
orderText.setBounds(100, 22, 165, 25); //Creates text field bounds
orderText.addActionListener(this);
panel.add(orderText); //Adds the text field to the window
//Creating the button to confirm order
confirmButton = new JButton(&quot;Confirm order&quot;);
confirmButton.setActionCommand(&quot;confirmButton&quot;);
confirmButton.setBounds(90, 100, 170, 30);
confirmButton.addActionListener(this);//Links the button to the action listener method inside this file
panel.add(confirmButton);
add(panel);
}
public static void main(String[] args) {
new GUI().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String textToTokenize = orderText.getText().trim();
// TRY to determine the delimiter used within the JTextField entry
String delimiter = textToTokenize.replaceAll(&quot;[^:;&#39;\&quot;\\|\\\\~!@#$%\\^&amp;\\&quot;
+ &quot;*-_+=&lt;&gt;.?/]&quot;, &quot;&quot;).trim();        
if (delimiter.length() == 0) {
delimiter = &quot; &quot;;
}
else {
delimiter = delimiter.substring(0, 1);
}
if (!textToTokenize.isEmpty() &amp;&amp; 
(e.getActionCommand().equals(&quot;confirmButton&quot;) || 
e.getActionCommand().equals(&quot;orderText&quot;))) {
List&lt;String&gt; shoppingList = new ArrayList&lt;&gt;();
// If you want to use Scanner&#39;s next() method then 
// just use Scanner to do it...
Scanner input = new Scanner(textToTokenize);
input.useDelimiter(&quot;\\s{0,}&quot; + delimiter + &quot;\\s{0,}&quot;);
while (input.hasNext()) {
shoppingList.add(input.next());
}
// Test...Display list in console window:
JOptionPane.showMessageDialog(this, &quot;&lt;html&gt;Supplied shopping list items are:&quot;
+ &quot;&lt;br&gt;&lt;br&gt;&lt;center&gt;&lt;font color=blue&gt;&quot; + shoppingList.toString()
.replaceAll(&quot;[\\[\\]]&quot;, &quot;&quot;) + &quot;&lt;/font&gt;&lt;/center&gt;&lt;br&gt;&lt;/html&gt;&quot;, 
&quot;Supplied List...&quot;, JOptionPane.INFORMATION_MESSAGE);
}
orderText.setText(&quot;&quot;);
orderText.requestFocus();
}
}

huangapple
  • 本文由 发表于 2020年9月16日 05:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63910193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定