Java 中的 GridLayout 帮助

发布于 2024-10-16 23:09:45 字数 398 浏览 4 评论 0原文

我有一个包含以下代码的 JPanel:

JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2, 10, 30));
final JTextField fileName = new JTextField();
pane.add(fileName);
JButton creater = new JButton("Create File");
pane.add(creater);
JButton deleter = new JButton("Delete File");
pane.add(deleter);

我想知道如何使 JTextField 占用 GridLayout 上的两个空格,同时让两个按钮通过在同一行上各占用一个空格来共享一行?

I have a JPanel with the following code:

JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2, 10, 30));
final JTextField fileName = new JTextField();
pane.add(fileName);
JButton creater = new JButton("Create File");
pane.add(creater);
JButton deleter = new JButton("Delete File");
pane.add(deleter);

I was wondering, how do I make it so that the JTextField takes up two spaces on the GridLayout, while having the two buttons share a row by taking up one space each on the same line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

り繁华旳梦境 2024-10-23 23:09:45

使用 GridLyout 很难做到这一点。您已经创建了更宽的单元格(例如 new GridLayout(2, 2, 10, 30),然后将 TextField 添加到第一个单元格。然后您必须使用 GridLayout(2, 1) 创建另一个面板,将其放入第二行的单元格中,并将按钮添加到此嵌套网格布局的第一个单元格中。

很快您就需要将 GridLayout 放入其他 GridLayout 中,

首先看看 GridBagLayout 。确保生活并不总是挑剔的:)。然后看看 MigLayout 等替代解决方案。它不是 JDK 的一部分,但它确实是强大的工具,可以让您的生活更轻松。

It is a hard to do with GridLyout. You have create wider cells (e.g. new GridLayout(2, 2, 10, 30), then add TextField to the fist cell. Then you have to create yet another panel with GridLayout(2, 1), put it into the cell in second line and add your button into 1 st cell of this nested grid layout.

Shortly you need GridLayout into other GridLayout.

There are better tools to implement this. First take a look on GridBagLayout. It is just to be sure that life is not always pick-nick :). Then take a look on alternative solutions like MigLayout. It is not a part of JDK but it really powerful tool that makes your life easier.

少女的英雄梦 2024-10-23 23:09:45

您无法使用 GridLayout 实现列跨度。我建议您尝试 GridBagLayoutGridBagConstraints

You cannot do column spans with GridLayout. I recomend you try GridBagLayout and GridBagConstraints

十年九夏 2024-10-23 23:09:45

在废弃了第 3 方布局的建议之后,并且由于我对 GBL 怀有恶意的仇恨,我认为是时候“把我的代码放在我的嘴上”以接受公众监督(和废弃)。

此 SSCCE 使用嵌套布局。

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

class SimpleLayoutTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JPanel ui = new JPanel( new BorderLayout(20,20) );
                // I would go for an EmptyBorder here, but the filled
                // border is just to demonstrate where the border starts/ends
                ui.setBorder( new LineBorder(Color.RED,15) );

                // this should be a button that pops a JFileChooser, or perhaps
                // a JTree of the existing file system structure with a JButton
                // to prompt for the name of a new File.
                final JTextField fileName = new JTextField();
                ui.add(fileName, BorderLayout.NORTH);

                JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 30));
                ui.add(buttonPanel, BorderLayout.CENTER);

                JButton creater = new JButton("Create File");
                buttonPanel.add(creater);
                JButton deleter = new JButton("Delete File");
                buttonPanel.add(deleter);

                JOptionPane.showMessageDialog(null, ui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

After trashing the suggestion of 3rd party layouts, and since I possess a malevolent hatred of GBL, I thought it was about to time to 'put my code where my mouth is' for public scrutiny (and trashing).

This SSCCE uses a nested layout.

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

class SimpleLayoutTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JPanel ui = new JPanel( new BorderLayout(20,20) );
                // I would go for an EmptyBorder here, but the filled
                // border is just to demonstrate where the border starts/ends
                ui.setBorder( new LineBorder(Color.RED,15) );

                // this should be a button that pops a JFileChooser, or perhaps
                // a JTree of the existing file system structure with a JButton
                // to prompt for the name of a new File.
                final JTextField fileName = new JTextField();
                ui.add(fileName, BorderLayout.NORTH);

                JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 30));
                ui.add(buttonPanel, BorderLayout.CENTER);

                JButton creater = new JButton("Create File");
                buttonPanel.add(creater);
                JButton deleter = new JButton("Delete File");
                buttonPanel.add(deleter);

                JOptionPane.showMessageDialog(null, ui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
拒绝两难 2024-10-23 23:09:45

查看如何使用 GridBagLayout 的教程。

示例代码:

    JPanel pane = new JPanel();
    GridBagLayout gridbag  = new GridBagLayout();
    pane.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();

    final JTextField fileName = new JTextField();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(fileName, c);

    JButton creater = new JButton("Create File");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 1;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(creater, c);

    JButton deleter = new JButton("Delete File");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    pane.add(deleter, c);

Take a look at the tutorial on How to Use GridBagLayout.

Sample code:

    JPanel pane = new JPanel();
    GridBagLayout gridbag  = new GridBagLayout();
    pane.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();

    final JTextField fileName = new JTextField();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(fileName, c);

    JButton creater = new JButton("Create File");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 1;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(creater, c);

    JButton deleter = new JButton("Delete File");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    pane.add(deleter, c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文