2010-01-13 4 views
0

많은 코드를 게시하게되어 죄송합니다! 왜 ListFrame이 작동하지 않는지 모르겠습니다. ??? 이들은 클래스입니다. 먼저 MainServer를 실행 한 다음 다른 패키지에서 MainFrame을 실행합니다. 올바른 사용자 이름과 암호를 삽입하면 Listframe이 표시되지만 메뉴 막대 또는 목록을 클릭하거나 버튼을 삭제하지만 아무 일도 일어나지 않을 것입니다. 왜? 도와주세요.스윙 문제 (프레임 표시 문제)

MainSerevr 클래스 :

public class MainServer { 

static Socket client = null; 
static ServerSocket server = null; 

public static void main(String[] args) { 
    System.out.println("Server is starting..."); 
    System.out.println("Server is listening..."); 

    try { 
     server = new ServerSocket(5050); 
    } catch (IOException ex) { 
     System.out.println("Could not listen on port 5050"); 
     System.exit(-1); 

    } 
    try { 
     client = server.accept(); 
     System.out.println("Client Connected..."); 
    } catch (IOException e) { 
     System.out.println("Accept failed: 5050"); 
     System.exit(-1); 
    } 
    try { 
     BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream())); 

     boolean done = false; 
     String line; 
     while (!done) { 
      line = streamIn.readLine(); 
      if (line.equalsIgnoreCase(".bye")) { 
       done = true; 
      } else { 
       System.out.println("Client says: " + line); 
      } 
     } 

     streamIn.close(); 
     client.close(); 
     server.close(); 
    } catch (IOException e) { 
     System.out.println("IO Error in streams " + e); 
    } 
}} 

ListFrame :

public class ListFrame extends javax.swing.JFrame implements PersonsModelChangeListener { 

    private InformationClass client; 
    private static DefaultListModel model = new DefaultListModel(); 
    private ListSelectionModel moDel; 

    /** Creates new form ListFrame */ 
    public ListFrame(InformationClass client) { 
     initComponents(); 
     this.client = client; 
     jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 


     fillTable(); 
     Manager.addListener(this); 
    } 

    private void deleteAPerson() { 
     int index = jList1.getSelectedIndex(); 
     String yahooId = (String) jList1.getSelectedValue(); 
     model.remove(index); 
     Manager.removeApersonFromSQL(yahooId); 
     int size = model.getSize(); 
     if (size == 0) { 
      jButton1.setEnabled(false); 
     } else { 
      if (index == size) { 
       index--; 
      } 
      jList1.setSelectedIndex(index); 
      jList1.ensureIndexIsVisible(index); 

     } 
    }      



    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {           
     AddAPerson frame = new AddAPerson(client); 
     frame.setVisible(true); 


    }           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     deleteAPerson(); 
    }           

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {          
     MainClient.setText(""); 
     MainClient.runAClient(); 
     ChatFrame frame = new ChatFrame(client); 
     frame.setVisible(true); 
    } 
    public void fillTable() { 
    try { 
     List<InformationClass> list = null; 
     list = Manager.getClientListFromMySQL(); 
     if (list == null) { 

      JOptionPane.showMessageDialog(this, "You should add a person to your list", "Information", JOptionPane.OK_OPTION); 
      return; 
     } else { 


      for (int i = 0; i < list.size(); i++) { 
       InformationClass list1 = list.get(i); 
       model.add(i, list1.getId()); 
      } 

      jList1.setModel(model); 
     } 


    } catch (SQLException ex) { 
     Logger.getLogger(ListFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

MainClient 클래스 :

public class MainClient { 


private static InformationClass info = new InformationClass(); 
private static Socket c; 
private static String text; 

public static String getText() { 
    return text; 
} 

public static void setText(String text) { 
    MainClient.text = text; 
} 

private static PrintWriter os; 
private static BufferedReader is; 
static boolean closed = false; 

/** 
* @param args the command line arguments 
*/ 
public static void runAClient() { 
    try { 
     c = new Socket("localhost", 5050); 

     os = new PrintWriter(c.getOutputStream()); 
     is = new BufferedReader(new InputStreamReader(c.getInputStream())); 

     String teXt = getText(); 
     os.println(teXt); 
     if(c!=null&& is!=null&&os!=null){ 
     String line = is.readLine(); 
     System.out.println("Text received: " + line); 
     } 


     c.close(); 
     is.close(); 
     os.close(); 


    } catch (UnknownHostException ex) { 
     System.err.println("Don't know about host"); 

    } catch (Exception e) { 
     System.err.println("IOException: " + e); 

    } 

} 


} 

편집 : 나는 때문에 MainClient.runAClient을을 writting입니다 문제를 발견했다 () 코드에서, 어디에 넣어야합니까? 도와주세요.

+3

이 코드는 컴파일되지 않습니다. 게시하는 예제 코드를 가능한 가장 작지만 여전히 문제가있는 동작을 보여주는 컴파일 가능한 예제로 다시 작성하십시오. 또한 올바른 코드 예제를 게시하는 방법에 대한 자세한 내용은 http://sscce.org/를 확인하십시오. – Bombe

+0

이 코드에서 MainClient.runAClient()를 추가했기 때문에이 문제점을 발견했습니다. 어디에 넣어야합니까? – Johanna

답변

1

article에는 간단한 클라이언트 - 서버 GUI를 나타내는 sscce이 들어 있습니다. 당신은 그것이 유익하다는 것을 알 수 있습니다. 그렇다면 Echo(Kind kind) 생성자의 마지막 줄에있는 버그를 해결하는 방법을 고려하십시오.

+0

MainClient.runAClient() 메소드를 code.whould로 작성했기 때문에 문제가있는 것을 발견했습니다.이 라인을 어디에 넣어야합니까? 감사합니다. – Johanna

+0

코드를 테스트 할 방법이 없습니다. 일반적으로 서버가 새로운 연결을 수용 할 준비가되면 언제든지 클라이언트 스레드를 시작할 수 있습니다. 인용 된 예제의'main()'메소드에서, 서버가 먼저 시작하고 그 다음에 클라이언트가 시작된다. 둘 중 하나가 닫힐 때까지 둘 다 실행됩니다. – trashgod