2012-05-22 4 views
0

첫 번째 게시물입니다. 저는 전임 과학 학위 소지자입니다.Java - JFrame의 내용이 전혀 표시되지 않습니다.

나는 임무를위한 기초적인 GUI를 만들었고, 내가 만든 JFrames 중 하나 인 나를 볼 수없는 몇 가지 이유 때문에 등록 클래스의 인스턴스가 호출 될 때 완전히 공백으로 표시된다. 로그인 클래스의 등록 단추 조치 수신기에서 ...

또한 Main 메소드를 포함하고 로그인 클래스를 호출하는 개별 메인 클래스가 있습니다. 로그인 클래스 JFrame 잘 작동하고 언급 한 바와 같이, 로그인 클래스 '등록 단추 동작 수신기 내에서 호출 될 때 등록 클래스를 사용하여 문제가 발생합니다. 프로그램의 다른 모든 JFrames도 잘 작동합니다.

나는 가장 기본적인 형식으로 줄이려고 시도했지만 여전히 무색의 빈 JFrame을 제외하고는 아무것도 표시하지 않지만 메인에서 직접 Register Class를 호출하려고 시도했지만 동일한 문제가 있습니다.

다음은 코드입니다 (미완료이지만 그대로 작동). 내 조잡함을 사과하지만 나는 완전한 초보자입니다.

누구에게 잘못 되었나요?

미리 감사드립니다.

package guitest; 

    import java.awt.Color; 
    import java.awt.Component; 
    import java.awt.FlowLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.BufferedWriter; 
    import java.io.FileWriter; 
    import java.awt.event.*; 
    import java.io.*; 
    import javax.swing.*; 

    public class Register extends JFrame{ 

     JButton regSubmit = new JButton("Submit"); 

     JTextField email = new JTextField(); 
     JTextField name = new JTextField(); 
     JTextField Address1 = new JTextField(); 
     JTextField Address2 = new JTextField(); 

     JPasswordField password1 = new JPasswordField(); 
     JPasswordField password2 = new JPasswordField(); 

     String nameTxt = email.getText(); 
     String passTxt = password1.getText(); 
     String info = ""; 

     FlowLayout layout1 = new FlowLayout();  

     public void Reg(){ 
      this.setTitle("La Volpe Registration"); 
      this.setLayout(layout1);  
      this.add(Address1); 
      this.add(Address2); 
      this.add(email); 
      this.add(password1); 
      this.add(password2); 
      this.add(name); 
      this.add(regSubmit); 
      this.getContentPane().setBackground(Color.green); 
      this.setSize(370, 160); 

      regSubmit.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent evt) { 
        regSubmitActionPerformed(evt); 
       } 

       private void regSubmitActionPerformed(java.awt.event.ActionEvent evt) { 

       String name = email.getText(); 
       String pass = password1.getText(); 
       String info = ""; 

       System.out.println("registering..."); 

       boolean eof; 

       try{ 
        // Create file 
        FileWriter file = new FileWriter("\\regdata.txt"); 
        BufferedWriter out = new BufferedWriter(file); 
        out.write("\n"+nameTxt+", "+passTxt); 
       } 

       catch (Exception e){ 
       } 
      } 
     }); 
     this.setVisible(true); 
    }  
} 

그리고 그것을 연결하는 클래스

...


package guitest; 

    import java.awt.Color; 
    import java.awt.Component; 
    import java.awt.FlowLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.BufferedWriter; 
    import java.io.FileWriter; 
    import java.awt.event.*; 
    import java.io.*; 
    import javax.swing.*; 

    /** 
    * @author david 
    */ 

    public class Login { 

    JFrame loginFrame = new JFrame(); 
    Register reg3 = new Register(); 

    JButton submit = new JButton("Submit"); 
    JButton clear = new JButton("Clear"); 
    JButton register = new JButton ("Register with Us"); 

    JPasswordField pass = new JPasswordField(20); 
    JTextField email = new JTextField(20); 
    JLabel em = new JLabel("Email Address: "); 
    JLabel pw = new JLabel("Password: "); 

    String pathname; 
    String line; 
    String [] records = new String [1000]; 
    int count = 0; 

    FlowLayout layout1 = new FlowLayout(); 

     public Login(){ 

      //Adds Email label and text field 
      loginFrame.add(em); 
      loginFrame.add(email); 

      //Adds password label and field 
      loginFrame.add(pw); 
      loginFrame.add(pass); 

      //Adds buttons 
      loginFrame.add(submit); 
      loginFrame.add(clear); 
      loginFrame.add(register); 

      loginFrame.getContentPane().setBackground(Color.green); 

      loginFrame.setLayout(layout1); 

      loginFrame.setSize(370, 160); 

      loginFrame.setTitle("La Volpe - Login"); 

      submit.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent evt) { 
        submitActionPerformed(evt);  
       }    

       private void submitActionPerformed(java.awt.event.ActionEvent evt){           
       String emailadd = email.getText(); 
       String password = pass.getText(); 
       pathname = "\\regdata.txt"; 

       boolean eof; 

       try{ 
        FileReader file = new FileReader(pathname); 
        BufferedReader buff = new BufferedReader(file); 

        eof = false; // set the eof boolean to false 

         while (!eof){ 
         line = buff.readLine(); 
         if (line == null){ // test to see if the end of file has been reached 
         eof = true; // if the end of file has been found set the eof Boolean to true 
         } 
         else{ 
         // end of file not reached so move the contents of the line to the records 
         //array 
         records[count] = line; 
         count ++; 
         System.out.println(line); // print out the new line input for error checking 
         } 
        }  
        buff.close(); 
       } 
       catch (IOException e){ 
        System.out.println("Error -- "+ e.toString()); 
       } 

       boolean notTrue = false; 

       for (int i = 0; i < count; i++) {  
        if ((!notTrue)&&((emailadd + "," + password).equals(records[i]))) { 
         FoodSelectionMain loggedIn = new FoodSelectionMain(); 
         loggedIn.setVisible(true); 
        } 
       } 
       if (!notTrue){ 
        JOptionPane.showInputDialog("Please check your login " 
        + "and try again. If you are a new user, please " 
        + "register by pressing the 'REGISTER' button"); 
       } 
      } 

     }); 

     // TODO add your handling code here: 


     clear.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       clearActionPerformed(evt); 
      } 

      public void clearActionPerformed(java.awt.event.ActionEvent evt){ 
       email.setText(null); 
       pass.setText(null); 
      } 

     }); 
     register.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       registerActionPerformed(evt); 
      } 

      public void registerActionPerformed(java.awt.event.ActionEvent evt){ 
       reg3.setVisible(true); 
        System.out.println("Register pressed"); 
      } 
     }); 
     loginFrame.setVisible(true); 
    } 
} 
+1

'SwingUtilities'에 대한 언급이없고'pack()'에 대한 호출이 없습니다. 나는 그것이 실패하지 않는 것에 놀랐다. 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –

+2

들여 쓰기와 공백을 매우 창의적으로 사용하면 좋겠지 만,이 독창성은 다른 사람들 (즉, 당신을 도우려는 사람들)이 코드를 쉽게 읽고 이해하는 것을 어렵게 만듭니다. 결국 낯선 사람들이 무료로 조언을 구하는 것이므로, 우리가 당신을 도울 수 없도록 약간의 노력을 기울 이도록 요청하는 것은 무례하지 않습니다. 행운을 빌어 요. –

+0

[여러 JFrames의 사용, 좋음/나쁨 연습] (http : // stackoverflow.co.kr/a/9554657/418556) –

답변

1

등록 클래스에서이, 시도는,) (등록에서) (등록 생성자의 이름을 수정 .

컨테이너의 서브 클래스의 객체를 생성 GUI가 응용 프로그램을

  1. 를 구성하기 전에 당신의 마음에 이러한 몇 가지 가이드 라인을 유지합니다.

  2. 컨테이너에있는 모든 구성 요소를 인스턴스 변수로 간주하십시오.

  3. 메소드 (예 : setComponent(), setHandler() 등)의 생성자 외부에서 이러한 인스턴스 변수 및 이벤트 처리를 설정하지만 생성자에서 이러한 메소드 호출을 수행하십시오.

  4. 이제 주요 사용이 ...

.

EventQueue.invokeLater(new Runnable() { 

    public void run() { 
     Myframe f = new Myframe(); 
     f.setVisible(true); 
    } 
} 
+0

'extend JFrame'은 당신의 대답이 어떤 upvotes도없는 이유입니다 – mKorbel

+0

나는이 포스트를 완전히 잊었습니다. 미안! 나는 대학에 지옥의 년 후에 다만 위치에 돌아왔다. 나에게 알려주기 바란다면 알려주세요. 그렇지 않으면 닫을 수 있습니다. 도움을 주셔서 감사합니다. 모든 답변에 감사드립니다. – DBIT

관련 문제