日期:2014-05-20  浏览次数:20796 次

AWT布局问题

怎么达到以上的要求,请指教

------解决方案--------------------
Java code
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
public class Example {
    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable(){
                @Override public void run(){
                    JFrame frame = new JFrame("Example");
                    JPanel container = new JPanel(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();

                    JPanel top = new JPanel();
                    top.add(new JLabel("Top"));
                    top.setBorder(BorderFactory.createLineBorder(Color.BLUE,3));
                    c.gridx = 0; c.gridy = 0;
                    c.weightx = 1;
                    c.gridwidth = 2;
                    c.fill = GridBagConstraints.BOTH;
                    container.add(top,c);

                    JPanel left = new JPanel();
                    left.add(new JLabel("East"));
                    left.setBorder(BorderFactory.createLineBorder(Color.BLACK,3));
                    c.gridx = 0; c.gridy = 1;
                    c.weightx = 0;
                    c.gridwidth = 1;                //reset to the default
                    c.weighty = 1.0;
                    c.gridwidth = GridBagConstraints.RELATIVE;
                    c.fill = GridBagConstraints.VERTICAL;
                    container.add(left,c);

                    JPanel center = new JPanel();
                    center.add(new JLabel("Center"));
                    center.setBorder(BorderFactory.createLineBorder(Color.GREEN,3));
                    c.gridx = 1; c.gridy = 1;
                    c.gridwidth = GridBagConstraints.REMAINDER;
                    c.fill = GridBagConstraints.BOTH;
                    container.add(center,c);

                    frame.setContentPane(container);
                    frame.setPreferredSize(new Dimension(600,400));
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
    }
}

------解决方案--------------------
Java code
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(top, BorderLayout.NORTH);
        panel.add(left, BorderLayout.WEST);
        panel.add(right, BorderLayout.CENTER);