1

我已经有一个面板,我在其中添加了按钮和一个文本区域。现在我需要在它下面显示一个表格。面板的布局是 AbsoluteLayout,如果我使用以下方法给表格充气,它会显示为空

        String data[][] = { { "vinod", "BCA", "A" }, { "Raju", "MCA", "b" },
                { "Ranjan", "MBA", "c" }, { "Rinku", "BCA", "d" } };

        String col[] = { "Name", "Course", "Grade" };
        JTable table = new JTable(data, col);
        table.setLocation(25,343);
        panel.add(table, BorderLayout.CENTER);

但是,当我使用 setbounds 时,表格显示还可以,但它占用的空间超过了所需的空间,并且这些行没有显示在空白空间中,而且看起来很糟糕。有谁知道如何仅设置表格的 x,y 位置并保留其余位置(宽度和高度以适应所需的内容)

String data[][] = { { "vinod", "BCA", "A" }, { "Raju", "MCA", "b" },
        { "Ranjan", "MBA", "c" }, { "Rinku", "BCA", "d" } };

String col[] = { "Name", "Course", "Grade" };
JTable table = new JTable(data, col);
table.setBounds(25,343,122,122);
panel.add(table, BorderLayout.CENTER);
4

2 回答 2

5
于 2013-02-18T14:30:28.260 回答
0
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.add (Box.createVerticalStrut(343), BorderLayout.NORTH);
frame.add (Box.createHorizontalStrut(25), BorderLayout.WEST);
frame.add (new JTable (5, 5), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
于 2013-02-18T14:21:15.513 回答