我试图通过使用setBounds()方法在我的JFrame中心移动一个JTable对象,但不幸的是它并没有在中心移动。使用setbounds()方法无法做到这一点。 这是我的代码:
package table;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class Table extends JFrame{
JTable tabel;
Table()
{
String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
tabel=new JTable(data,columnNames);
tabel.setLayout(null);
tabel.setBounds(100,900,700,400);
tabel.setPreferredScrollableViewportSize(new Dimension(500,50));
tabel.setFillsViewportHeight(true);
JScrollPane pane=new JScrollPane(tabel);
add(pane);
}
public static void main(String[] args) {
Table gui=new Table();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setBounds(300,100,700,500);
gui.setVisible(true);
}
}
我做错了什么?