GridBagLayout 变得疯狂
我有一个 GridBagLayout,但一个标签 (maxSizeLbl) 变得疯狂,而另一个标签 (maxDateLbl) 不可见,有人知道我做错了什么吗?
这是图片:
这是代码:
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchGUI {
public static void main(String args[]) {
JFrame frame = new JFrame("Search Files");
JPanel panel = new JPanel();
JTextField rootTF = new JTextField(20);
JTextField containsTF = new JTextField(20);
JTextField containsNotTF = new JTextField(20);
JTextField minSizeTF = new JTextField(10);
JTextField maxSizeTF = new JTextField(10);
JTextField suffixTF = new JTextField(10);
JTextField prefixTF= new JTextField(10);
JTextField minDateTF = new JTextField(10);
JTextField maxDateTF = new JTextField(10);
JCheckBox hiddenCB = new JCheckBox("search hidden");
JCheckBox recursiveCB = new JCheckBox("search subfolders");
JButton searchBtn = new JButton("search");
JLabel rootLbl = new JLabel("search in: ", JLabel.CENTER);
JLabel containsLbl = new JLabel("Filename contains: ", JLabel.CENTER);
JLabel containsNotLbl = new JLabel(" Filename contains not", JLabel.CENTER);
JLabel minSizeLbl = new JLabel("min. Size", JLabel.CENTER);
JLabel maxSizeLbl = new JLabel("max. Size", JLabel.CENTER);
JLabel suffixLbl = new JLabel("Filetypes", JLabel.CENTER);
JLabel prefixLbl = new JLabel("begins with", JLabel.CENTER);
JLabel minDateLbl = new JLabel("min. Date", JLabel.CENTER);
JLabel maxDateLbl = new JLabel("max Date", JLabel.CENTER);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
// addComponent(c, gbl, );
// x y w h wx wy
addComponent(c, gbl, rootLbl, 0, 0, 2, 1, 1.0, 0.0);
addComponent(c, gbl, rootTF, 0, 1, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsLbl, 0, 2, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsTF, 0, 3, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsNotLbl, 0, 4, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsNotTF, 0, 5, 2, 1, 1.0, 0.0);
addComponent(c, gbl, minSizeLbl, 0, 6, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxSizeLbl, 1, 6, 1, 1, 1.0, 0.0);
addComponent(c, gbl, minSizeTF, 0, 7, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxSizeTF, 1, 7, 1, 1, 1.0, 0.0);
addComponent(c, gbl, suffixLbl, 0, 8, 2, 1, 1.0, 0.0);
addComponent(c, gbl, suffixTF, 0, 9, 2, 1, 1.0, 0.0);
addComponent(c, gbl, minDateLbl, 0, 10, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxSizeLbl, 1, 10, 1, 1, 1.0, 0.0);
addComponent(c, gbl, minDateTF, 0, 11, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxDateTF, 1, 11, 1, 1, 1.0, 0.0);
addComponent(c, gbl, searchBtn, 0, 12, 2, 1, 1.0, 0.0);
frame.setSize(gbl.preferredLayoutSize(c));
frame.setVisible(true);
}
static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbl.setConstraints( c, gbc );
cont.add( c );
}
}
提前致谢。
I have a GridBagLayout, but one label (maxSizeLbl) goes crazy and an other one (maxDateLbl) isnt visible, anyone knows what I've done wrong?
Here is the Picture:
And here is the Code:
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchGUI {
public static void main(String args[]) {
JFrame frame = new JFrame("Search Files");
JPanel panel = new JPanel();
JTextField rootTF = new JTextField(20);
JTextField containsTF = new JTextField(20);
JTextField containsNotTF = new JTextField(20);
JTextField minSizeTF = new JTextField(10);
JTextField maxSizeTF = new JTextField(10);
JTextField suffixTF = new JTextField(10);
JTextField prefixTF= new JTextField(10);
JTextField minDateTF = new JTextField(10);
JTextField maxDateTF = new JTextField(10);
JCheckBox hiddenCB = new JCheckBox("search hidden");
JCheckBox recursiveCB = new JCheckBox("search subfolders");
JButton searchBtn = new JButton("search");
JLabel rootLbl = new JLabel("search in: ", JLabel.CENTER);
JLabel containsLbl = new JLabel("Filename contains: ", JLabel.CENTER);
JLabel containsNotLbl = new JLabel(" Filename contains not", JLabel.CENTER);
JLabel minSizeLbl = new JLabel("min. Size", JLabel.CENTER);
JLabel maxSizeLbl = new JLabel("max. Size", JLabel.CENTER);
JLabel suffixLbl = new JLabel("Filetypes", JLabel.CENTER);
JLabel prefixLbl = new JLabel("begins with", JLabel.CENTER);
JLabel minDateLbl = new JLabel("min. Date", JLabel.CENTER);
JLabel maxDateLbl = new JLabel("max Date", JLabel.CENTER);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
// addComponent(c, gbl, );
// x y w h wx wy
addComponent(c, gbl, rootLbl, 0, 0, 2, 1, 1.0, 0.0);
addComponent(c, gbl, rootTF, 0, 1, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsLbl, 0, 2, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsTF, 0, 3, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsNotLbl, 0, 4, 2, 1, 1.0, 0.0);
addComponent(c, gbl, containsNotTF, 0, 5, 2, 1, 1.0, 0.0);
addComponent(c, gbl, minSizeLbl, 0, 6, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxSizeLbl, 1, 6, 1, 1, 1.0, 0.0);
addComponent(c, gbl, minSizeTF, 0, 7, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxSizeTF, 1, 7, 1, 1, 1.0, 0.0);
addComponent(c, gbl, suffixLbl, 0, 8, 2, 1, 1.0, 0.0);
addComponent(c, gbl, suffixTF, 0, 9, 2, 1, 1.0, 0.0);
addComponent(c, gbl, minDateLbl, 0, 10, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxSizeLbl, 1, 10, 1, 1, 1.0, 0.0);
addComponent(c, gbl, minDateTF, 0, 11, 1, 1, 1.0, 0.0);
addComponent(c, gbl, maxDateTF, 1, 11, 1, 1, 1.0, 0.0);
addComponent(c, gbl, searchBtn, 0, 12, 2, 1, 1.0, 0.0);
frame.setSize(gbl.preferredLayoutSize(c));
frame.setVisible(true);
}
static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbl.setConstraints( c, gbc );
cont.add( c );
}
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您添加了两次 maxSizeLbl(而不是 maxDateLbl)
You added the maxSizeLbl twice (instead of maxDateLbl)
确保您彻底学习了 http://madbean.com/anim 中不可或缺的
GridBagLayout
教程/totallygridbag/ 首先。看来你没有使用任何IDE。 Eclipse 在
maxDateLbl
下面显示了一条奇怪的黄色波浪线——它没有被使用。如果您点击maxSizeLbl
,您会看到它被使用了两次。将第二个
maxSizeLbl
替换为maxDateLbl
。并再次观看教程。
Make sure you thoroughly study the indispensable
GridBagLayout
tutorial at http://madbean.com/anim/totallygridbag/ first.It seems like you don't use any IDE. Eclipse shows a curious yellow squiggly line below
maxDateLbl
--it's not being used. And if you click onmaxSizeLbl
, you'll see it's being used twice.Replace the second
maxSizeLbl
withmaxDateLbl
.And watch the tutorial again.
包括杰弗里关于 maxSizeLbl/maxDateLbl 的发现,这对我有用,并假设字段标签占据文本字段的左侧而不是顶部。 (仅显示相关代码部分。)
Including Geoffrey's discovery about maxSizeLbl/maxDateLbl, this is what worked for me and assumes the field labels occupy the left side of the text field and not the top. (Only the relevant section of code shown.)