2014-04-02 2 views
0

배열에 8 개의 값을 넣을 수 있지만 사용자가 입력 할 수 있도록 배열을 만드는 방법은 무엇입니까? JOptionPane을 사용하여 데이터를 배열에 입력하는 방법

내가 지금까지

    import javax.swing.JOptionPane; 

        public class Southside Report { 
         public static void main(String[] args) { 

         int FINAL MIN_STAFF = 7; 
         int total_staff = 0; 

         double[] num_students = 8; 
+0

에 코드에도 [시간 JAVA에 배열 한 값을 채우기 위해 사용자 입력] – nachokk

+0

의 중복 가능성을 컴파일 (http://stackoverflow.com/questions/18711496/user-input-to-populate-an-array-one-value-at-time-java) 귀하의 질문을 게시하기 전에 google을 검색하십시오. 이 질문은 귀하의 질문과 다른 질문 사이에 충분한 상관 관계가 있으므로 안전하게 제거 될 것입니다. 이것이 중복이 아니라고 생각한다면 어떻게 그리고 왜 설명해주십시오. 그래서 SO에 대한 기본 규칙은 일반적인 연구를 한 다음 게시하는 것입니다. –

답변

3

당신이 선언해야 무엇을 당신의 array 같이

double[] num_students = new double[8]; 

그리고 int FINAL MIN_STAFF = 7; 당신에 의해 JOptionPane를 사용하여 값을 할당 할 수 다음 FINAL int MIN_STAFF = 7;

해야한다 하는 중 :

int i=0; 
while(i<8){ 
    try{ 
     num_students[i]=Double.parseDouble(JOptionPane.showInputDialog("Enter Number:")); 
     i++; 
    } 
    catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "Please enter valid number"); 
    } 
} 
3

JOptionPane을 살펴보십시오. 옵션 창은 매우 사용자 지정할 수 있습니다. 게다가 당신의 코드는 컴파일되지 않는다. 나는 사용자가 하나의 대화 상자에서만 8 개의 텍스트를 입력하기를 원하며, 아래의 예와 같이 약간의 커스터마이징이 가능한 optionPane으로 할 수 있다고 생각한다.

import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class JOptionPaneTest { 

    public static final int OPTIONS = 8; 
    private List<JTextField> textfields = new ArrayList<>(OPTIONS); 

    private JPanel panel; 


    public JOptionPaneTest(){ 
     panel = new JPanel(); 

     for(int i =0;i< OPTIONS;i++){ 
      JTextField textfield = new JTextField(5); 
      textfields.add(textfield); 
      panel.add(new JLabel(Integer.toString(i+1)+": ")); 
      panel.add(textfield); 
     } 

    } 


    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
      JOptionPaneTest example = new JOptionPaneTest(); 
      int result = JOptionPane.showConfirmDialog(null, example.panel, 
        "Please Enter Values", JOptionPane.OK_CANCEL_OPTION); 
      if (result == JOptionPane.OK_OPTION) { 
      for(JTextField textfield : example.textfields){ 
       System.out.println(textfield.getText()); 
      } 

      } 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

출력 :

enter image description here

관련 문제