2014-12-06 2 views
0

try 및 catch 함수를 실행할 때 Jframe에서 JPanels를 추가 할 수 없습니다. 이미 시도하거나 catch하지만 내 패널을 추가하려했지만 그다지 도움이되지 않습니다! ps. 나는 시도 기능 전에 frame.add(new Product("lol"));를 실행하는 경우에만 작동try/catch jdbc java jframe을 jframe 안에 넣을 수 없습니다.

public static void main(String[] args) { 
     Connection conn = null; 
     Statement stmt = null; 
     frame.setVisible(true); 
     frame.setSize (new Dimension (704, 454)); 
     GridLayout layout = new GridLayout(3, 3, 41, 10); 
     String shit = null; 
     frame.setLayout (layout); 

     try{ 
      //STEP 2: Register JDBC driver 

      Class.forName("com.mysql.jdbc.Driver"); 

      //STEP 3: Open a connection 
      System.out.println("Connecting to a selected database..."); 
      conn = DriverManager.getConnection(DB_URL, USER, PASS); 
      System.out.println("Connected database successfully..."); 


      stmt= conn.createStatement(); 
      ResultSet rs; 

      rs = stmt.executeQuery("select productName from product"); 
      rs.next(); 
      String name = rs.getString("productName"); 
      System.out.println(name); 
      shit = name; 

     }catch(SQLException se){ 
      //Handle errors for JDBC 
      se.printStackTrace(); 
     }catch(Exception e){ 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
     }finally{ 
      //finally block used to close resources 

      try{ 
       if(stmt!=null) 
       conn.close(); 
      }catch(SQLException se){ 
      }// do nothing 
      try{ 
       if(conn!=null) 
       conn.close(); 
      }catch(SQLException se){ 
       se.printStackTrace(); 
      }//end finally try 
     }//end try 
     System.out.println("Goodbye!"); 


     frame.revalidate(); 
     frame.repaint(); 
     frame.add(new Product("lol")); 


    } 
} 

제품 클래스

public Product(String name){ 
     JLabel label1 = new JLabel(name); 
     add(label1); 
     setBorder(blackline); 

    } 

답변

1

당신은 당신이 코드

frame.revalidate(); 
frame.repaint(); 
frame.add(new Product("lol")); 
을 변경해야하므로 구성 요소를 추가 한 후 repaint()revalidate()를 호출해야

to this

frame.add(new Product("lol")); 
frame.revalidate(); 
frame.repaint(); 
+2

고쳐서 고마워, 지금 바보 같아. ((( – PNC

+0

) 더 나은 접근 방법은 시작시 GUI에 빈 테이블을 넣고 DB 쿼리 후에 데이터를 채우는 것입니다. –

관련 문제