gpt4 book ai didi

java - JTextField 和 getActionCommand 存储在用户扩展数组中

转载 作者:行者123 更新时间:2023-12-02 04:48:12 25 4
gpt4 key购买 nike

我使用此 GUI 程序的主要目的是让用户在名字框中输入学生的名字,在姓氏框中输入相应的姓氏,然后单击“保存”。点击“保存”会将名字和姓氏保存到各自的数组中。用户输入名称后,将单击“排序”。然后,这将按字母顺序对名称进行排序,然后重新绘制 GUI 以显示新排序的名称数组的结果。我遇到的主要问题是,当我输入名称时,我会收到一长串错误列表(如下所示)。我也不知道如何重新绘制 GUI 以在列表中显示名称数组。任何帮助将不胜感激,谢谢!

这是我当前的代码。 (变量名称很长,但我讨厌创建变量名称然后忘记它们的用途。) 导入java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.*;

public class main extends JFrame {

JTextField studentNameInputFirst, studentNameInputLast, studentNamesEnetered;
JButton nextName, sort;
JLabel firstName, lastName;
String disp = "";
int totalNumberOfStudents = 0;
String[] studentFirstNames = new String[totalNumberOfStudents];
String[] studentLastNames = new String[totalNumberOfStudents];

public main() {
Container container = getContentPane();
container.setLayout(new FlowLayout());

studentNamesEnetered = new JTextField();
studentNameInputFirst = new JTextField(15);
studentNameInputLast = new JTextField(15);
nextName = new JButton("Save");
sort = new JButton("Sort");
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");

container.add(firstName);
container.add(studentNameInputFirst);
container.add(lastName);
container.add(studentNameInputLast);
container.add(nextName);
container.add(sort);

nextName.addActionListener(new nextNameListener());
sort.addActionListener(new sortListener());

setSize(325, 120);
setVisible(true);
}

private class nextNameListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
totalNumberOfStudents++;
studentFirstNames[totalNumberOfStudents] = studentNameInputFirst.getText();
studentLastNames[totalNumberOfStudents] = studentNameInputLast.getText();
studentNameInputLast = null;
studentNameInputFirst = null;
}
}
private class sortListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}

public static void main(String[] args) {
main drawGui = new main();
drawGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

这是我在运行上述代码时遇到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at main$nextNameListener.actionPerformed(main.java:48)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

编辑:使用提供的建议使用 ArrayList 而不是普通数组的答案后,我不再收到错误。我还对程序的命名进行了更改,以遵循 Java 命名约定,并且为了我自己清楚地了解程序的用途。已对第 13、14、45 和 46 行进行了更改。感谢您的帮助!

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

public class ClassNameSorting extends JFrame {

JTextField studentNameInputFirst, studentNameInputLast, studentNamesEnetered;
JButton nextName, sort;
JLabel firstName, lastName;
String disp = "";
ArrayList<String> studentNameFirst = new ArrayList<String>();
ArrayList<String> studentNameLast = new ArrayList<String>();


public ClassNameSorting() {
Container container = getContentPane();
container.setLayout(new FlowLayout());

studentNamesEnetered = new JTextField();
studentNameInputFirst = new JTextField(15);
studentNameInputLast = new JTextField(15);
nextName = new JButton("Save");
sort = new JButton("Sort");
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");

container.add(firstName);
container.add(studentNameInputFirst);
container.add(lastName);
container.add(studentNameInputLast);
container.add(nextName);
container.add(sort);

nextName.addActionListener(new nextNameListener());
sort.addActionListener(new sortListener());

setSize(325, 120);
setVisible(true);
}

private class nextNameListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
studentNameFirst.add(studentNameInputFirst.getText());
studentNameLast.add(studentNameInputLast.getText());
studentNameInputLast = null;
studentNameInputFirst = null;
}
}
private class sortListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}

public static void main(String[] args) {
ClassNameSorting drawGui = new ClassNameSorting();
drawGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

最佳答案

int totalNumberOfStudents = 0;
String[] studentFirstNames = new String[totalNumberOfStudents];
String[] studentLastNames = new String[totalNumberOfStudents];

您的数组大小为 0。您需要使用您希望添加到数组中的条目数来分配数组。

不要使用数组,因为您不知道其大小应该是多少。而是使用 ArrayList

关于java - JTextField 和 getActionCommand 存储在用户扩展数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29494627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com