GridBagLayout 变得疯狂

发布于 2024-09-26 07:18:52 字数 4234 浏览 3 评论 0原文

我有一个 GridBagLayout,但一个标签 (maxSizeLbl) 变得疯狂,而另一个标签 (maxDateLbl) 不可见,有人知道我做错了什么吗?

这是图片:

alt text

这是代码:

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:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风情万种。 2024-10-03 07:18:52

您添加了两次 ma​​xSizeLbl(而不是 ma​​xDateLbl

You added the maxSizeLbl twice (instead of maxDateLbl)

生死何惧 2024-10-03 07:18:52

确保您彻底学习了 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 on maxSizeLbl, you'll see it's being used twice.

Replace the second maxSizeLbl with maxDateLbl.

And watch the tutorial again.

痴情换悲伤 2024-10-03 07:18:52

包括杰弗里关于 maxSizeLbl/maxDateLbl 的发现,这对我有用,并假设字段标签占据文本字段的左侧而不是顶部。 (仅显示相关代码部分。)


addComponent(c, gbl,        rootLbl, 0, 0, 1, 1, 1.0, 0.0);
addComponent(c, gbl,         rootTF, 1, 0, 3, 1, 1.0, 0.0);

addComponent(c, gbl,    containsLbl, 0, 1, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     containsTF, 1, 1, 3, 1, 1.0, 0.0);

addComponent(c, gbl, containsNotLbl, 0, 2, 1, 1, 1.0, 0.0);
addComponent(c, gbl,  containsNotTF, 1, 2, 3, 1, 1.0, 0.0);

addComponent(c, gbl,     minSizeLbl, 0, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      minSizeTF, 1, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     maxSizeLbl, 2, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      maxSizeTF, 3, 3, 1, 1, 1.0, 0.0);

addComponent(c, gbl,      suffixLbl, 0, 4, 1, 1, 1.0, 0.0);
addComponent(c, gbl,       suffixTF, 1, 4, 3, 1, 1.0, 0.0);

addComponent(c, gbl,     minDateLbl, 0, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      minDateTF, 1, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     maxDateLbl, 2, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      maxDateTF, 3, 5, 1, 1, 1.0, 0.0);

addComponent(c, gbl,      searchBtn, 0, 6, 4, 1, 1.0, 0.0);

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.)


addComponent(c, gbl,        rootLbl, 0, 0, 1, 1, 1.0, 0.0);
addComponent(c, gbl,         rootTF, 1, 0, 3, 1, 1.0, 0.0);

addComponent(c, gbl,    containsLbl, 0, 1, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     containsTF, 1, 1, 3, 1, 1.0, 0.0);

addComponent(c, gbl, containsNotLbl, 0, 2, 1, 1, 1.0, 0.0);
addComponent(c, gbl,  containsNotTF, 1, 2, 3, 1, 1.0, 0.0);

addComponent(c, gbl,     minSizeLbl, 0, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      minSizeTF, 1, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     maxSizeLbl, 2, 3, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      maxSizeTF, 3, 3, 1, 1, 1.0, 0.0);

addComponent(c, gbl,      suffixLbl, 0, 4, 1, 1, 1.0, 0.0);
addComponent(c, gbl,       suffixTF, 1, 4, 3, 1, 1.0, 0.0);

addComponent(c, gbl,     minDateLbl, 0, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      minDateTF, 1, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,     maxDateLbl, 2, 5, 1, 1, 1.0, 0.0);
addComponent(c, gbl,      maxDateTF, 3, 5, 1, 1, 1.0, 0.0);

addComponent(c, gbl,      searchBtn, 0, 6, 4, 1, 1.0, 0.0);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文