JFrame无法正确显示

时间:2013-04-23 13:32:17

标签: java sql swing jdbc jframe

我的程序存在设计问题。

首先显示带有组合框的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);
     }
    }

有人能说清楚我做错了吗?

我尝试了很多不同的事情试图找到底部,但程序似乎没有回应我投入的任何内容?

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

由于

3 个答案:

答案 0 :(得分:2)

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

此外,您需要将Swing组件放在Event Dispatch线程(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);
        }       
    });
}

答案 1 :(得分:1)

  1. 分离GUI和数据库的逻辑,可以在try - catch - finally块之前定义列名,因为ResultSetMetaData似乎是(从不)未使用

  2. rs.close();以及stmt.close();connection.close();将在finally block

  3. 中关闭
  4. JTable, XxxTableModel is based on 2D array,然后Vector data = new Vector();应为Vector<Vector<Object>> data = new Vector<Vector<Object>>();

  5. 定义为局部变量(JFrameJTableJScrollpane),在整个应用程序时间内,所有存在的对象仍然可在GUI中看到

  6. 请参阅InitialThread,否则JFrame.pack()JFrame.setVisible(true)应包含在invokeLater()

  7. JTable中的
  8. JScrollPane未返回正确的Dimension,需要覆盖getPreferredScrollableViewportSize(new Dimension(x, y));,然后JFrame.pack()会返回任何{{1}的正确坐标包裹在JComponent

答案 2 :(得分: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);
}