- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下情况。我使用 GridBagLayout 创建了一个具有以下比例的 JPanel。我想弄清楚这一点,然后我将添加更多组件。现在我有类似的东西
------------------------------------
|LABEL 45% | LABEL 10% | LABEL 45%|
------------------------------------
每个标签水平和垂直各占用 1 个单元格(网格宽度/高度 = 1)
权重 (x) 为 0.45、0.1 和 0.45(从左到右)
权重 (y) 为 0.0
所有标签均已填充 GBC.HORIZONTAL, anchor 为 WEST、WEST、EAST。
JPanel topPanel = new JPanel(new GridBagLayout());
topPanel.add(new JLabel("1"), new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
topPanel.add(new JLabel("2"), new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
topPanel.add(new JLabel("3"), new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
此时一切正常,第一个标签获得与右侧标签完全相同的空间(即可用宽度的 45%,另一个标签获得 10%)。完美。
但是,当我尝试添加另一个带有 GridBagLayout 的 JPanel,而不是标签(左/右)时,比例不会按照定义保持。
在左侧我想添加一个JPanel,其定义如下
----------------------
|JLabel..............|
|--------------------|
|JTextArea...........|
|....................|
----------------------
JLabel 的权重为 0.0(对于 x/y),WEST anchor ,并填充 NONE
JTextArea - 权重 1.0(x)、1.0、WEST anchor ,填充两者(以便拉伸(stretch))
myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE", new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
在右侧,我想添加一个带有 GridBagLayout 的 JPanel,其中包含另外两个带有 GridBagLaouts 的 JPanel(现在没有必要描述如何定义)。
--> 因此,当我添加左侧面板而不是标签时,它会占据屏幕的 60% 左右(这是不正确的,它应该占据 45%)。
你能发现哪里出了问题吗?我没主意了
LE:这是一个简单的例子
public class Example extends JFrame {
public Example() {
init();
}
private JPanel createPanel(Component comp) {
JPanel topPanel = new JPanel(new GridBagLayout());
topPanel.add(comp, new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
topPanel.add(new JLabel("2"), new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
topPanel.add(new JLabel("3"), new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
return topPanel;
}
private JPanel getPanelWithLabel() {
return createPanel(new JLabel("1"));
}
private JPanel getPanelWithPanel() {
JPanel myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
return createPanel(myPanel);
}
private void init() {
setTitle("Simple example");
setSize(800, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
panel.add(getPanelWithLabel(), BorderLayout.CENTER);
//panel.add(getPanelWithPanel(), BorderLayout.CENTER);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Example ex = new Example();
}
});
}
}
最佳答案
问题出在 JTextArea
的行上。
由于 25
行比 45% 宽,因此它会自行扩展。
我不会使用 25
,而是使用容器 (myPanel
) 的 WIDTH
myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
<小时/>
因为用这种方法尺寸仍然不同。我尝试首先创建一个 JPanel
并向其添加 JLabel
。
JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("3"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
// We add myPanel and two(JLabel) to topPanel first and than the below.
topPanel.add(threeCont, new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
<小时/>
System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());
First Panel's Size : java.awt.Dimension[width=441,height=96]
Second Panel's Size : java.awt.Dimension[width=98,height=96]
Third Panel's Size : java.awt.Dimension[width=441,height=16]
<小时/>
public static void main(String[]a) {
JFrame fr = new JFrame();
fr.setSize(new Dimension(1000,1000));
fr.setLocationRelativeTo(null);
JPanel topPanel = new JPanel(new GridBagLayout());
Insets INSETS_0PX = new Insets(0,0,0,0);
JLabel one = new JLabel("1");
JLabel two = new JLabel("2");
one.setBackground(Color.yellow);
two.setBackground(Color.blue);
JPanel myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
topPanel.add(myPanel, new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
topPanel.add(two, new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
topPanel.add(threeCont, new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
fr.add(topPanel);
fr.setVisible(true);
System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());
}
关于java - GridBagLayout 中的 JPanel 不保持比例(weightx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802679/
我正在将第一个 GridBagLayout 设置为主 JPanel。在此面板中,还使用此布局添加了其他十个面板(外部约束)。子面板包含大量文本,应使用新布局和 innerConstraints 将其放
我的任务是使用 Swing 在 Java 中设计一个基本的 UI,但我无法完成它的布局。我想创建类似于 this 的东西但是我使用 GridBagLayout 的尝试导致了一些非常困惑的事情。谁能提供
我有以下代码 GridBagConstraints c = new GridBagConstraints(); c.gridx=0; c.gridy=0; c.gridwidt
我正面临 Java GridBagLayout 的问题。 运行以下代码: import java.awt.GridBagConstraints; import java.awt.GridBagLayo
我正在使用 GridBagLayout到(当前)显示两行。我知道这种布局对于这项任务来说太过分了,但我正在努力学习如何使用它。问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大差距(请参见
我正在使用 swing 尝试制作一个垄断板。我试图将板上的属性/空间制作为单独的 JPanel,它们可以有自己的实现、信息等,但我在空间的形状方面遇到了一些问题。我正在使用 GridBagLayout
我正在开发我的第一个应用程序来练习布局管理器,我开始学习 GridBagLayout,我想创建一个带有 Logo 和登录框的登录窗口。 目前我的应用程序如下所示: (来源:gyazo.com) 我觉得
我正在开发的 UI 显示一个面板,让用户选择电影并播放。有播放、暂停等控件。 布局似乎是我想要的。该面板使用 GridBagLayout。第 2 行显示状态消息的文本区域,第 3 行显示带有按钮和进度
我有一个面板,用作卡片布局的一部分。该面板使用 GridBagLayout。我向其中添加了两个组件:填充设置为 BOTH 的 JTextArea 和填充设置为水平的 JTextField。它们只占据水
我的问题是当我不乱用 ipadx 和 ipady 时。我的按钮粘在一起,但当我摆弄 ipadx 和 ipady 时,我发现第三个按钮将一个单元格移开。为什么以及如何解决这个困惑?代码: public
我想缩短我的文本字段,这样它就不会延伸到我的 jframe 的末尾,所以它现在看起来是这样的: 如何控制文本字段的宽度,使其不会像我尝试过 setPreferedSize() 和 setSize()
尝试将我的 JTable 旁边的表单与 GridBagLayout 对齐,但我正在努力将我的组件放在面板顶部。我需要价格标签和字段位于商品标签和字段下方,但我无法将其从面板底部移动。 如果我使用 an
我正在使用 GridBagLayout 测试一个简单的表单,但遇到了一些对齐问题。我想在顶部“Item”行下方的行上放置两个小字段,但长文本字段导致其下方的小文本字段无法正确对齐。 这是它当前正在执行
是否可以有一个像这张图一样的GridBagLayout? (本例中为多列 4 行 + 一列 5 行) 计算器的GridBagLayout: 最佳答案 对于最后一列,创建一个使用 GridLayout
我正在尝试学习如何使用JAVA的GUI库Swing。我想做的是将 2 个磁盘图像排成一行,并将 3 个打印机图像放在它们下面的行中。每个磁盘镜像应位于两个打印机镜像的中间。我(不幸的是)正在使用 Gr
我有一个 JPanel ,其中 GridBagLayout 作为布局管理器,我正在尝试进行这种安排: 忽略边框的额外深蓝色空间。 我总共有 5 列和 3 行,所有组件都将 setPreferredSi
如何为 GridBagLayout 中的不同行设置不同的颜色? P.S:我无法根据客户要求在我的应用程序中使用 swing。 最佳答案 对于 AWT 也适用 setBackground(Color)
我在 GridBagLayout 中有一个 3 * 6 的 JButtons 数组。但由于每个按钮的文本长度可能有所不同,因此每列按钮的宽度略有不同。如何使它们具有相同的宽度? 这是代码: for (
请看下面的代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestForm exte
我正在为一些按钮设置布局。我想在中间有 2 个按钮,在最后有一个。我有两个在中间,但最后一个在一边。如何将“后退”按钮设置在其他按钮下方。 (我对此进行了研究)。 public class Optio
我是一名优秀的程序员,十分优秀!