如何给面板内的列固定列宽?
How to give columns inside a panel fixed column widths?
如果我想在面板内有一组具有固定列跨度的列,最好的 Swing 布局是什么?
如下所示:
我希望第一列跨越面板宽度的 20%,第二列跨越面板宽度的 50%,第三列跨越面板宽度的 30%
在 Oracle's 网站上的 Java Swing 中有一个很棒的布局视觉指南。
对于你的情况,GridBagLayout would be your best option even though it's the most complex. There are other layouts that you could get away with although it depends on what you are actually trying to do. You can look at the GridLayout, the BoxLayout, the BorderLayout and even the default layout (FlowLayout) 可以。
您也可以查看 JTable,因为您写的是列,我注意到左侧的小圆圈看起来像索引号或其他东西,但我可能是错的。也许你正在尝试做某种 table...
我的结果:
您可以使用 MigLayout 库,它对 java 布局很有用
对于我的示例代码
panel.setLayout(new MigLayout());
panel.add(new JTextField(), "w 20%");
panel.add(new JTextField(), "w 50%");
panel.add(new JTextField(), "w 30%");
如果我想在面板内有一组具有固定列跨度的列,最好的 Swing 布局是什么?
如下所示:
我希望第一列跨越面板宽度的 20%,第二列跨越面板宽度的 50%,第三列跨越面板宽度的 30%
在 Oracle's 网站上的 Java Swing 中有一个很棒的布局视觉指南。
对于你的情况,GridBagLayout would be your best option even though it's the most complex. There are other layouts that you could get away with although it depends on what you are actually trying to do. You can look at the GridLayout, the BoxLayout, the BorderLayout and even the default layout (FlowLayout) 可以。
您也可以查看 JTable,因为您写的是列,我注意到左侧的小圆圈看起来像索引号或其他东西,但我可能是错的。也许你正在尝试做某种 table...
我的结果:
您可以使用 MigLayout 库,它对 java 布局很有用 对于我的示例代码
panel.setLayout(new MigLayout());
panel.add(new JTextField(), "w 20%");
panel.add(new JTextField(), "w 50%");
panel.add(new JTextField(), "w 30%");