GridBagLayout对齐问题

时间:2014-02-23 15:29:04

标签: java swing layout gridbaglayout

我从一段时间以来一直在练习GridBagLayout,但是当我想要所需的输出时我仍感到困惑。我想要这种输出

GUI Required

但我搞砸了创建一个桌子在一边,两个按钮在另一边。桌子被挤压了。

任何人都可以给我代码来对齐这些组件(按钮和表格)。组件的参考是 activateServer,addFiles和table

提前致谢!

编辑:这是我正在使用的代码(仅用于说明问题的简短代码)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ServerCode extends JFrame
{
    ServerCode()
    {
        JTable table = new JTable(10,3);
        JButton addFiles= new JButton("Add files"),activateServer = new JButton("Activate Button");

        setLayout(new GridBagLayout());

        add(table,new GridBagConstraints(0,0,6,6, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));
        add(addFiles,new GridBagConstraints(1,6,2,1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));
        add(activateServer,new GridBagConstraints(6,6,2,1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));

        setSize(700,400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String...args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ServerCode();
            }
        });
    }

}

它的输出是:

Problem

2 个答案:

答案 0 :(得分:2)

哎呀,我只是为此使用BorderLayout。如果不需要,请忘记GridBagLayout。例如......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class LayoutFoo extends JPanel {
   private static final int PREF_W = 800;

   public LayoutFoo() {

      JPanel buttonPanel = new  JPanel();
      buttonPanel.add(new JButton("Foo"));
      buttonPanel.add(Box.createHorizontalStrut(10));
      buttonPanel.add(new JButton("Bar"));


      String[] columnNames = {"Mon", "Tues", "Wed"};
      DefaultTableModel model = new DefaultTableModel(columnNames, 25);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);
      scrollPane.getViewport().setPreferredSize(table.getPreferredSize());
      // scrollPane.getViewport().setPreferredSize(table.getPreferredScrollableViewportSize());

      JLabel southLabel = new JLabel("Foobars!");
      southLabel.setForeground(Color.white);
      JPanel southPanel = new JPanel();
      southPanel.setBackground(Color.black);
      southPanel.add(southLabel);

      setLayout(new BorderLayout(5, 5));
      add(buttonPanel, BorderLayout.NORTH);
      add(scrollPane, BorderLayout.CENTER );
      add(southPanel, BorderLayout.SOUTH);
   }

   public Dimension getPreferredSize() {
      Dimension superSize = super.getPreferredSize();
      int width = PREF_W > superSize.width ? PREF_W : superSize.width;
      return new Dimension(width, superSize.height);
   }


   private static void createAndShowGUI() {
      LayoutFoo paintEg = new LayoutFoo();

      JFrame frame = new JFrame("Smart File Transfer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

答案 1 :(得分:0)

如果你想保留GridBagLayout,我相信你需要这样的东西:

    add(activateServer,new GridBagConstraints(0,0,1,1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));
    add(addFiles,new GridBagConstraints(1,0,1,1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));
    add(table,new GridBagConstraints(0,1,2,1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));

如果您在使用GridBagConstraints的创建者时遇到问题,您应该尝试使用更简单的方法,例如this example

了解更多信息,请尝试oracle documentation