如何在GridBagLayout中将元素对齐到顶部?

时间:2014-05-30 10:01:20

标签: java swing alignment layout-manager gridbaglayout

我希望在点击按钮后将文本元素放在GridBagLayout中。 但元素是垂直居中的。那么如何强制网格包布局将元素定位到顶部呢?

以下是一个示例代码:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class RefreshPanel {

private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private GridBagLayout gbl = new GridBagLayout();
private JMenu menuStart= new JMenu("Start");
private JMenu menuEdit= new JMenu("Edit");
private JMenu menuWindow= new JMenu("Window");
private JMenu menuHelp= new JMenu("help");
private JMenuBar menuBar = new JMenuBar();

RefreshPanel() {
    menuBar.add(menuStart);
    menuBar.add(menuEdit);
    menuBar.add(menuWindow);
    menuBar.add(menuHelp);
    scrollbar = new JScrollPane(panel);

    panel.setBackground(Color.WHITE);
    panel.setLayout(gbl);                       

    addButtonListener();        
    createFrame();
} //constructor

public void addButtonListener() {
    newItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            arrayList.add("data");
            textPane = generateTextPane(arrayList.size(), arrayList);
            System.out.println("Länge: "+textPane.length);
            for(int i=0;i<textPane.length;i++) {
                System.out.println(textPane[i].getText());
                addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 0);
                panel.revalidate();
            }       
            frame.revalidate();
        }
    });
}

private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
        JTextPane [] textPane = new JTextPane[arraySize];
        for(int i=0;i<textPane.length;i++) {
            textPane[i]=new JTextPane();    
            textPane[i].setText((String) arrayList.get(i));
            System.out.println("length >> "+textPane.length);
        }
    return textPane;
}

public void addComponent(Container cont, 
         GridBagLayout gbl, 
         Component c,
         int x, int y,
         int width, int height,
         double weightx, double weighty) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbc.anchor = GridBagConstraints.SOUTH;
    gbl.setConstraints( c, gbc );
    cont.add( c );
}
public void createFrame() {
    frame.add(menuBar, BorderLayout.NORTH);
    frame.add(scrollbar, BorderLayout.CENTER);
    frame.add(newItem, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(300,300));
    frame.setVisible(true);
}
public static void main(String [] args) {
    new RefreshPanel();
}       
}

我知道当我放置一个新元素时,网格会动态地改变它们的大小。 任何想法如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

我已经更改了GridBagLayout属性。检查一下:

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class RefreshPanel {

private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private GridBagLayout gbl = new GridBagLayout();
private JMenu menuStart= new JMenu("Start");
private JMenu menuEdit= new JMenu("Edit");
private JMenu menuWindow= new JMenu("Window");
private JMenu menuHelp= new JMenu("help");
private JMenuBar menuBar = new JMenuBar();

RefreshPanel() {
    menuBar.add(menuStart);
    menuBar.add(menuEdit);
    menuBar.add(menuWindow);
    menuBar.add(menuHelp);
    scrollbar = new JScrollPane(panel);

    panel.setBackground(Color.WHITE);
    gbl.columnWidths = new int[]{0, 0, 0, 0};
    gbl.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
    gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    panel.setLayout(gbl);                       

    addButtonListener();        
    createFrame();
} //constructor

public void addButtonListener() {
    newItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            arrayList.add("data");
            textPane = generateTextPane(arrayList.size(), arrayList);
            System.out.println("Länge: "+textPane.length);
            for(int i=0;i<textPane.length;i++) {
                System.out.println(textPane[i].getText());
                panel.add(textPane[i],new GridBagConstraints( 0,  i,  1,  1, 
                        1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
                panel.revalidate();
            }       
            frame.revalidate();
        }
    });
}

private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
        JTextPane [] textPane = new JTextPane[arraySize];
        for(int i=0;i<textPane.length;i++) {
            textPane[i]=new JTextPane();    
            textPane[i].setText((String) arrayList.get(i));
            System.out.println("length >> "+textPane.length);
        }
    return textPane;
}

public void addComponent(Container cont, 
         GridBagLayout gbl, 
         Component c,
         int x, int y,
         int width, int height,
         double weightx, double weighty) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbc.anchor = GridBagConstraints.SOUTH;
    gbl.setConstraints( c, gbc );
    cont.add( c );
}
public void createFrame() {
    frame.add(menuBar, BorderLayout.NORTH);
    frame.add(scrollbar, BorderLayout.CENTER);
    frame.add(newItem, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(300,300));
    frame.setVisible(true);
}
public static void main(String [] args) {
    new RefreshPanel();
}       
}