这个GUI程序的主要目的是让用户在第一个名字框中输入学生的名字,在姓氏框中输入相应的姓氏,然后点击保存。然后,点击保存会将名字和姓氏保存到各自的数组中。用户完成输入名称后,他们将点击排序。然后,这将按字母顺序对名称进行排序,然后重新绘制GUI以显示新排序的名称数组的结果。我的主要问题是当我输入一个名字时,我会得到一个错误的清单(见下文)。我也不知道如何重绘GUI以显示列表中的名称数组。任何帮助将不胜感激,谢谢!
这是我目前的代码。 (变量名很长,但我讨厌变量名,然后忘记它们的用途。) import 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);
}
}
答案 0 :(得分:2)
int totalNumberOfStudents = 0;
String[] studentFirstNames = new String[totalNumberOfStudents];
String[] studentLastNames = new String[totalNumberOfStudents];
您的数组大小为0.您需要为数组分配当前要添加到数组中的条目数。
不要使用数组,因为你不知道它的大小。而是使用ArrayList
。