GridBagLayout格式不正确

时间:2016-04-18 23:51:07

标签: java swing debugging

我正在尝试让配置面板占据屏幕顶部,然后将输入和输出面板并排放置。我也试图让文本区域分别为70个字符宽,30行高。但是,现在,配置面板根本没有显示,文本区域只有35个字符宽,2行高。我已经按照我发现的所有示例和教程进行了操作。我做错了什么?

public class BorderWrapper {
    public static void main(String[] args) {
        //Create frame
        JFrame frame = new JFrame("Border Wrapper");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create main panel
        MainPanel panel = new MainPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);

        //Display frame
        Dimension minSize = new Dimension(650, 375);
        frame.setPreferredSize(minSize);
        frame.setMinimumSize(minSize);
        frame.pack();
        frame.setVisible(true);
    }
}



public class MainPanel extends JPanel {
    private static final Font INPUT_FONT = new Font("Monospaced", Font.PLAIN, 12);

    private JTextArea inputArea, outputArea;
    private JTextField titleField, topBorderField, sideBorderField;

    public MainPanel() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //Set up config panel
        JPanel configPanel = new JPanel();
        configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS));
        configPanel.setMaximumSize(new Dimension(400, 200));

        titleField      = new JTextField(25);
        titleField.setFont(INPUT_FONT);
        topBorderField  = new JTextField(1);
        topBorderField.setFont(INPUT_FONT);
        sideBorderField = new JTextField(4);
        sideBorderField.setFont(INPUT_FONT);

        configPanel.add(new JLabel("Title:"));
        configPanel.add(titleField);
        configPanel.add(new JLabel("Top border:"));
        configPanel.add(topBorderField);
        configPanel.add(new JLabel("Side border:"));
        configPanel.add(sideBorderField);

        c.gridwidth = 2;
        c.gridx     = 0;
        c.gridy     = 0;
        add(configPanel, c);

        //Set up Input panel
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
        inputArea = new JTextArea("Type or paste your stuff here . . .");
        inputArea.setFont(INPUT_FONT);
        inputArea.setLineWrap(true);
        inputArea.setWrapStyleWord(true);
        inputArea.setColumns(75);
        JScrollPane inputPane = new JScrollPane(inputArea);
        inputPane.setMinimumSize(new Dimension(250, 400));

        JLabel inputLabel = new JLabel("Text Box");
        inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        inputPanel.add(inputLabel);
        inputPanel.add(inputPane);
        inputPanel.setMinimumSize(new Dimension(250, 400));

        c.gridwidth = 1;
        c.gridx     = 0;
        c.gridy     = 1;
        add(inputPanel, c);

        //Set up Output panel
        JPanel outputPanel = new JPanel();
        outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
        outputArea = new JTextArea();
        outputArea.setFont(INPUT_FONT);
        outputArea.setLineWrap(true);
        outputArea.setWrapStyleWord(true);
        outputArea.setColumns(75);
        JScrollPane outputPane = new JScrollPane(outputArea);
        outputPane.setMinimumSize(new Dimension(250, 400));

        JLabel outputLabel = new JLabel("Wrapped Output");
        outputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        outputPanel.add(outputLabel);
        outputPanel.add(outputPane);
        outputPanel.setMinimumSize(new Dimension(250, 400));

        c.gridwidth = 1;
        c.gridx     = 1;
        c.gridy     = 1;
        add(outputPanel, c);
    }
}

最初,我打算尝试使用BorderLayout,因为它似乎对我试图制作的布局最有意义,但是当我将它们设置为BorderLayout.WEST和BorderLayout时,这工作做得更糟。 EAST。

1 个答案:

答案 0 :(得分:1)

修改程序以在MainPanel中使用BorderLayout以及其他一些小的更改以获得所需的外观。检查是否有帮助。

public class BorderWrapper {
    public static void main(String[] args) {
        // Create frame
        JFrame frame = new JFrame("Border Wrapper");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create main panel
        MainPanel panel = new MainPanel();
        frame.getContentPane().add(panel);

        // Display frame
        Dimension minSize = new Dimension(650, 375);
        frame.setPreferredSize(minSize);
        frame.setMinimumSize(minSize);
        frame.pack();
        frame.setVisible(true);
    }
}

class MainPanel extends JPanel {
    private static final Font INPUT_FONT = new Font("Monospaced", Font.PLAIN, 12);

    private JTextArea inputArea, outputArea;
    private JTextField titleField, topBorderField, sideBorderField;

    public MainPanel() {
        setLayout(new BorderLayout());

        // Set up config panel
        JPanel configPanel = new JPanel();
        configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS));
        configPanel.setMaximumSize(new Dimension(400, 200));

        titleField = new JTextField(25);
        titleField.setFont(INPUT_FONT);
        topBorderField = new JTextField(1);
        topBorderField.setFont(INPUT_FONT);
        sideBorderField = new JTextField(4);
        sideBorderField.setFont(INPUT_FONT);

        configPanel.add(new JLabel("Title:"));
        configPanel.add(titleField);
        configPanel.add(new JLabel("Top border:"));
        configPanel.add(topBorderField);
        configPanel.add(new JLabel("Side border:"));
        configPanel.add(sideBorderField);

        add(configPanel, BorderLayout.NORTH);

        // Set up Input panel
        JPanel lowerPanel = new JPanel(new GridLayout(1, 1));
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
        inputArea = new JTextArea("Type or paste your stuff here . . .");
        inputArea.setFont(INPUT_FONT);
        inputArea.setLineWrap(true);
        inputArea.setWrapStyleWord(true);
        inputArea.setColumns(75);
        JScrollPane inputPane = new JScrollPane(inputArea);

        JLabel inputLabel = new JLabel("Text Box");
        inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        inputPanel.add(inputLabel);
        inputPanel.add(inputPane);

        lowerPanel.add(inputPanel);

        // Set up Output panel
        JPanel outputPanel = new JPanel();
        outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
        outputArea = new JTextArea();
        outputArea.setFont(INPUT_FONT);
        outputArea.setLineWrap(true);
        outputArea.setWrapStyleWord(true);
        outputArea.setColumns(75);
        JScrollPane outputPane = new JScrollPane(outputArea);

        JLabel outputLabel = new JLabel("Wrapped Output");
        outputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        outputPanel.add(outputLabel);
        outputPanel.add(outputPane);

        lowerPanel.add(outputPanel);
        add(lowerPanel, BorderLayout.CENTER);
    }
}

enter image description here

我觉得将BorderLayout用于这种格式很方便。不过,你仍然可以对使用GridBagConstraints发布的代码进行一些更改,以获得所需的外观。逐一进行以下更改,您将观察到差异。< / p>

1.您通过使用BorderLayout将MainPanel与NORTH对齐。但在您的情况下,整个组件集放在MainPanel中,因此最好将它放在center.So而不是NORTH使用下面:(在此更改之后,你会看到完整的输入和输出面板)

MainPanel panel = new MainPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);

2.您已将父框架的尺寸设置为尺寸(高度= 375)

minSize = new Dimension(650, 375);

您的组件(configPanel = 200,outputPanel = 400)组合高度超过375.将父级的高度增加到大约600.

3.而不是BoxLayout尝试使用GridLayout for configPanel。

configPanel.setLayout(new GridLayout(1,6,5,0));

对现有代码进行上述3次更改将获得预期的输出。希望这可以澄清。