我目前在 JPanel 中有一个 JPanel,我想在其中放置 JScrollPane/JTable。我无法将其放置在我想要的位置。我想让表格适合 JPanel 的整个宽度,但只填充垂直显示表格中所有动态数据所需的空间。
这是我目前拥有的代码,图片就是它的样子。蓝色区域是表格/滚动情况的嵌套面板。还有为什么桌子下面有一个愚蠢的灰色盒子。我该如何摆脱它?对不起图片,数据很敏感。谢谢。
JPanel panel = new JPanel();
panel.setBounds(50, 100, 300, 450);
panel.setBackground(Color.blue);
contentPane.add(panel); //contentPane is the outter panel
JScrollPane scrollpane = new JScrollPane(table); //table was created earlier
panel.add(scrollpane,BorderLayout.NORTH);
示例程序(我不想要桌子下面的那个大矩形)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(350,100,1000,800);
JPanel contentPane = new JPanel(new BorderLayout());
frame.setContentPane(contentPane);
JTable table = new JTable(5,5);
JScrollPane scrollpane = new JScrollPane(table);
table.setFillsViewportHeight(true);
contentPane.add(scrollpane,BorderLayout.NORTH);
frame.revalidate();
}
}