2015-04-07 82 views
0

我想将两个数组的内容添加到两个TextFields。当你运行程序时,问题是当我重新绘制窗口时,我尝试显示的当前TextField(第70 - 83行),它们只显示它们数组中的最后一项。有什么办法增加在堆叠列表中的所有项目将数组添加到JTextField

这是我的第一类ClassNameSorting 代码(一个ontop的另一个。):

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.*; 
public class ClassNameSorting extends JFrame { 

    JTextField studentNameInputFirst, studentNameInputLast, studentNamesEneteredLast, studentNamesEnteredFirst; 
    JButton nextName, sort, backToStart; 
    JLabel firstName, lastName, classList; 
    String disp = ""; 
    ArrayList<String> studentNameFirst = new ArrayList<String>(); 
    ArrayList<String> studentNameLast = new ArrayList<String>(); 
    ArrayList<String> sortedStudentNameFirst = new ArrayList<String>(); 
    ArrayList<String> savedUnsortedStudentNameLast = new ArrayList<String>(); 
    Container container = getContentPane(); 

    public ClassNameSorting() { 
     container.setLayout(new FlowLayout()); 

     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: "); 

     nextName.setPreferredSize(new Dimension(110, 20)); 
     sort.setPreferredSize(new Dimension(110, 20)); 

     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(262, 120); 
     setVisible(true); 
    } 
    private class nextNameListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      studentNameFirst.add(studentNameInputFirst.getText()); 
      studentNameLast.add(studentNameInputLast.getText()); 
      studentNameInputLast.setText(null); 
      studentNameInputFirst.setText(null); 
     } 
    } 
    private class sortListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      savedUnsortedStudentNameLast = new ArrayList<String>(studentNameLast); 
      Collections.sort(studentNameLast); 

      int totalSizeOfArray = studentNameLast.size(); 
      for(int i = 0; i < totalSizeOfArray; i++){ 
       boolean containsYorN = false; 
       String tempElementForContains = studentNameLast.get(i); 
       String tempElementFromStudentNameLast = savedUnsortedStudentNameLast.get(i); 
       containsYorN = savedUnsortedStudentNameLast.contains(tempElementForContains); 

       if(containsYorN == true){ 
        int tempIndexPos = savedUnsortedStudentNameLast.indexOf(tempElementForContains); 
        String tempIndexElement = studentNameFirst.get(tempIndexPos); 
        sortedStudentNameFirst.add(i, tempIndexElement); 
       }  
      } 
      studentNamesEneteredLast = new JTextField(); 
      studentNamesEnteredFirst = new JTextField(); 
      for(int i = 0; i < totalSizeOfArray; i++){ 
       studentNamesEneteredLast.setText(studentNameLast.get(i)); 
      } 
      for(int i = 0; i < totalSizeOfArray; i++){ 
       studentNamesEnteredFirst.setText(sortedStudentNameFirst.get(i)); 
      } 
      studentNamesEneteredLast.setEditable(false); 
      studentNamesEnteredFirst.setEditable(false); 
      container.add(studentNamesEneteredLast); 
      container.add(studentNamesEnteredFirst); 
      setSize(262, 500); 
      revalidate(); 
     } 
    } 
} 

我第二类是:(DrawMainWindow)

import javax.swing.JFrame; 
public class DrawMainWindow { 
    public static void main(String[] args) { 
     ClassNameSorting drawGui = new ClassNameSorting(); 
     drawGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     drawGui.setLocationRelativeTo(null); 
    } 
} 

第一个数组是studentNameLast。我正在尝试将其添加到studentNamesEnteredLast文本字段中。第二个是sortedStudentNameFirst被添加到studentNamesEnteredFirst。

感谢您的帮助!

回答

0

能够切换到GridLayout,然后添加每个姓氏,然后一起名字。

代码:

container.setLayout(new GridLayout(totalSizeOfArray+3,2)); 
    for(int i = 0; i < totalSizeOfArray; i++){ 
     studentNamesEnteredLast = new JTextField(); 
     studentNamesEnteredFirst = new JTextField(); 
     studentNamesEnteredFirst.setEditable(false); 
     studentNamesEnteredLast.setEditable(false); 
     studentNamesEnteredLast.setText(studentNameLast.get(i)); 
     studentNamesEnteredFirst.setText(sortedStudentNameFirst.get(i)); 
     container.add(studentNamesEnteredLast); 
     container.add(studentNamesEnteredFirst); 
    } 

例子:

enter image description here

2
for(int i = 0; i < totalSizeOfArray; i++){ 
    studentNamesEneteredLast.setText(studentNameLast.get(i)); 
} 

JTextField中的文本将只反映列表的最后一项。如果你想添加整个列表,创建列表选择路线,并设置JTextField中的文本与字符串(您不妨让他们通过一些字符分隔 - 下面使用逗号)

StringBuilder sb = new StringBuilder(); 
for(int i = 0; i < totalSizeOfArray; i++){ 
    sb.append(studentNameLast.get(i)).append(",");//comma delim 
} 
studentNamesEneteredLast.setText(sb.toString()); 

不完全确定你在做什么,但JList或JTable可能更适合于显示列表信息

+0

感谢您的答复!我在寻找的内容与此屏幕截图中的内容类似:http://i.imgur.com/hT8F3Rj.png 当我单击排序时,它将并排放入姓氏和名字。 –

+0

我不确定我完全清楚你想要什么,但是也许JTable是你的:https://docs.oracle.com/javase/tutorial/uiswing/components/table.html – copeg

+0

我是实际上能够做到这一点没有。我只是在循环中创建了新的TextField。谢谢你的帮助! (查看我的回答) –