简体   繁体   English

Java Swing:BorderLayout中的SpringLayout

[英]Java Swing: SpringLayout within BorderLayout

I'm having problems with my GridLayout whereby a JTextField widens to fit an entry longer than the initial size. 我的GridLayout出现问题,即JTextField扩展为适合条目的长度超过了初始大小。 Another poster with this problem was told SpringLayout was the easiest solution, so that's what I'm trying. 另一个有这个问题的发贴人说,SpringLayout是最简单的解决方案,所以这就是我正在尝试的方法。 However I'm having problems getting this panel to even display. 但是我在使该面板均匀显示方面遇到问题。

I've top, bottom, left, and right panels each with different layouts that are set in a top-level panel (the only one in this frame). 我在顶部,底部,左侧和右侧面板中分别设置了不同的布局,这些布局在顶级面板中设置(此框架中只有一个)。 This SpringLayout I'm trying is to be in the right panel (added to the top panel as topPanel.add(springPanel, BorderLayout.EAST - this was how I did it when this panel was a GridLayout). The code below is modeled after the SpringLayout Oracle tutorial. Sorry I'm new to this layout and am only using it to use fixed widths for JTextFields. 我正在尝试的SpringLayout位于右侧面板中(作为topPanel.add(springPanel,BorderLayout.EAST-添加到顶部面板中,这是我在此面板为GridLayout时的操作方式)。下面的代码如下抱歉,我是此布局的新手,并且仅将其用于JTextFields的固定宽度。

JPanel testPanel = new JPanel();
SpringLayout layout = new SpringLayout();
testPanel.setLayout(layout);
JLabel label = new JLabel("First field: ");
JTextField field = new JTextField("enter text");

testPanel.add(label);
testPanel.add(field);

layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, testPanel);
layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, testPanel);
layout.putConstraint(SpringLayout.WEST, field, 5, SpringLayout.EAST, label);
layout.putConstraint(SpringLayout.NORTH, field, 5, SpringLayout.NORTH, testPanel);

...

topPanel.add(testPanel, BorderLayout.EAST);

Any guidance would be appreciated. 任何指导将不胜感激。 Thanks. 谢谢。

EDIT: Adding the cols argument doesn't seem to work, but I'll keep trying. 编辑:添加cols参数似乎不起作用,但我会继续尝试。 The same problem exists with GridBagLayout. GridBagLayout存在相同的问题。 Tabbing to the next field doesn't re-size the previous field, but clicking outside those fields elsewhere in the frame causes it to expand the length of its containing text which is what I need to avoid. 切换到下一个字段不会重新调整上一个字段的大小,但是在框架中其他位置的这些字段之外单击会导致它扩大其包含文本的长度,这是我需要避免的。 And I don't have enough rep pts to post images of what I'm doing 而且我没有足够的代表来张贴我正在做的事情的图像

EDIT2: Adding cols works - I tried that at some point. EDIT2:添加cols的工作原理-我在某些时候尝试过。 Not entirely sure how it's working, but any value (1, 10, 15 tested) seems to fix the length so leaving that panel's focus no longer causes a size change. 不能完全确定它是如何工作的,但是任何值(已测试1、10、15)似乎都可以固定长度,因此离开该面板的焦点不再导致尺寸改变。 Thanks! 谢谢!

@Andrew Thompson's answer worked - use col arg to specify # of columns (any number seems to work). @Andrew Thompson的答案有效-使用col arg指定列数(任何数字似乎都可以)。 Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM