JFrame太小而pack()无效

时间:2017-11-22 05:12:33

标签: java jframe window-resize

所以我有两个JFrame。第一个只有一个按钮,其中有一个事件可以调用第二个。第二个JFrame包含一个填充了MySQL数据库数据的表,该表是以下代码:

public class NewClass extends JFrame {
    public NewClass()
    {
        ArrayList columnNames = new ArrayList();
        ArrayList data = new ArrayList();
        String url = "jdbc:mysql://localhost:3306/MYDB";
        String userid = "root";
        String password = "password";
        String sql = "SELECT * FROM MYTABLE";
    try (Connection connection = DriverManager.getConnection( url, userid, password );
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery( sql ))
    {
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();

        for (int i = 1; i <= columns; i++)
        {
            columnNames.add( md.getColumnName(i) );
        }

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

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

            data.add( row );
        }
    }
    catch (SQLException e)
    {
        System.out.println( e.getMessage() );
    }

    Vector columnNamesVector = new Vector();
    Vector dataVector = new Vector();

    for (int i = 0; i < data.size(); i++)
    {
        ArrayList subArray = (ArrayList)data.get(i);
        Vector subVector = new Vector();
        for (int j = 0; j < subArray.size(); j++)
        {
            subVector.add(subArray.get(j));
        }
        dataVector.add(subVector);
    }

    for (int i = 0; i < columnNames.size(); i++ )
        columnNamesVector.add(columnNames.get(i));

    JTable table = new JTable(dataVector, columnNamesVector)
    {
        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)
{
    NewClass frame = new NewClass();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

当我直接运行NewClass时,pack()按原样工作,并根据内部高度的大小调整JFrame的大小。但是,每当我使用按钮运行第一个JFrame然后按下按钮来调用该事件时,NewClass会弹出,但窗口太小了。我尝试过使用setSize,显然是pack()。

当我手动调整JFrame的大小时,列和行就像它们应该的那样,只是我无法弄清楚为什么pack()不起作用。

对于如何调整第二个JFrame的大小而不必手动执行此操作,您有什么想法吗? 提前谢谢!

编辑:

第一个JFrame上调用NewClass的按钮代码如下:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    NewClass frame = new NewClass();
    frame.setVisible(true);
}

1 个答案:

答案 0 :(得分:0)

更改...

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "ApplicationCookie",
    LoginPath = new PathString("/auth/login"),
    Provider = new CookieAuthenticationProvider { OnApplyRedirect = OnApplyRedirect }
});

public static void OnApplyRedirect(CookieApplyRedirectContext context)
{
    var url = HttpContext.Current.Request.Url.AbsoluteUri;

    string redirectUrl = "/auth/login";
    if (url.ToLower().Contains("/su/"))
    {
        redirectUrl = "/su/auth/login";
    }
    else if (url.ToLower().Contains("/app/"))
    {
        redirectUrl = "/app/auth/login";
    }

    context.Response.Redirect(redirectUrl);
}

更像是......

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    NewClass frame = new NewClass();
    frame.setVisible(true);
}

这似乎与您在private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { NewClass frame = new NewClass(); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } 方法

中所做的相符