1

所以,我有 GroupLayout 和 3 个组件的基本框架。理论上应该是这样的

[--------标签------]
[按钮] [按钮]

但它只显示了我最后使用的按钮组件(按钮“bJeden”被拉伸以适合整个窗口)。这是我的代码:
框架文件:

public class MainFrame extends JFrame{
    GroupLayout layout = new GroupLayout(getContentPane());
    JButton bZero = new JButton("0");
    JButton bJeden = new JButton("1");
    JLabel label = new JLabel("LABEL");
    MainFrame(){
        this.setBounds(200, 200, 640, 480);
        layout.setHorizontalGroup(layout.createSequentialGroup()
              .addComponent(label)
              .addGroup(layout.createSequentialGroup()
                        .addComponent(bZero)
                        .addComponent(bJeden)
                        )
              );
        this.setVisible(true);
    }
}

主文件:

public class Main {
    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
    }
}

如何解决?

4

1 回答 1

2

您没有为内容窗格设置布局

    MainFrame(){
       getContentPane().setLayout(layout);
       this.setBounds(200, 200, 640, 480);
       ...
于 2011-03-17T06:21:40.120 回答