在Java、Swing、Flowlayout中移动Jtable
到目前为止,我有以下代码:在Java、Swing、Flowlayout中移动Jtable,java,swing,flowlayout,Java,Swing,Flowlayout,到目前为止,我有以下代码: public class Table extends JFrame { JTable table; public Table() { setLayout (new FlowLayout()); //Default layout String[] columnNames = {"Fly model", "Fly kode", "Destination", "Tidspu
public class Table extends JFrame {
JTable table;
public Table()
{
setLayout (new FlowLayout()); //Default layout
String[] columnNames = {"Fly model", "Fly kode",
"Destination", "Tidspunkt"};
Object[][] data = {
{"Boeing 737", "Ab79SO", "Oslo", "22:00"},
{"MD125", "Tb682O", "Stockholm", "15:21"},
{"Boeing 737", "HJ72SR", "Reikjavic", "08:13"},
};
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 50));
table.setFillsViewportHeight(true);
setVisible(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public JTable returnJTable()
{
setVisible(false);
return table;
}
}
我不习惯使用
FlowLayout
,因此我不知道如何在我使用的JFrame中移动此对象。我知道,当您使用null
(绝对)布局时,可以使用setBounds()
告诉JFrame元素的位置。但是我如何在FlowLayout中做到这一点呢?你不能用FlowLayout
做到这一点。可以水平或垂直地逐个添加新组件,但不能将组件添加到特定位置。您可以尝试在JTable
之前/之后的空白面板或标签上使用一些技巧,但最好使用其他布局
尝试使用BorderLayout
,它很简单,借助它,您可以将JTable
定位到不同的位置。读一读
或者,您可以使用另一个
LayoutManager
,对其进行设置并选择。使用FlowLayout
您无法移动对象。所有对象都放置在一行中
尝试使用边框布局
或网格布局
。
如果更改方向,可以移动桌子
Panel myTable = new Panel(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.HORIZONTAL; //area
c1.ipadx = 0; //spacing
c1.ipady = 0; //spacing
c1.weightx = 1.0; //horizontal
c1.weighty = 1.0; //vertical
c1.anchor = GridBagConstraints.CENTER; //orientation
c1.insets = new Insets(10,10,10,10); //padding
c1.gridx = 0; //column
c1.gridy = 0; //line
c1.gridheight = 1; //number of lines
c1.gridwidth = 1; //number of columns
myTable.add(new JScrollPane(table),c1);