帮助Java的gridbaglayout?

时间:2015-01-09 18:23:50

标签: java swing awt layout-manager gridbaglayout

好吧,我正在尝试将其设置为“输入您的名字”JLabel显示在JTextField左侧的旁边,遗憾的是它显示在中心。我所说的是第一个JLabel和第一个JTextArea,请忽略其余的代码。

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

public class practice extends JFrame  {

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

    public practice(){
        JPanel jp = new JPanel();

        jp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        jp.add(new JLabel(""),gbc);

        gbc.gridx=0;
        gbc.gridy=1;
        jp.add(new JLabel("Enter your name\n"),gbc);

        gbc.gridx=0;
        gbc.gridy=0;
        jp.add(new JLabel(" "),gbc);

        gbc.gridx=0;
        gbc.gridy=0;
        jp.add(new JLabel(" "),gbc);

        gbc.insets = new Insets(30,0,0,0);
        gbc.gridx=0;
        gbc.gridy=1;
        jp.add(new JTextArea(3,30),gbc);

        gbc.insets = new Insets(10,0,0,0);
        gbc.gridx=0;
        gbc.gridy=2;
        jp.add(new JTextArea(3,10),gbc);

        gbc.insets = new Insets(10,0,0,0);
        gbc.gridx=0;
        gbc.gridy=3;
        jp.add(new JTextArea(3,10),gbc);

        gbc.gridx=0;
        gbc.gridy=0;
        jp.add(new JRadioButton(""),gbc);

        gbc.gridx=0;
        gbc.gridy=0;
        jp.add(new JRadioButton (""),gbc);

        gbc.gridx=0;
        gbc.gridy=0;
        jp.add(new JRadioButton(""),gbc);

        this.add(jp);
        setTitle("Card");
        setSize(700,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}

3 个答案:

答案 0 :(得分:1)

我建议为要放入GUI的每个Swing组件创建一个GridBagConstraints。这样,您不必记住任何默认值,并为每个组件指定每个参数。

这是我使用GridBagLayout的JDialog。

Statistics

这是JDialog的代码。

package com.ggl.sudoku.solver.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SolutionDialog {

    protected static final Insets   buttonInsets    = new Insets(10, 10, 0, 10);

    private int                     singleCount;
    private int                     guessCount;

    private long                    elapsedTime;

    private JDialog                 dialog;

    private SudokuFrame             frame;

    public SolutionDialog(SudokuFrame frame, int singleCount, int guessCount,
            long elapsedTime) {
        this.frame = frame;
        this.singleCount = singleCount;
        this.guessCount = guessCount;
        this.elapsedTime = elapsedTime;
        createPartControl();
    }

    private void createPartControl() {
        dialog = new JDialog(frame.getFrame(), "Statistics");
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel resultsPanel = new JPanel();
        resultsPanel.setLayout(new GridBagLayout());

        int gridy = 0;

        JLabel singleCountLabel = new JLabel("Cells with one possible number:");
        addComponent(resultsPanel, singleCountLabel, 0, gridy, 1, 1,
                buttonInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        String s = singleCount + " cells";
        JLabel singleCountString = new JLabel(s);
        addComponent(resultsPanel, singleCountString, 1, gridy++, 1, 1,
                buttonInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JLabel guessCountLabel = new JLabel("Cells where the solver guessed:");
        addComponent(resultsPanel, guessCountLabel, 0, gridy, 1, 1,
                buttonInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        s = guessCount + " cells";
        JLabel guessCountString = new JLabel(s);
        addComponent(resultsPanel, guessCountString, 1, gridy++, 1, 1,
                buttonInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JLabel elapsedTimeLabel = new JLabel("Elapsed Time:");
        addComponent(resultsPanel, elapsedTimeLabel, 0, gridy, 1, 1,
                buttonInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        s = elapsedTime + " milliseconds";
        JLabel elapsedTimeString = new JLabel(s);
        addComponent(resultsPanel, elapsedTimeString, 1, gridy++, 1, 1,
                buttonInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        mainPanel.add(resultsPanel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        JButton okButton = new JButton("OK");
        okButton.setAlignmentX(JButton.RIGHT_ALIGNMENT);
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });
        buttonPanel.add(okButton);

        mainPanel.add(buttonPanel);

        dialog.add(mainPanel);
        dialog.pack();
        dialog.setBounds(getBounds());
        dialog.setVisible(true);
    }

    private void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
            int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    protected Rectangle getBounds() {
        Rectangle f = frame.getBounds();
        Rectangle d = dialog.getBounds();
        d.x = f.x + (f.width - d.width) / 2;
        d.y = f.y + (f.height - d.height) / 2;
        return d;
    }

}

答案 1 :(得分:0)

gridxgridy定义对象将放置在网格中的哪个单元格。它本质上是一个坐标系......就像电子表格一样。

您在(0,0)处放置了许多组件,并且在您询问的具体实例中,您将“输入您的姓名”标签和JTextArea(与{{1}不同1}})在(0,1)的同一个单元格中。因此,它们相互叠加。

答案 2 :(得分:0)

如果你想把它放在另一个旁边,你应该尝试这样做:

if let cell = self.table.cellForRowAtIndexPath(path) as? ICComplaintCategoryCell {
    cell.reload()
}

另请阅读锚点,因为我没有看到你使用它们,你可以使用:

#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>

using namespace std;
typedef pair<int, int> pii;

int main() {
    int l, r;
    vector<pii> pairs;
    while (cin >> l >> r) {
        pairs.emplace_back(l, r);
    }
    pairs.emplace_back(INT_MAX, INT_MAX); // sentinel
    sort(pairs.begin(), pairs.end(), 
    [](const pii& x, const pii& y) { return x.first < y.first; });

    auto p_one = pairs.begin(), p_two = pairs.begin() + 1;
    int L = p_one->first;
    int R = p_one->second;
    int sum = 0;
    while (p_two != pairs.end()) {
        if (R < p_two->first) {
            sum += abs(L - R) + 1;
            L = p_two->first;
            R = INT_MIN;
        }
        p_one++; p_two++;
        if (p_one->second > R) R = p_one->second;
    }
    cout << sum;
}

用于显示自行开始以来的对象,而不是从中心开始。

最后,我建议您创建一种向窗口添加对象的方法,以避免大量代码。 这是一个非常好的DEITEL&例如我使用的:

gbc.gridx = 0;
gbc.gridy = 0; 
Container.add(//Your Label);

gbc.gridx = 1;
gbc.gridy = 0;
Container.add(//Your TextField);

所以现在你只需要这样做:

gbc.anchor = GridBagConstraints.LINE_START;

确保为widht和height赋予一个值,因为如果没有,构造函数现在不知道添加对象的位置。 并且还要确保在构造函数的同一个类中声明该方法。

如果我有一些语法错误,对不起,但我很自豪我的脑子里有一个Nopal。 JAJAJA。

我希望这个答案可以帮到你。