我就是用的BoderLayout,那怎样设置panel的大小能生效呢!

解决方案 »

  1.   

    getContentPane().add(panel1,BoderLayout.WEST);
    getContentPane().add(panel2,BoderLayout.EAST);
    panel1.setBounds(x, y, 680, 480);
    panel2.setBounds(x,y,150,480);
    最后要打包
    pack();
      

  2.   

    把布局设为null,然后用setBounds设置绝对位置,位置大小都由自己定.
    不过这样应用程序变化的时候就不一定好看了
    还是用JScrollPane来的妥当
      

  3.   

    把代码贴出来,各位请帮忙看一下!import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;public class TestFrame extends JFrame {
       
    static final int FRAME_WIDTH = 800;
    static final int FRAME_HTIGHT = 600;
    private JDesktopPane desktop;
    JPanel leftPane,rightPane;
        public TestFrame() {
            super("窗体");
    setBounds(0,0,FRAME_WIDTH,FRAME_HTIGHT);
    setDefaultLookAndFeelDecorated(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true); Container contentPane = getContentPane();
    leftPane = new JPanel();
    leftPane.setLayout(null);
    leftPane.setBounds(0,0,640,480);
    leftPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createLineBorder(Color.black),
    "Work Area"));
    desktop = new JDesktopPane();
    desktop.setLocation(20,20);
    desktop.setSize(640,480);
    leftPane.add(desktop);

    rightPane = new JPanel();
    rightPane.setLayout(null);
    rightPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createLineBorder(Color.black),
    "Info Area"));
    rightPane.setSize(150,480);

    contentPane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.insets = new Insets(1,1,1,1);
    add(contentPane,leftPane,gbc,0,1,1,1);
    add(contentPane,rightPane,gbc,1,1,1,1);

    /*
    contentPane.add(leftPane,BorderLayout.CENTER);
    contentPane.add(rightPane,BorderLayout.EAST);
     */
        }

    private void add(Container cn,Component c, GridBagConstraints gbc, int x, int y, int w, int h){
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    cn.add(c, gbc);
        }    public static void main(String[] args) {
            TestFrame testFrame = new TestFrame();
    testFrame.setVisible(true);
        }
    }
      

  4.   

    getContentPane().setLayout(null);之后才能自己布局
      

  5.   

    to beyond_xiruo:我不太明白你的意思!
    我在上面的代码中已经设定了leftPane 和rightPane的Layout和位置,如:
    leftPane = new JPanel();
    leftPane.setLayout(null);
    leftPane.setBounds(0,0,640,480);
    那如果getContentPane().setLayout(null);那我最后的整个布局要怎么做呢?
    我不是在JB中写代码,所以对于在JB中的布局我不熟悉,我就是在文本编辑器里写!
    所以请您说的清楚些!谢谢!
      

  6.   

    setBounds(x, y, width, height) only works in setLayout(null)
      

  7.   

    leftPane.setBounds(0,0,640,480);
    这句是在JFrame上布置的,所以一定要getContentPane().setLayout(null);
    setBounds才能起作用.