java - try/catch jdbc java 无法将 jpanel 放入 jframe 中

标签 java mysql swing jpanel try-catch

当我执行 try 和 Catch 函数时,我无法在 Jframe 中添加 JPanels 我已经尝试在每个 try 或 catch 中添加我的面板,但它没有用,请帮助我! 附:仅当我在 try 函数之前执行 frame.add(new Product("lol")); 时才有效

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);

    }

最佳答案

添加组件后您需要调用repaint()revalidate(),因此您应该更改此代码

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

到此

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

关于java - try/catch jdbc java 无法将 jpanel 放入 jframe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27329315/

相关文章:

java - 从 jButton 获取文本值

java - Maven + ODL osgi.wiring.package javax

MYSQL Join weeks table with Sales 和 Group by Salesman and Weeks

mysql - 我的sql代码哪里出了问题?

Java .jar 应用程序无法在 Mac Os X Snow Leopard 上启动

java - 为什么 JPanel 只重绘一次?

Java 将 XML 转换为 YAML

java - 将 Canvas 转换为 WritableImage 的替代方法

java - 识别可以转换为递归方法的方法

mysql - 如何在 SQL 中编写此 UPDATE,以更新具有不同 WHERE 条件的多行