Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不带换行符和/或垂直方向的FlowLayout_Java_Swing_Layout_Java 8 - Fatal编程技术网

Java 不带换行符和/或垂直方向的FlowLayout

Java 不带换行符和/或垂直方向的FlowLayout,java,swing,layout,java-8,Java,Swing,Layout,Java 8,这是一项后续行动。任何可行的答案都会回答这个问题 哪种布局可以在尽可能少的修改的情况下使用,以复制FlowLayout的对齐性质,但永远不会换行,并且可以自上而下地使用 显而易见的候选者,与JPanels没有很好的合作。考虑下面两个例子: import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPa

这是一项后续行动。任何可行的答案都会回答这个问题

哪种布局可以在尽可能少的修改的情况下使用,以复制FlowLayout的对齐性质,但永远不会换行,并且可以自上而下地使用

显而易见的候选者,与JPanels没有很好的合作。考虑下面两个例子:

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class App
{
  public static void main(String[] args)
  {
    JFrame window = new JFrame();
    Box box = new Box(BoxLayout.Y_AXIS);
    for(int i = 0; i < 5; ++i)
    {
      JLabel label = new JLabel("XX");
      box.add(label);
    }
    box.add(Box.createVerticalGlue());
    window.add(box);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
  }
}
import javax.swing.Box;
导入javax.swing.BoxLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
类应用程序
{
公共静态void main(字符串[]args)
{
JFrame窗口=新JFrame();
长方体=新长方体(长方体布局Y_轴);
对于(int i=0;i<5;++i)
{
JLabel标签=新的JLabel(“XX”);
添加(标签);
}
添加(box.createVerticalGlue());
窗口。添加(框);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
这将正确显示标签的垂直线,从顶部开始,并在标签占用空间时向底部延伸。好

但是,只需稍加修改:

  public static void main(String[] args)
  {
    JFrame window = new JFrame();
    Box box = new Box(BoxLayout.Y_AXIS);
    for(int i = 0; i < 5; ++i)
    {
      JLabel label = new JLabel("XX");
      JPanel panel = new JPanel();
      panel.add(label);
      box.add(panel);
    }
    box.add(Box.createVerticalGlue());
    window.add(box);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
  }
publicstaticvoidmain(字符串[]args)
{
JFrame窗口=新JFrame();
长方体=新长方体(长方体布局Y_轴);
对于(int i=0;i<5;++i)
{
JLabel标签=新的JLabel(“XX”);
JPanel面板=新的JPanel();
面板。添加(标签);
框。添加(面板);
}
添加(box.createVerticalGlue());
窗口。添加(框);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
这会将长方体的所有组件拉伸到相同的高度,使标签彼此远离。糟糕

重写JPanel的getPreferredSize和getMaximumSize方法(使用getMinimumSize)没有任何效果,而且是一种不好的修复方法,因为它依赖于组件,而不是容器及其布局


附录:
下面是使用GroupLayout已经相当成功的尝试。不幸的是,设计师似乎没有想到,在默认大小和首选大小中,选择最小大小是个好主意

此外,如果可以反转GroupLayout.SequentialGroup的顺序,那么API将无法帮助您了解如何操作。我当然不知道如何扩展这个类

import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class LineLayout extends GroupLayout
{
  public LineLayout(Container owner, int axis)
  {
    super(owner);
    this.direction = axis;
    this.direction |= owner.getComponentOrientation() != ComponentOrientation.LEFT_TO_RIGHT
                    ? LineLayout.RIGHT_TO_LEFT : LineLayout.LEFT_TO_RIGHT;
    this.setupGroups();
  }
  public LineLayout(Container owner, int axis, int orientation)
  {
    super(owner);
    this.direction = axis;
    this.direction |= orientation;
    this.setupGroups();
  }

  @Override // to replicate FlowLayout functionality : this method is called from owner.add
  public void addLayoutComponent(Component component, Object constraints)
  {
    if(constraints == null)
    {
      // REALLY surprised that this works, considering that overriding the JPanel's
      // getMaximumSize method with getPreferredSize had no effect
      this.horizontal.addComponent(component, GroupLayout.DEFAULT_SIZE,
                                              GroupLayout.DEFAULT_SIZE,
                                              GroupLayout.PREFERRED_SIZE);
      this.vertical.addComponent  (component, GroupLayout.DEFAULT_SIZE,
                                              GroupLayout.DEFAULT_SIZE,
                                              GroupLayout.PREFERRED_SIZE);
    }
    // TODO: else
  }

  protected void setupGroups()
  {
    super.setAutoCreateGaps(false); // does nothing
    if((this.direction & LineLayout.AXIS) == LineLayout.Y_AXIS)
    {
      this.horizontal = super.createParallelGroup();
      this.vertical   = (this.direction & LineLayout.ORIENTATION) == LineLayout.RIGHT_TO_LEFT
                      ? this.createSequentialInvertedGroup() : super.createSequentialGroup();
    }
    else
    {
      this.horizontal = (this.direction & LineLayout.ORIENTATION) == LineLayout.RIGHT_TO_LEFT
                      ? this.createSequentialInvertedGroup() : super.createSequentialGroup();
      this.vertical   = super.createParallelGroup();
    }
    super.setHorizontalGroup(this.horizontal);
    super.setVerticalGroup  (this.vertical);
  }

  // How!?
  // protected LineLayout.SequentialInvertedGroup createSequentialInvertedGroup() { return new LineLayout.SequentialInvertedGroup(); }
  protected GroupLayout.SequentialGroup createSequentialInvertedGroup() { return super.createSequentialGroup(); } // placeholder

  protected int direction;
  protected GroupLayout.Group horizontal;
  protected GroupLayout.Group vertical;

  // not sure how reliable the constant field values of BoxLayout are, whether it's smart to assume them unchanging over the ages
  public static final int AXIS = 0b1;
  public static final int X_AXIS = 0b0; // = BoxLayout.X_AXIS;
  public static final int Y_AXIS = 0b1; // = BoxLayout.Y_AXIS;
  public static final int ORIENTATION = 0b10;
  public static final int LEFT_TO_RIGHT = 0b00; // also top to bottom
  public static final int RIGHT_TO_LEFT = 0b10; // also bottom to top

  // No idea how; only has "add" methods; cannot actually do anything with the added components!?
  //protected static class SequentialInvertedGroup extends GroupLayout.SequentialGroup
  //{}
}

class Applikation
{
  public static void main(String[] args)
  {
    JFrame window = new JFrame();
    JPanel box = new JPanel();
    box.setLayout(new LineLayout(box, LineLayout.Y_AXIS));
    for(int i = 0; i < 5; ++i)
    {
      JLabel label = new JLabel("XX");
      JPanel panel = new JPanel();
      panel.add(label);
      box.add(panel);
    }
    window.add(box);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
  }
}
导入java.awt.Component;
导入java.awt.ComponentOrientation;
导入java.awt.Container;
导入javax.swing.GroupLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
类LineLayout扩展了GroupLayout
{
公共线布局(容器所有者,int轴)
{
超级(业主);
这个方向=轴;
this.direction |=owner.getComponentOrientation()!=ComponentOrientation.LEFT\u到\u
?LineLayout.RIGHT_至_LEFT:LineLayout.LEFT_至_RIGHT;
这是setupGroups();
}
公共线布局(容器所有者、内部轴、内部方向)
{
超级(业主);
这个方向=轴;
这个方向|=方向;
这是setupGroups();
}
@重写//以复制FlowLayout功能:此方法从owner.add调用
public void addLayoutComponent(组件组件、对象约束)
{
if(约束==null)
{
//考虑到超越JPanel的
//使用getPreferredSize的getMaximumSize方法没有效果
this.horizontal.addComponent(组件,GroupLayout.DEFAULT_大小,
GroupLayout.DEFAULT\u大小,
组布局。首选尺寸);
this.vertical.addComponent(组件,GroupLayout.DEFAULT_大小,
GroupLayout.DEFAULT\u大小,
组布局。首选尺寸);
}
//托多:还有别的吗
}
受保护的组()
{
super.setAutoCreateGaps(false);//不执行任何操作
if((this.direction&LineLayout.AXIS)==LineLayout.Y\u轴)
{
this.horizontal=super.createParallelGroup();
this.vertical=(this.direction&LineLayout.ORIENTATION)=LineLayout.RIGHT\u到\u LEFT
?this.createSequentialInvertedGroup():super.createSequentialGroup();
}
其他的
{
this.horizontal=(this.direction&LineLayout.ORIENTATION)=LineLayout.RIGHT\u到\u LEFT
?this.createSequentialInvertedGroup():super.createSequentialGroup();
this.vertical=super.createParallelGroup();
}
super.setHorizontalGroup(this.horizontal);
super.setVerticalGroup(此.vertical);
}
//怎么啦!?
//受保护的LineLayout.SequentialInvertedGroup CreateSquentialInvertedGroup(){返回新的LineLayout.SequentialInvertedGroup();}
受保护的GroupLayout.SequentialGroup CreateSquentialInvertedGroup(){return super.CreateSquentialGroup();}//占位符
保护int方向;
受保护的组布局。组水平;
受保护的组布局。组垂直;
//不确定BoxLayout的常量字段值有多可靠,假设它们随着时间的推移保持不变是否明智
公共静态最终int轴=0b1;
公共静态最终int X_轴=0b0;/=BoxLayout.X_轴;
公共静态最终输入Y_轴=0b1;/=BoxLayout.Y_轴;
公共静态最终整数方向=0b10;
public static final int LEFT_TO_RIGHT=0b00;//也是从上到下
public static final int RIGHT_TO_LEFT=0b10;//也从下到上
//不知道怎么做;只有“添加”方法;实际上无法对添加的组件执行任何操作!?
//受保护的静态类SequentialInvertedGroup扩展了GroupLayout.SequentialGroup
//{}
}
课堂应用
{
公共静态void main(字符串[]args)
{
JFrame窗口=新JFrame();
JPanel box=新的JPanel();
box.setLayout(新的LineLayout(box,LineLayout.Y_轴));
对于(int i=0;i<5;++i)
{
杰拉贝
private static int MAX_HEIGHT = 40;
private static final Dimension DIMENSION = new Dimension(Integer.MAX_VALUE, MAX_HEIGHT); 
public static void main(String[] args)
{
JFrame window = new JFrame();
Box box = new Box(BoxLayout.Y_AXIS){
    private static final long serialVersionUID = 1L;

    @Override
    public Component add(Component comp) {
        comp.setMaximumSize(DIMENSION);
        return super.add(comp);
    }
};
for(int i = 0; i < 5; ++i)
{
  JLabel label = new JLabel("XX");
  JPanel panel = new JPanel();
  panel.add(label);
  box.add(panel);
}
window.add(box);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
panel.setMaximumSize(new Dimension(100,20));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));