[英]Moving Jtable in Java, Swing, Flowlayout
到目前為止我有這個代碼:
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
您無法移動對象。 所有對象都放在一行中。
嘗試使用BorderLayout
或GridBagLayout
。 這是布局管理器的可視化指南 。
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);
如果更改方向,則可以移動工作台。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.