简体   繁体   English

如何在GUI中向数组添加值

[英]How do i add values to an array in a GUI

I have a GUI that has a input value field at the top with a button to add the value to the array. 我有一个GUI,它的顶部有一个输入值字段,并带有一个将值添加到数组的按钮。 The array is below it in the form of 10 empty jTextboxes labeled 0-9. 数组以10个空的jTextbox(标记为0-9)的形式在其下方。 When the user enters a number in the value text box and clicks add the number is put into the textfields below. 当用户在值文本框中输入数字并单击时,会将数字添加到下面的文本字段中。 Once the array is populated I will find the minimum and maximum values. 填充数组后,我将找到最小值和最大值。 I have no problem with the logic behind the sort, the find minimum, find maximum values, but I am having a hard time with the logic behind loading the array, I tried to use a couple of different tactics. 我对排序,查找最小值,查找最大值背后的逻辑没有问题,但是在加载数组背后的逻辑上我很难受,我尝试使用几种不同的策略。

//This is my start of the on_click event for the add button

jTextboxValue.getText();  //I know that this gets me the text from the box
jTextboxvalueArray0.setText();  // I know this is how I set the text in the various boxes

My question is this, what is the most concise way to instantiate and load this array, if it were a console app I would do something like: 我的问题是,实例化和加载此数组的最简洁方法是什么,如果它是控制台应用程序,我将执行以下操作:

Scanner input = new Scanner(System.in);

int array[] = new int[10];

System.out.println("Please enter your Values.");

       for (int i = 0 ; i < array.length; i++ ) {
       int next = input.nextInt();

Now when it comes to doing this within a GUI I am stumped, and this has been a recurring source of confusion for me for some time now. 现在,当涉及到在GUI中执行此操作时,我感到很困惑,这在一段时间以来一直让我感到困惑。 I have read the oracle tutorials, a couple java books but nothing has clicked yet. 我已经阅读了oracle教程,还有几本Java书籍,但是还没有点击。 How do I transfer my logic from working in the console to working in a GUI? 如何将逻辑从在控制台中转换为在GUI中转换?

Suggestions: 建议:

  1. Consider using an array of JTextFields (what is a JTextBox anyway?), say called textFields. 考虑使用一个称为TextFields的JTextFields数组(无论如何是JTextBox?)。
  2. Give your gui an int index variable that is initialized to 0. 给您的gui一个初始化为0的int索引变量。
  3. When the user enters a number into the entry field and presses the correct button, get the text from the input JTextField via getText() , put it into a local String variable, 当用户在输入字段中输入数字并按下正确的按钮时,可通过getText()从输入JTextField中获取文本,并将其放入本地String变量中,
  4. then pass that variable into the setText(text) method of the index item of the textFields array, textFields[index].setTexzt(text) . 然后将该变量传递到textFields数组的索引项textFields[index].setTexzt(text)setText(text)方法中。
  5. Increment the index variable, index++ . 增加索引变量index++

Note: why not simply let the user enter all their numbers directly into each JTextField of the textFields array, and then have them press the evaluate JButton when done? 注意:为什么不简单地让用户将所有数字直接输入到textFields数组的每个JTextField中,然后让他们在完成时按评估JButton? Why try to stuff the JTextFields yourself as state is your goal? 为什么要自己填充JTextField,因为状态是您的目标? That seems kind of a kludge to me. 对我来说,这似乎是一种错误。

Have a single JTextField as the primary user input. 有一个JTextField作为主要用户输入。 Have a JButton next to it that acts as the "add" mechanism. 在它旁边有一个JButton ,它充当“添加”机制。

When clicking add, add the value to a JList instead. 单击添加时,将值添加到JList Don't focus so much on the array, as it's just getting in your way. 不要过多地关注数组,因为它只会妨碍您的工作。 You can manage the JList 's model to determine when you have what ever limit of numbers you need/want (if you need/want one). 您可以管理JList的模型来确定何时需要/想要的数量限制(如果需要/想要一个)。

See How to Use Lists for more details 有关更多详细信息,请参见如何使用列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM