如何设置此GUI的大小?

Mik*_*oud 2 java swing jframe

我有一个applet(这是一个SSCCE):

package tutoringcalculator;

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

public class TutoringCalculator extends JApplet {
    private JPanel _root;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Tutoring Calculator");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JApplet applet = new TutoringCalculator();
                applet.init();

                frame.setContentPane(applet.getContentPane());

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                applet.start();
            }
        });
    }

    private JPanel swingContainer;

    @Override
    public void init() {
        swingContainer = new JPanel(new BorderLayout());
        add(swingContainer, BorderLayout.CENTER);
        createScene();
        setSize(600, 400);
    }

    private void createScene() {
        JLabel lbl;
        JTextField txt;

        _root = new JPanel(new FlowLayout());

        // build the session minutes
        lbl = new JLabel();
        lbl.setText("Session Minutes:");
        _root.add(lbl);

        txt = new JTextField();
        _root.add(txt);

        swingContainer.add(_root);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想大小设置为600x400 -但我发出setSize(600, 400)applet,frame,swingContainer,和_root并没有什么变化.我得到一个看起来像这样的屏幕:

辅导计算器窗口

And*_*son 5

如上所述,applet通常会获得HTML的大小(强制它).自由浮动JFrame是不同的.相反,它的组件内部建议一个大小,然后在时间计算pack().EG在构造函数中设置大小后JTextField,这就是我们所看到的:

小辅导计算器

现在,我希望你会在完成之前添加更多组件,并且它们也会使大小更大,但是还有一些其他提示可以使内容更大.边框和布局填充.这是它的一些看起来的样子.

有白色空间的辅导计算器

因此,它归结为:a)添加一大堆组件,使GUI更大,和/或b)添加布局构造器和边框中定义的空白区域.

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

public class TutoringCalculator {

    private JPanel _root;
    private JPanel swingContainer;
    private JLabel lbl;
    private JTextField txt;

    TutoringCalculator() {
        swingContainer = new JPanel(new BorderLayout());
        // set an empty border to it
        swingContainer.setBorder(new EmptyBorder(20,20,20,20));

        // suggest a spacing of 20px between components
        _root = new JPanel(new FlowLayout( FlowLayout.CENTER, 20, 20));
        swingContainer.add(_root);

        // add a line border so we can see the bounds of this container
        _root.setBorder(new LineBorder(Color.BLACK, 1));

        // this has a size as needed by the content
        lbl = new JLabel("Session Minutes:");
        _root.add(lbl);

        // Suggest a size (in characters - 10) for the text field
        txt = new JTextField(10);
        _root.add(txt);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Tutoring Calculator");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                TutoringCalculator calc = new TutoringCalculator();

                frame.setContentPane(calc.swingContainer);

                frame.pack();
                // much better!
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)