JFrame 中的组件大小不可调整

Components size not adjustable in JFrame

我正在尝试制作一个内部带有 JProgressbar 和 JButton 的 JFrame,以便用户查看进程的进度并能够中止进程。

我似乎遇到的唯一问题是进度条和按钮组件始终调整为 JFrame 大小,而不是我设置的大小。见图一; Picture 1

目标是让它看起来像这个例子; Picture 2

有人有什么建议吗? 请参阅下面的代码;

JFrame f = new JFrame("Retrieve Datalog");
JButton b = new JButton("Abort");

JProgressBar progressBar = new JProgressBar();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setIconImage(ICONBAR.getImage());
f.setResizable(false);
f.setSize(300, 100);    
f.setLocationRelativeTo(getFrame());      
b.setSize(50, 10);            

progressBar.setSize(f.getWidth() - 100, f.getHeight() - 50);    
progressBar.setValue(50);
progressBar.setStringPainted(true);

f.add(progressBar, BorderLayout.NORTH);
f.add(b, BorderLayout.CENTER);    
f.setVisible(true);

PS: 我正在使用 NetBeans 8.1 IDE、JDK v8u91

我尝试了一些代码。您可以使用网格包布局。有代码和快照 Snapshot

import java.awt.*;
import javax.swing.*;

/**
 * JFrame with a progress bar and button. With the size of their own.
 *
 * @author Tadesse
 */
public class Test extends JFrame {

    JButton button = new JButton("Cancel");
    JProgressBar progressBar = new JProgressBar();

    public Test() {
        GridBagConstraints g = new GridBagConstraints();
        setLayout(new GridBagLayout());
        set(g, 0, 0, GridBagConstraints.CENTER);
        add(progressBar, g);
        set(g, 0, 1, GridBagConstraints.CENTER);
        add(button, g);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 100);
        setVisible(true);
    }

    public void set(GridBagConstraints c, int x, int y, int anchor) {
        c.gridx = x;
        c.gridy = y;
        c.anchor = anchor;
    }

    public static void main(String[] args) {
        new Test();
    }
}