在我的框架中使用GroupLayout

时间:2014-01-10 09:18:34

标签: java swing

public static class MyDictionaryFrame extends JFrame {
    public MyDictionaryFrame() {
        JLabel label = new JLabel("Enter a word");
        JTextField searchTF = new JTextField(10);


        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setVerticalGroup(layout.createSequentialGroup().addGroup(
                layout.createParallelGroup(BASELINE).addComponent(label)
                        .addComponent(searchTF)));
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setTitle("fdf");

    }
}

public static void main(String args[]) {
    new MyDictionaryFrame().setVisible(true);
}
}

我找不到我的代码崩溃的地方。现在我正在尝试学习GroupLayout,但现在还没有成功。

Exception in thread "main" java.lang.IllegalStateException:
javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7cf02bee,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=1,columnWidth=0,command=,horizontalAlignment=LEADING] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1086)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:878)
at java.awt.Container.preferredSize(Container.java:1788)
at java.awt.Container.getPreferredSize(Container.java:1773)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:917)
at java.awt.Container.preferredSize(Container.java:1788)
at java.awt.Container.getPreferredSize(Container.java:1773)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
at java.awt.Container.preferredSize(Container.java:1788)
at java.awt.Container.getPreferredSize(Container.java:1773)
at java.awt.Window.pack(Window.java:809)
at blb.hygh$MyDictionaryFrame.<init>(hygh.java:30)
at blb.hygh.main(hygh.java:39)

1 个答案:

答案 0 :(得分:1)

当我测试它时,它说

  

IllegalStateException .. JLabel .. is not attached to a horizontal group

看起来你也需要添加一个水平组,而不仅仅是一个垂直组

运行这个独立程序,添加水平组,它可以正常工作

import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyDictionaryFrame extends JFrame {

    public MyDictionaryFrame() {
        JLabel label = new JLabel("Enter a word");
        JTextField searchTF = new JTextField(10);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);


        layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(label)
                .addComponent(searchTF)));

        layout.setVerticalGroup(layout.createSequentialGroup().addGroup(
                layout.createParallelGroup(BASELINE).addComponent(label)
                .addComponent(searchTF)));
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setTitle("fdf");

    }

    public static void main(String args[]) {
        new MyDictionaryFrame().setVisible(true);
    }
}

更新 - 作品

import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyDictionaryFrame extends JFrame {

    public MyDictionaryFrame() {
        JLabel label = new JLabel("Enter a word");
        JTextField searchTF = new JTextField(10);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        //layout.setAutoCreateGaps(true);
        //layout.setAutoCreateContainerGaps(true);


        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(searchTF, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(246, Short.MAX_VALUE))
        );
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setTitle("fdf");

    }

    public static void main(String args[]) {
        new MyDictionaryFrame().setVisible(true);
    }
}