- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如标题中所述,我一直在尝试设置一种带有水平滚动的垂直流布局。布局中的组件将是 JLabel。让我画一张图:
+-------------------------+ <--- window
|Label1 Label4 Label7|
|Label2 Label5 Label8| <--- labels
|Label3 Label6 Label9|
|<===============>========| <--- scrollbar
+-------------------------+
同一个窗口,垂直展开
+--------------------------+ <--- window
|Label1 Label5 Label9 |
|Label2 Label6 Label10| <--- labels
|Label3 Label7 Label11|
|Label4 Label8 Label12|
|<===================>=====| <--- scrollbar
+--------------------------+
因此,标签将填充可用的垂直空间,然后创建一个新列。一旦可用的水平空间用完,就会出现一个水平滚动条。
通常不应出现垂直滚动条;但是,如果窗口的垂直高度异常小,那么最好有一个垂直滚动条。
非常感谢任何帮助。我是 Java 的新手,所以任何额外的解释都会很棒。谢谢!
编辑:
根据以下回复,我现在正在处理: http://tips4java.wordpress.com/2008/11/06/wrap-layout/和 http://code.google.com/p/verticalflowlayout/
我有这样的 WrapLayout 扩展 VerticalFlowLayout:
package LogicSim;
import java.awt.*;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
/**
* FlowLayout subclass that fully supports wrapping of components.
*/
public class VerticalWrapLayout extends VerticalFlowLayout
{
private Dimension preferredLayoutSize;
/**
* Constructs a new <code>WrapLayout</code> with a left
* alignment and a default 5-unit horizontal and vertical gap.
*/
public VerticalWrapLayout()
{
super();
}
/**
* Constructs a new <code>FlowLayout</code> with the specified
* alignment and a default 5-unit horizontal and vertical gap.
* The value of the alignment argument must be one of
* <code>WrapLayout</code>, <code>WrapLayout</code>,
* or <code>WrapLayout</code>.
* @param align the alignment value
*/
public VerticalWrapLayout(int align)
{
super(align);
}
/**
* Creates a new flow layout manager with the indicated alignment
* and the indicated horizontal and vertical gaps.
* <p>
* The value of the alignment argument must be one of
* <code>WrapLayout</code>, <code>WrapLayout</code>,
* or <code>WrapLayout</code>.
* @param align the alignment value
* @param hgap the horizontal gap between components
* @param vgap the vertical gap between components
*/
public VerticalWrapLayout(int align, int hgap, int vgap)
{
super(align, hgap, vgap);
}
/**
* Returns the preferred dimensions for this layout given the
* <i>visible</i> components in the specified target container.
* @param target the component which needs to be laid out
* @return the preferred dimensions to lay out the
* subcomponents of the specified container
*/
@Override
public Dimension preferredLayoutSize(Container target)
{
return layoutSize(target, true);
}
/**
* Returns the minimum dimensions needed to layout the <i>visible</i>
* components contained in the specified target container.
* @param target the component which needs to be laid out
* @return the minimum dimensions to lay out the
* subcomponents of the specified container
*/
@Override
public Dimension minimumLayoutSize(Container target)
{
Dimension minimum = layoutSize(target, false);
minimum.width -= (getHgap() + 1);
return minimum;
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred)
{
synchronized (target.getTreeLock())
{
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0)
targetWidth = Integer.MAX_VALUE;
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth)
{
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0)
{
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null)
{
dim.width -= (hgap + 1);
}
return dim;
}
}
/*
* A new row has been completed. Use the dimensions of this row
* to update the preferred size for the container.
*
* @param dim update the width and height when appropriate
* @param rowWidth the width of the row to add
* @param rowHeight the height of the row to add
*/
private void addRow(Dimension dim, int rowWidth, int rowHeight)
{
dim.width = Math.max(dim.width, rowWidth);
if (dim.height > 0)
{
dim.height += getVgap();
}
dim.height += rowHeight;
}
}
这是我的框架设置:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout( new VerticalWrapLayout(0) );
JScrollPane pane = new JScrollPane(panel);
frame.add( pane, BorderLayout.CENTER );
for (int i=0; i < 80; i++ ) {
panel.add( new JLabel( "Label" + i ) );
}
现在,这会按照我想要的方式在垂直列中设置标签,但它仍然会创建垂直滚动条。在修改 VerticalWrapLayout 类时,我很犹豫。另外,我真的不明白 JScrollPane 如何与这些类交互。关于如何进行的任何建议?
已解决!请参阅下面的答案以及我的答案。
最佳答案
这里是一个 VerticalFlowLayout 的例子。它是 FlowLayout 类的副本,其中一些逻辑更改为“垂直”导向而不是“水平”导向:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A flow layout arranges components in a directional flow, much
* like lines of text in a paragraph. The flow direction is
* determined by the container's <code>componentOrientation</code>
* property and may be one of two values:
* <ul>
* <li><code>ComponentOrientation.TOP_TO_BOTTOM</code>
* <li><code>ComponentOrientation.BOTTOM_TO_TOP</code>
* </ul>
* Flow layouts are typically used
* to arrange buttons in a panel. It arranges buttons
* horizontally until no more buttons fit on the same line.
* The line alignment is determined by the <code>align</code>
* property. The possible values are:
* <ul>
* <li>{@link #TOP TOP}
* <li>{@link #BOTTOM BOTTOM}
* <li>{@link #CENTER CENTER}
* <li>{@link #LEADING LEADING}
* <li>{@link #TRAILING TRAILING}
* </ul>
* <p>
*/
public class VerticalFlowLayout implements LayoutManager, java.io.Serializable
{
/**
* This value indicates that each row of components
* should be left-justified.
*/
public static final int TOP = 0;
/**
* This value indicates that each row of components
* should be centered.
*/
public static final int CENTER = 1;
/**
* This value indicates that each row of components
* should be right-justified.
*/
public static final int BOTTOM = 2;
/**
* <code>align</code> is the property that determines
* how each column distributes empty space.
* It can be one of the following three values:
* <ul>
* <code>TOP</code>
* <code>BOTTOM</code>
* <code>CENTER</code>
* </ul>
*
* @see #getAlignment
* @see #setAlignment
*/
int align; // This is the one we actually use
/**
* The flow layout manager allows a seperation of
* components with gaps. The horizontal gap will
* specify the space between components and between
* the components and the borders of the
* <code>Container</code>.
*
* @see #getHgap()
* @see #setHgap(int)
*/
int hgap;
/**
* The flow layout manager allows a seperation of
* components with gaps. The vertical gap will
* specify the space between rows and between the
* the rows and the borders of the <code>Container</code>.
*
* @see #getHgap()
* @see #setHgap(int)
*/
int vgap;
/**
* Constructs a new <code>VerticalFlowLayout</code> with a centered alignment and a
* default 5-unit horizontal and vertical gap.
*/
public VerticalFlowLayout()
{
this(CENTER, 5, 5);
}
/**
* Constructs a new <code>VerticalFlowLayout</code> with the specified
* alignment and a default 5-unit horizontal and vertical gap.
* The value of the alignment argument must be one of
* <code>VerticalFlowLayout.TOP</code>, <code>VerticalFlowLayout.BOTTOM</code>,
* or <code>VerticalFlowLayout.CENTER</code>
* @param align the alignment value
*/
public VerticalFlowLayout(int align)
{
this(align, 5, 5);
}
/**
* Creates a new flow layout manager with the indicated alignment
* and the indicated horizontal and vertical gaps.
* <p>
* The value of the alignment argument must be one of
* <code>VerticalFlowLayout.TOP</code>, <code>VerticalFlowLayout.BOTTOM</code>,
* or <code>VerticalFlowLayout.CENTER</code>.
* @param align the alignment value
* @param hgap the horizontal gap between components
* and between the components and the
* borders of the <code>Container</code>
* @param vgap the vertical gap between components
* and between the components and the
* borders of the <code>Container</code>
*/
public VerticalFlowLayout(int align, int hgap, int vgap)
{
this.hgap = hgap;
this.vgap = vgap;
setAlignment(align);
}
/**
* Gets the alignment for this layout.
* Possible values are <code>VerticalFlowLayout.TOP</code>,
* <code>VerticalFlowLayout.BOTTOM</code> or <code>VerticalFlowLayout.CENTER</code>,
* @return the alignment value for this layout
* @see java.awt.VerticalFlowLayout#setAlignment
* @since JDK1.1
*/
public int getAlignment()
{
return align;
}
/**
* Sets the alignment for this layout. Possible values are
* <ul>
* <li><code>VerticalFlowLayout.TOP</code>
* <li><code>VerticalFlowLayout.BOTTOM</code>
* <li><code>VerticalFlowLayout.CENTER</code>
* </ul>
* @param align one of the alignment values shown above
* @see #getAlignment()
* @since JDK1.1
*/
public void setAlignment(int align)
{
this.align = align;
}
/**
* Gets the horizontal gap between components
* and between the components and the borders
* of the <code>Container</code>
*
* @return the horizontal gap between components
* and between the components and the borders
* of the <code>Container</code>
* @see java.awt.VerticalFlowLayout#setHgap
* @since JDK1.1
*/
public int getHgap() {
return hgap;
}
/**
* Sets the horizontal gap between components and
* between the components and the borders of the
* <code>Container</code>.
*
* @param hgap the horizontal gap between components
* and between the components and the borders
* of the <code>Container</code>
* @see java.awt.VerticalFlowLayout#getHgap
* @since JDK1.1
*/
public void setHgap(int hgap) {
this.hgap = hgap;
}
/**
* Gets the vertical gap between components and
* between the components and the borders of the
* <code>Container</code>.
*
* @return the vertical gap between components
* and between the components and the borders
* of the <code>Container</code>
* @see java.awt.VerticalFlowLayout#setVgap
* @since JDK1.1
*/
public int getVgap() {
return vgap;
}
/**
* Sets the vertical gap between components and between
* the components and the borders of the <code>Container</code>.
*
* @param vgap the vertical gap between components
* and between the components and the borders
* of the <code>Container</code>
* @see java.awt.VerticalFlowLayout#getVgap
*/
public void setVgap(int vgap) {
this.vgap = vgap;
}
/**
* Adds the specified component to the layout.
* Not used by this class.
* @param name the name of the component
* @param comp the component to be added
*/
public void addLayoutComponent(String name, Component comp) {
}
/**
* Removes the specified component from the layout.
* Not used by this class.
* @param comp the component to remove
* @see java.awt.Container#removeAll
*/
public void removeLayoutComponent(Component comp) {
}
/**
* Returns the preferred dimensions for this layout given the
* <i>visible</i> components in the specified target container.
*
* @param target the container that needs to be laid out
* @return the preferred dimensions to lay out the
* subcomponents of the specified container
* @see Container
* @see #minimumLayoutSize
* @see java.awt.Container#getPreferredSize
*/
public Dimension preferredLayoutSize(Container target)
{
synchronized (target.getTreeLock())
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
boolean firstVisibleComponent = true;
for (int i = 0 ; i < nmembers ; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
if (firstVisibleComponent)
{
firstVisibleComponent = false;
}
else
{
dim.height += vgap;
}
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
}
/**
* Returns the minimum dimensions needed to layout the <i>visible</i>
* components contained in the specified target container.
* @param target the container that needs to be laid out
* @return the minimum dimensions to lay out the
* subcomponents of the specified container
* @see #preferredLayoutSize
* @see java.awt.Container
* @see java.awt.Container#doLayout
*/
public Dimension minimumLayoutSize(Container target)
{
synchronized (target.getTreeLock())
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
boolean firstVisibleComponent = true;
for (int i = 0 ; i < nmembers ; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
dim.width = Math.max(dim.width, d.width);
if (firstVisibleComponent)
{
firstVisibleComponent = false;
}
else
{
dim.height += vgap;
}
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
}
/**
* Lays out the container. This method lets each
* <i>visible</i> component take
* its preferred size by reshaping the components in the
* target container in order to satisfy the alignment of
* this <code>VerticalFlowLayout</code> object.
*
* @param target the specified component being laid out
* @see Container
* @see java.awt.Container#doLayout
*/
public void layoutContainer(Container target)
{
synchronized (target.getTreeLock())
{
Insets insets = target.getInsets();
int maxHeight = target.getSize().height - (insets.top + insets.bottom + vgap*2);
int nmembers = target.getComponentCount();
int x = insets.left + hgap;
int y = 0;
int columnWidth = 0;
int start = 0;
boolean ttb = target.getComponentOrientation().isLeftToRight();
for (int i = 0 ; i < nmembers ; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getPreferredSize();
m.setSize(d.width, d.height);
if ((y == 0) || ((y + d.height) <= maxHeight))
{
if (y > 0)
{
y += vgap;
}
y += d.height;
columnWidth = Math.max(columnWidth, d.width);
}
else
{
moveComponents(target, x, insets.top + vgap, columnWidth, maxHeight - y, start, i, ttb);
y = d.height;
x += hgap + columnWidth;
columnWidth = d.width;
start = i;
}
}
}
moveComponents(target, x, insets.top + vgap, columnWidth, maxHeight - y, start, nmembers, ttb);
}
}
/**
* Centers the elements in the specified row, if there is any slack.
* @param target the component which needs to be moved
* @param x the x coordinate
* @param y the y coordinate
* @param width the width dimensions
* @param height the height dimensions
* @param columnStart the beginning of the column
* @param columnEnd the the ending of the column
*/
private void moveComponents(
Container target, int x, int y, int width, int height, int columnStart, int columnEnd, boolean ttb)
{
switch (align)
{
case TOP:
y += ttb ? 0 : height;
break;
case CENTER:
y += height / 2;
break;
case BOTTOM:
y += ttb ? height : 0;
break;
}
for (int i = columnStart ; i < columnEnd ; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
int cx;
cx = x + (width - m.getSize().width) / 2;
if (ttb)
{
m.setLocation(cx, y);
}
else
{
m.setLocation(cx, target.getSize().height - y - m.getSize().height);
}
y += m.getSize().height + vgap;
}
}
}
/**
* Returns a string representation of this <code>VerticalFlowLayout</code>
* object and its values.
* @return a string representation of this layout
*/
public String toString()
{
String str = "";
switch (align)
{
case TOP: str = ",align=top"; break;
case CENTER: str = ",align=center"; break;
case BOTTOM: str = ",align=bottom"; break;
}
return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
}
public static void main(String[] args)
{
JPanel main = new JPanel( new BorderLayout() );
final JPanel buttons = new JPanel(new VerticalFlowLayout() );
// buttons.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
main.add(buttons, BorderLayout.CENTER);
for (int i = 0; i < 7; i++)
{
buttons.add( new JRadioButton("button " + i) );
}
JButton button = new JButton("Add Radio Button");
main.add(button, BorderLayout.SOUTH);
button.addActionListener( new ActionListener()
{
private int i = 8;
public void actionPerformed(ActionEvent e)
{
buttons.add( new JRadioButton("button R Us" + i++) );
buttons.revalidate();
// pack();
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(main);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
理论上,您应该能够采用 WrapLayout 并让它扩展此类,然后自定义代码也可以垂直定向。
编辑:
I have the WrapLayout extending VerticalFlowLayout as such:
您不能只扩展 VerticalFlowLayout。 WrapLayout 代码旨在根据父容器的大小计算固定宽度。您需要更改计算固定高度的行为。我没试过,但基本上你需要将“宽度”相关的变量引用更改为“高度”,并将“高度”相关的变量引用更改为“宽度”,以便代码在垂直维度而不是水平维度上工作。
关于Java - 水平滚动的垂直 "FlowLayout",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8196530/
如果附加了 'not-scroll' 类,我希望我的 body 不滚动,否则它应该正常工作。 我已经搜索这个问题两天了,但找不到任何适合我的解决方案。 我想要的是向 body 添加一个 class,并
我发现似乎是 iOS Safari 中的错误(我正在 iOS 8 上进行测试)。当绝对定位的 iFrame 漂浮在一段可滚动内容上方时,滚动 iFrame 也会滚动下面的内容。以下 HTML (ava
我有以下代码来显示一系列投资组合图片,这些图片以 SVG 格式存储在滚动 div 中: 在 Safari 中滚动使用两根手指或鼠标滚轮当光标位于 SVG 之一上时不起作用。 该页
我想用 javascript 做的是: 一旦你向下滚动页面,将#sidebar-box-fixed 的位置从 position: relative; 更改为定位:固定;。改回position:rela
我对 Elasticsearch 的滚动功能有点困惑。在 elasticsearch 中,每当用户在结果集上滚动时,是否可以每次调用搜索 API?来自文档 "search_type" => "scan
我试图做到这一点,以便当我向上或向下滚动页面时,它会运行不同的相应功能。我发现了一个类似的问题here但我已经尝试了他们的答案并且没有运气。 注意:此页面没有正常显示的滚动条。没有地方可以滚动。 bo
(C语言,GTK库) 在我的表单上,我有一个 GtkDrawingArea 小部件,我在上面使用 Cairo 绘制 GdkPixbufs(从文件加载)。我想要完成的是能够在窗口大小保持固定的情况下使用
最近我一直在尝试创建一个拉到(刷新,加载更多)swiftUI ScrollView !!,灵感来自 https://cocoapods.org/pods/SwiftPullToRefresh 我正在努
我正在开发一个应用程序,其中有两个带有可放置区域的列表和一个带有可拖动项目的侧面菜单。 当我滚动屏幕时,项目的位置困惑。 我试图在谷歌上寻找一些东西,最后得到了这个问题:jQuery draggabl
我在 UIWebView 中加载了一个 HTML 表单,而我的 UIWebView 恰好从 View 的中间开始并扩展。我必须锁定此 webView 不滚动并将其放在 ScrollView 之上以允许
如何在每个元素而不是整个元素上应用淡入淡出(与其高度相比)? HTML: CSS: * { padding: 0; margin: 0; box-sizing: border
我想使用带有垂直轴的 PageView 并使用鼠标滚动在页面之间移动,但是当我使用鼠标滚动时页面不滚动...仅页面单击并向上/向下滑动时滚动。 有什么办法吗? 我想保留属性 pageSnapping:
我制作这个程序是为了好玩,但我被卡住了,因为程序在屏幕外运行。如何在不完全更改代码的情况下实现滚动条。 public static void main(String args[]) throws IO
我想使用带有垂直轴的 PageView 并使用鼠标滚动在页面之间移动,但是当我使用鼠标滚动时页面不滚动...仅页面单击并向上/向下滑动时滚动。 有什么办法吗? 我想保留属性 pageSnapping:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
使用 jquery 技术从 css-tricks.com 获得滚动/跟随侧边栏,如果您不知道我在说什么,这里是代码: $(function() { var $sidebar = $
我是 jQuery Mobile 新手。我需要向我的应用程序添加 Facebook 滑动面板功能。 我经历了 sliding menu panel ,它工作正常,但我在菜单面板中的内容超出了窗口大小,
有没有办法在 js 或 jQuery 或任何其他工具中检测 ctrl + 滚动。我正在尝试执行一些动态布局代码,我需要检测不同分辨率下的屏幕宽度,我通过使用 setTimeout() 的计时器实现了这
我有一部分html代码:
我想控制 RichTextBox 滚动,但在控件中找不到任何方法来执行此操作。 这样做的原因是我希望当鼠标光标位于 RichTextBox 控件上时鼠标滚轮滚动有效(它没有事件焦点:鼠标滚轮事件由表单
我是一名优秀的程序员,十分优秀!