用netbeans开发直接拖拽,很容易实现图上的布局。如果非得要知道代码的话,我感觉,上中下应该用网格布局GridLayout.(new GridLayout(3,1)),在依次添加三个JPanel。中下就在JPanel上添加表格就行了。在上面的用FloweLayout,这个布局管理器是根据从左到右的顺序添加,如果在一行下添加不下,就会依次往下一行添加。还有一种办法,就是在上面的JPanel用GridLayout,分成两行一列,上面的添加JLabel和按钮,下面的就是JTable。
模拟代码:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame
{
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JLabel label;
private JTable table1;
private JTable table2;
private JTable table3;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
public Test()
{
btn1=new JButton("1");
btn2=new JButton("2");
btn3=new JButton("3");
label=new JLabel("This is a simple");
table1=new JTable();
table2=new JTable();
table3=new JTable();
table1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
table2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
table3.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
panel5=new JPanel();
this.setLayout(new GridLayout(3,1));
this.add(panel1);
this.add(panel2);
this.add(panel3);
panel2.add(table2);
panel3.add(table3);
panel1.setLayout(new GridLayout(2,1));
panel1.add(panel4);
panel1.add(panel5);
panel4.setLayout(new FlowLayout());
panel4.add(label);
panel4.add(btn1);
panel4.add(btn2);
panel4.add(btn3);
panel5.add(table1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(220,260);
}
public static void main(String[] args)
{
Test test=new Test();
test.setVisible(true);
}
}
上面三个按钮可以放在底层面板里。
面板名称.getContentPane().setLayout(null);
控件名.setBounds(x轴,y轴,宽,高);
你试试不使用布局管理器,使用绝对定位,这样应该就可以了
。。。你可以使用定位布局