1

我的程序有设计问题。

首先显示一个带有组合框的 Jframe。组合框中充满了您选择的城镇名称并点击搜索。

然后程序会显示一个 jtable,其中包含表中的值。它生成 Jtable 和信息,但 jframe 本身显示为好像没有设置大小参数,导致不显示任何内容,直到您通过将其拖动到右下角手动增加大小。

这是Jtable的代码

public class Display_All extends JFrame
{
    public Display_All(Object town)
    {
        Vector columnNames = new Vector();
        Vector data = new Vector();

        try
        {
            //  Connect to an Access Database


            String driver = "com.mysql.jdbc.Driver";
            String url ="jdbc:mysql://localhost:3306/ire";
            String userid = "root";
            String password = "password";

            Class.forName( driver );
            Connection connection = DriverManager.getConnection( url, userid, password );

            //  Read data from a table

            String sql = "(SELECT Activities_Name,       Activities_Email,Activities_Address_1,   Town, Activities_Phone ,Activities_Mobile, Activities_Web_Address, Activities_Owner FROM activities WHERE Town ='"+town+"')"
                    + "UNION ALL ( SELECT Attraction_Name, Attraction_Email, Attraction_Address_1, Town, Attraction_Phone, Attraction_Mobile, Attraction_Web_Address, Attraction_Owner FROM attractions WHERE Town = '"+town+"')"
                    + "UNION ALL ( SELECT BB_Name, BB_Email, BB_Address_1 ,Town, BB_Phone, BB_Mobile, BB_Web_Address, BB_Owner FROM bb WHERE Town = '"+town+"')"
                    + "UNION ALL ( SELECT Hotel_Name ,Hotel_Email ,Hotel_Address_1, Town, Hotel_Phone, Hotel_Mobile, Hotel_Web_Address, Hotel_Owner FROM hotels WHERE Town = '"+town+"')"
                    + "UNION ALL ( SELECT Pub_Name, Pub_Email ,Pub_Address_1, Town, Pub_Phone ,Pub_Mobile, Pub_Web_Address, Pub_Owner FROM pubs WHERE Town = '"+town+"')"
                    + "UNION ALL ( SELECT Restaurant_Name, Restaurant_Email, Restaurant_Address_1, Town, Restaurant_Phone, Restaurant_Mobile, Restaurant_Web_Address, Restaurant_Owner FROM restaurant WHERE Town = '"+town+"')"
                    + "UNION ALL ( SELECT Self_Catering_Name, Self_Catering_Email, Self_Catering_Address_1, Town, Self_Catering_Phone, Self_Catering_Mobile, Self_Catering_Web_Address, Self_Catering_Owner FROM self_catering WHERE Town = '"+town+"')";


            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names


            columnNames.addElement( "Business Name" );
            columnNames.addElement( "Email" );
            columnNames.addElement( "Address 1" );
            columnNames.addElement( "Town" );
            columnNames.addElement( "Phone" );
            columnNames.addElement( "Mobile" );
            columnNames.addElement( "Web Address" );
            columnNames.addElement( "Owner" );


            //  Get row data

            while (rs.next())
            {
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++)
                {
                    row.addElement( rs.getObject(i) );
                }

                data.addElement( row );
            }

            rs.close();
            stmt.close();
            connection.close();
        }
        catch(Exception e)
        {
            System.out.println( e );
        }

        //  Create table with database data

        JTable table = new JTable(data, columnNames )
        {
            public Class getColumnClass(int column)
            {
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                    {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

    //        JPanel buttonPanel = new JPanel();
    //        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

    public static void main(String[] args)
    {
        Display_All frame = new Display_All("town");
        frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setVisible(true);
    }

     public void run() {
        new Display_All("town").setVisible(true);
     }
    }

谁能阐明我做错了什么?

我已经尝试了很多不同的方法来了解这一点,但是该程序似乎没有响应我放入其中的任何内容?

很难解释我会展示图片,但我的声誉太低了。

谢谢

4

3 回答 3

2

您必须设置 JFrame 的大小,或者打包 JFrame。

此外,您需要将 Swing 组件放在事件调度线程 (EDT) 上。

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Display_All frame = new Display_All("town");
            frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );

            frame.setSize(new Dimension(400, 300));  // or
            frame.pack();

            frame.setVisible(true);
        }       
    });
}
于 2013-04-23T13:39:46.287 回答
1
  1. GUI 和数据库的拆分逻辑,列名可以在 -- 块之前定义trycatch因为finally似乎ResultSetMetaData(从不)没有使用

  2. rs.close();并且stmt.close();应该connection.close();关闭finally block

  3. JTable,XxxTableModel是基于二维数组, 那么Vector data = new Vector();应该是Vector<Vector<Object>> data = new Vector<Vector<Object>>();

  4. 定义为局部变量 ( JFrame, JTable, JScrollpane),在整个应用程序期间,所有存在的对象在您的 GUI 中仍然可见

  5. InitialThread,否则JFrame.pack()并且JFrame.setVisible(true)应该被包裹到invokeLater()

  6. JTableinJScrollPane不返回正确的Dimension,需要覆盖getPreferredScrollableViewportSize(new Dimension(x, y));然后JFrame.pack()为任何JComponent包裹的返回正确的坐标JScrollPane

于 2013-04-23T13:51:31.410 回答
0

这个主要方法将使您能够看到 JFrame 并将其设置为系统的外观。即 Windows 操作系统,这意味着 JFrame 看起来像一个 Windows 对话框。

public static void main(String[] args) 
{
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ignored) {}
    MainWindow mainWindow = new MainWindow();
    mainWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
    mainWindow.setLocationRelativeTo(null);
    mainWindow.setVisible(true);
    mainWindow.setSize(500,300);
}
于 2013-04-23T14:24:55.107 回答