如何使用setViewPortView()选项在JScrollPane中并排添加两个JTable,并根据分配给JscrollPane的大小设置表的大小
答案 0 :(得分:8)
答案 1 :(得分:3)
JScrollPane
只能包含一个JComponent
,但您可以将表格包装到JPanel
并将其添加到JScrollPane
,如下所示:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestFrame extends JFrame{
public TestFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
pack();
setVisible(true);
}
private void init() {
JTable t1 = new JTable(10,5);
JTable t2 = new JTable(15,5);
JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(0,5,0,5);
c.gridy=0;
c.gridx=0;
p.add(t1.getTableHeader(),c);
c.gridx=1;
p.add(t2.getTableHeader(),c);
c.gridx=0;
c.gridy=1;
p.add(t1,c);
c.gridx=1;
p.add(t2,c);
add(new JScrollPane(p));
}
public static void main(String... strings) {
new TestFrame();
}
}
答案 2 :(得分:2)
如何在JScrollPane中并排添加两个JTable setViewPortView()选项并相应地设置表的大小 到分配给JscrollPane的大小
JScrollPane
被指定只嵌套一个JComponent