2

我有一个小程序(这是一个 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);
    }
}

我想将大小设置为 600x400 - 但我已经发布setSize(600, 400)applet, frame, swingContainer,_root并且没有任何变化。我得到一个看起来像这样的屏幕:

辅导计算器窗口

4

1 回答 1

5

如前所述,小程序通常由 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);
            }
        });
    }
}
于 2013-05-16T08:46:31.773 回答