设置Box的大小

时间:2013-04-04 14:34:12

标签: java swing jpanel jscrollpane border-layout

这似乎是一个愚蠢的问题,但我想改变jFrame中“盒子”的大小。我将包括代码和图片。

这是JTableResultSet类:

import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

public class JTableResultSet {
    public static void main(String[] args) {
        Vector columnNames = new Vector();
        Vector data = new Vector();
        JPanel panel = new JPanel();   //
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/exper482_social", "admin", "testing");
            String sql = "Select username, forename, lastname, password from social_users";
            Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columns = metaData.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(metaData.getColumnName(i));
            }
            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(resultSet.getObject(i));
                }
                data.addElement(row);
            }
            resultSet.close();
            statement.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        JTable table = new JTable(data, columnNames);
        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setMaxWidth(250);//250
        }
        JScrollPane scrollPane = new JScrollPane(table);        
        panel.add(scrollPane);       
        JFrame frame = new JFrame();
        frame.add(panel);//adding panel to the frame
        frame.setSize(600, 400); //setting frame size 600 400
        frame.setVisible(true);  //setting visibility true
    }
}

这是照片:

http://i.stack.imgur.com/Wblfx.png

有人可以告诉我,我可以做些什么来让盒子更大?感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用BorderLayoutJScrollPane占据JPanel

的整个区域
JPanel panel = new JPanel(new BorderLayout());

默认的JPanel布局FlowLayout使用组件的首选大小,该大小不会展开以填充窗口。

阅读Guide to Layout Managers