2013-05-26 2 views
0

java Window Builder 응용 프로그램에 문제가 있습니다. 다른 클래스의 배열 변수에 이름 (textField)을 저장하려면이 애플리케이션을 만들어야합니다.스윙 - 배열의 텍스트 필드 저장

그래서 새 이름 버튼을 누르면 textField에서 이름을 전달해야하며 다른 문자를 소개하고 다음 새 위치로 배열에 전달하는 등의 작업을 수행 할 수 있습니다. 그 후 배열의 이름을 textArea에 표시해야합니다.

문제는 내가 textField 텍스트를 배열로 전달하는 대신 버튼을 누르면 스윙이 많이 발생한다는 것입니다. String에서 String으로 n 변수를 변경하면 오류가 발생하지 않지만 하나의 이름 만 저장합니다.

데이터 클래스

public class data { 

String[] n; 

public void saveData(String na, int counter){ 
    n[counter]=na; 
} 

public void showData(int counter){ 
    for (int i = 0; i < counter; i++) { 
     System.out.println(n[counter]); 
    } 
} 

} 

창 클래스 누군가가 저를 도와 주실 수

import java.awt.EventQueue; 
public class dataWindow { 

// Data OBJECT 
data d = new data(); 
private JFrame frame; 
private JTextField textName; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       dataWindow window = new dataWindow(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public dataWindow() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JLabel lblName = new JLabel("Name"); 

    textName = new JTextField(); 
    textName.setColumns(10); 

    JButton btnNewName = new JButton("New Name"); 
    btnNewName.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String n; 
      int c=0; 
      // HERE I GET THE TEXT AND PASS IT TO THE ARRAY IN DATA CLASS 
      // here I think that I get the error when I press the "New Name" button 
      n=textName.getText(); 
      d.saveData(n, c); 

      textName.setText(""); 
      c++; 
     } 
    }); 
    GroupLayout groupLayout = new GroupLayout(frame.getContentPane()); 
    groupLayout.setHorizontalGroup(
     groupLayout.createParallelGroup(Alignment.LEADING) 
      .addGroup(groupLayout.createSequentialGroup() 
       .addGap(44) 
       .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
        .addComponent(btnNewName) 
        .addGroup(groupLayout.createSequentialGroup() 
         .addComponent(lblName) 
         .addGap(18) 
         .addComponent(textName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(259, Short.MAX_VALUE)) 
    ); 
    groupLayout.setVerticalGroup(
     groupLayout.createParallelGroup(Alignment.LEADING) 
      .addGroup(groupLayout.createSequentialGroup() 
       .addGap(72) 
       .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) 
        .addComponent(lblName) 
        .addComponent(textName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(ComponentPlacement.RELATED, 114, Short.MAX_VALUE) 
       .addComponent(btnNewName) 
       .addGap(33)) 
    ); 
    frame.getContentPane().setLayout(groupLayout); 
} 

} 

? 미리 감사드립니다.

답변

0

배열을 초기화하지 않았기 때문입니다.

String[] n = new String[x]; // x is the number of names you want to store. 

저장하려는 이름의 수를 지정하지 않으려면 목록을 사용해야합니다.

List<String> n = new ArrayList<String>(); 

아마도 이것은 더 나은 구현 일 것입니다.

관련 문제