- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个 Java 应用程序,目前我使用这个布局管理器:
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
/**
* @author Pasban
*/
public class VerticalLayout implements LayoutManager {
public final static int CENTER = 0;
public final static int RIGHT = 1;
public final static int LEFT = 2;
public final static int BOTH = 3;
public final static int TOP = 1;
public final static int BOTTOM = 2;
private int vgap;
private int alignment;
private int anchor;
public VerticalLayout() {
this(5, CENTER, TOP);
}
public VerticalLayout(int vgap) {
this(vgap, CENTER, TOP);
}
public VerticalLayout(int vgap, int alignment) {
this(vgap, alignment, TOP);
}
public VerticalLayout(int vgap, int alignment, int anchor) {
this.vgap = vgap;
this.alignment = alignment;
this.anchor = anchor;
}
private Dimension layoutSize(Container parent, boolean minimum) {
Dimension dim = new Dimension(0, 0);
Dimension d;
synchronized (parent.getTreeLock()) {
int n = parent.getComponentCount();
for (int i = 0; i < n; i++) {
Component c = parent.getComponent(i);
if (c.isVisible()) {
d = minimum ? c.getMinimumSize() : c.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
dim.height += d.height;
if (i > 0) {
dim.height += vgap;
}
}
}
}
Insets insets = parent.getInsets();
dim.width += insets.left + insets.right;
dim.height += insets.top + insets.bottom + vgap + vgap;
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
synchronized (parent.getTreeLock()) {
int n = parent.getComponentCount();
Dimension pd = parent.getSize();
int y = 0;
for (int i = 0; i < n; i++) {
Component c = parent.getComponent(i);
Dimension d = c.getPreferredSize();
if (c.isVisible()) {
y += d.height + vgap;
}
}
y -= vgap;
if (anchor == TOP) {
y = insets.top;
} else if (anchor == CENTER) {
y = (pd.height - y) / 2;
} else {
y = pd.height - y - insets.bottom;
}
for (int i = 0; i < n; i++) {
Component c = parent.getComponent(i);
Dimension d = c.getPreferredSize();
if (!c.isVisible()) {
continue;
}
int x = 1;
int wid = pd.width - 3;
c.setBounds(x, y, wid, d.height);
y += d.height + vgap;
}
}
}
public Dimension minimumLayoutSize(Container parent) {
return layoutSize(parent, false);
}
public Dimension preferredLayoutSize(Container parent) {
return layoutSize(parent, false);
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public String toString() {
return getClass().getName() + "[vgap=" + vgap + " align=" + alignment + " anchor=" + anchor + "]";
}
}
使用示例(组件垂直对齐):
如您所见,每个组件都在一行上。但是,我希望一些组件在一条线上有两个。
例如,JButtons 将位于一行,就像现在一样。 JTextAreas 也是如此。
但是,我希望 JLabels 和 JTextFields 位于同一行。 JLabels 和 JComboBox 也一样,位于同一行。
我真的不知道该怎么做。有人能给我解释一下吗? :D
最佳答案
首先,真的很难理解为什么要创建自己的布局?看看GridBagLayout
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
使用 GridBagLayout 可以轻松满足您所解释的用例。
关于java - Swing - 更改 LayoutManager : two components per line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42316347/
根据谷歌的 RecyclerView documentation ,您可以通过在 RecyclerView 的“layoutManager”属性中指定其类名,从布局文件中设置特定的 LayoutMan
运行实例化此类的代码时: static final class MyFrame extends JFrame { private CardLayout layout = new CardLay
我有一个带有卡片列表的 RecyclerView。我想知道是否可以通过编程将 RecyclerView 的 LayoutManager 更改为 Linear(在使用手机时)和 StaggeredGri
我在尝试为我的 recyclerview 设置布局管理器时遇到此错误。我已经创建了一个布局管理器并设置了我的 recyclerview 来使用它。由于某种原因,layoumanager 为空。 jav
前段时间我读到这个article显示了一种在 Swing 中实现鼠标可调整大小的组件的方法。 作者使用空 LayoutManager 以允许绝对组件定位。我知道不应该使用 null 布局,所以我的问题
请看附图。我是 Java GUI 的初学者,希望有人能指导我为这样的 GUI 选择布局管理器。我知道我可能必须使用嵌套的布局管理器,但我不确定哪个可以帮助我完成这项工作。 最佳答案 这是一个想法: 在
我想像这样实现一个 ConcatAdapter : 此页面的 recyclerView 包含以下元素: 标题 水平recyclerView(带分页) 标题 垂直recyclerView(无分页) 标题
所以,我对 LayoutManager 完全陌生。我已经阅读过有关现有 LayoutManager 的信息,但没有了解最好的方法是获得这样的布局。不同的颜色框代表 JPanel。 我应该使用哪一个?
我正在学习在 GUI 中定位对象的不同方法,但我不太明白为什么需要转换为 LayoutManager 才能使用 setLayout 方法。是否需要实现 LayoutManager 接口(interfa
我正在尝试对 UITextView 的 layoutManager 进行子类化 但不断发生崩溃。我一直在看的示例是 Objective C 中的,所以我对如何更改布局管理器感到有点困惑 这是我的代码.
我正在开发一个应用程序,并且有一个需要自定义布局管理器的面板。为了测试我的 LayoutManager 实现,我希望内置一些 Debug模式,非常类似于 MigLayout 的 Debug模式,它在
我正在尝试在我的应用程序中包含一个 SwipeRefreshLayout。它可以工作,但是当我关闭我的 WIFI 时(我的应用程序使用它,如果没有 wifi,我想包括警报)。任何人有任何建议为什么我得
我有一段文字,它在屏幕上的显示完全像这样: #first SomePersonFirstName SomePersonLastName #secondTag 在我的应用程序中,我保留了有关此文本的 3
如何防止 GUI 组件在 BorderLayout 管理器中调整大小时缩小?是否可以设置最小尺寸以使组件不会缩小超过它?似乎使用 setBounds 不适用于布局管理器。 还有一个问题:如果我使用嵌套
我无法找到为什么我们需要为 RecycleView 设置 LayoutManager 的解释。有人可以解释吗? mLayoutManager = new LinearLayoutManager(thi
我正在尝试编译这个非常简单的 BoxLayout 管理器测试,但我不断收到此错误:“Container 类型中的方法 setLayout(LayoutManager) 不适用于我的行面板上的参数 (L
我想利用 JFrame 的 Container 的默认布局:LayoutManager,避免使用 专门化它>FlowLayout ¹. 所以我写了这段代码: LayoutManager layout_
我用这样的 StaggeredGridLayoutManager 制作了 RecyclerView: 我需要在上面添加一些 TextView(像 LinearLayoutManager 中的一个列)。
我正在尝试构建一个涉及 RecyclerView 或 GridView 的 UI,我想在其中根据动态填充的项目的数量和宽度自动设置列数。 这是我到目前为止所取得的成就的形象...... 这是我想要实现
我正在使用 RecyclerView 和 StaggeredGridLayoutManager。 我希望这个 StaggeredGridLayoutManager 或任何一个 LayoutManage
我是一名优秀的程序员,十分优秀!