【发布时间】:2014-11-08 11:32:26
【问题描述】:
我正在创建一个想要上下滚动的定制表格,所以我使用 JScrollPane。
我的问题是,每次我在表格中添加另一行时,它都会将其添加到最后一行的一侧,而不是最后一行的下方,这是我想要的位置。例如,它向表中添加列而不是行。
需要添加的行数不确定,取决于表中有多少条目。
我知道我可以使用表格,但出于本练习的目的,我想这样做。
下面是我的代码的 MCVE 版本,它演示了这个问题。我保留了颜色变化,因此字段更明显:
public class Customers {
public static JFrame frame = new JFrame();
public static void frameGui(JPanel panel, String name){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setSize(1200,500);
frame.setVisible(true);
}
public static void ScrollCustomersGui(){
JPanel Table = new JPanel();
Table.add(customersTableHeadings(Table));
Table.add(customersTableHeadings(Table));
Table.add(customersTableHeadings(Table));
JScrollPane scroll = new JScrollPane(Table);
JPanel All = new JPanel(new BorderLayout());
All.add(scroll);
frameGui(All, "Customers");
}
public static JPanel customersTableHeadings(JPanel panel){
FlowLayout FlowCustTable = (FlowLayout)panel.getLayout();
FlowCustTable.setHgap(0);
JPanel customersTable = new JPanel(FlowCustTable);
JTextField custid = new JTextField("ONE");
custid.setPreferredSize(new Dimension(500, 50));
custid.setBackground(Color.CYAN);
customersTable.add(custid);
return customersTable;
}
}
【问题讨论】:
标签: java user-interface jpanel jscrollpane