Java - 无法确定要使用哪种布局?

时间:2013-10-31 15:18:27

标签: java swing layout

我的布局很完美,直到我无法弄清楚如何进行拖放工作。因此,为了简化编码,我将程序右下角的标签切换为按钮,允许单击以在主面板中生成对象。

现在我使用BoxLayout切换它们,按钮的大小无法使图像完美地适合它们,从而留下照片中看到的边缘空间。我现在还有一个水平滚动条,我以前没有标签。

enter image description here

我尝试了几种不同的布局来尝试修复这些按钮的大小,但我无法正常工作。我只需要一个垂直滚动条,我希望按钮是图像的确切大小,就像它们在它们上面的面板中一样。我尝试将布局设置为null,就像我在所有其他面板中一样,并使用setBounds()方法,这对于放置非常有效,但滚动条会消失,不会滚动。

有人有什么建议吗?

编辑:这是我使用null布局时会发生的事情。

enter image description here

1 个答案:

答案 0 :(得分:2)

如果你使用swing,我真的建议你使用GridBag布局。其他布局还有很多不足之处。这是一个优先考虑的问题,如果你愿意,你可以手动将其列出来 - 没有正确的答案。

我更喜欢GridBag(或MigLayout - 他们自己的)的原因是你有一个首选的组件大小和填充概念的概念。自从我编写Swing以来已经有一段时间了(我会尝试保持这种方式!)但你基本上都在寻找类似的东西:

{
   //Pseudo Code, I'd have to go read the API again, I wrote a set of utilities so I    wouldn't have to think about it.
  GridBagConstraints constraints = ....;
  constraints.weightX = 1.0; //fill the area by X
  constraints.weightY = 1.0; //fill by Y
  constraints.fill = GridBagConstraints.BOTH; //or one...
  component.setPreferredSize(image.size());
  layout.add(component, constraints); 
}

基本上你正在做的是说“尽量使用我喜欢的尺寸”,但要根据这些规则填写。

替代方案 - 不使用布局只是自己定位组件(这绝对没有错)。

{
  JPanel panel =...;
  panel.setLayout(null);

  ...
  myButton3.setX(0);
  myButton3.setY(2 * buttonHeight); //third button
  myButton.setSize(myButton.getPreferredSize()); //which I assume you set
  ...
  panel.add(myButton3);
  ...
}

无论如何,有很多选择。不要觉得你需要使用布局,自己编写。你应该关心这些事情并让它发挥作用,但你不应该受苦。布局通常很容易实现,你不应该害怕离开它。

所有这一切,GridBag将做你想要的。或者,Mig很棒,并且有一些很好的GUI编辑器。

更新 - > ------------------------------- 这是一个简洁的例子 - 我真诚地不提倡这种编程风格,我只是不想要类垃圾邮件的例子。

   package _tests;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Grids extends JFrame
{
  private static final long serialVersionUID = 1L;

  public static void main(String ... args)
  {
    new Grids().setVisible(true);
  }

  public Grids()
  {
    //Null layout example
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(250, 300);
    setMinimumSize(new Dimension(285, 300)); //Windows 8 ~ border size + scrollbar
    setTitle("Test layouts");

    JPanel scrollTarget = new JPanel()
    {
      private static final long serialVersionUID = 1L;
      {
        setSize(250, 1000);
        setPreferredSize(new Dimension(250, 1000));
        //setLayout(null); -- uncomment for absolute
        setLayout(new GridBagLayout());

        int lastX = 0;
        int lastY = 0;
        for(int i = 0; i < 5; i++)
        {
          final String label = "Button " + i;
          JButton tmp = new JButton()
          {
            private static final long serialVersionUID = 1L;
            {
              setText(label);
              setPreferredSize(new Dimension(250, 200)); //Preferred
            }
          };

          tmp.setSize(tmp.getPreferredSize()); //What you're layout usually does..
          //add(tmp);
          //tmp.setLocation(lastX, lastY);
          //lastY += tmp.getHeight();

          add(tmp, getButtonConstraint(0, i));
        }
      }

    };

    add(new JScrollPane(scrollTarget));
    }



      private GridBagConstraints getButtonConstraint(int x, int y)
      {
        GridBagConstraints tmp = new GridBagConstraints();
        tmp.fill = GridBagConstraints.BOTH;
        tmp.weightx = 1.0;
        tmp.weighty = 1.0;
        tmp.gridx = x;
        tmp.gridy = y;
        tmp.anchor = GridBagConstraints.NORTHEAST;

        return tmp;
      }

}
相关问题