Java 网格布局和 JPanel

发布于 2024-12-19 00:34:10 字数 1357 浏览 3 评论 0 原文

我有一个问题。

我有以下程序,但我在做一件事时遇到了麻烦。我创建了一个网格布局,并使用 JPanel 在网格中输入按钮和文本,但如果我的文本很长,所有内容似乎都会改变大小。有人可以帮助我如何防止这种情况发生吗?

这是我的代码:

package JFrameTester;

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



public class JFrameTester {


public JPanel createContentPane (){

        JPanel panel = new JPanel();
        JButton button1,button2,button3,button4;

        JPanel mainPanel = new JPanel(new GridLayout(2, 0, 40, 10));

        //JPanel red = createSquareJPanel(Color.red, 50);
        button1 =  new JButton ("button1");
        button2 =  new JButton ("button2");
        button3 =  new JButton ("button3");
        button4 =  new JButton ("button4");
        Label one   = new Label("Rohihtjthjhtjghjghmgfjgjghjghj");
        //add(button1);


        mainPanel.add(button1);
        mainPanel.add(one);
        mainPanel.add(button2);
        mainPanel.add(button3);
        mainPanel.add(button4);


        panel.add(mainPanel);
        panel.setOpaque(true);
        return panel;
    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("GridLayout");
        JFrameTester Display = new JFrameTester();
        frame.setContentPane(Display.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);


 }
}

I have a question.

I have the following program and I'm having trouble with one thing. I have created a grid layout, and I'm using JPanel to enter buttons and text with in the grid, but everything seems to change size if my text is very long. Can someone help me with how I can prevent this from happening?

This is my code:

package JFrameTester;

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



public class JFrameTester {


public JPanel createContentPane (){

        JPanel panel = new JPanel();
        JButton button1,button2,button3,button4;

        JPanel mainPanel = new JPanel(new GridLayout(2, 0, 40, 10));

        //JPanel red = createSquareJPanel(Color.red, 50);
        button1 =  new JButton ("button1");
        button2 =  new JButton ("button2");
        button3 =  new JButton ("button3");
        button4 =  new JButton ("button4");
        Label one   = new Label("Rohihtjthjhtjghjghmgfjgjghjghj");
        //add(button1);


        mainPanel.add(button1);
        mainPanel.add(one);
        mainPanel.add(button2);
        mainPanel.add(button3);
        mainPanel.add(button4);


        panel.add(mainPanel);
        panel.setOpaque(true);
        return panel;
    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("GridLayout");
        JFrameTester Display = new JFrameTester();
        frame.setContentPane(Display.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);


 }
}

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

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

发布评论

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

评论(3

年少掌心 2024-12-26 00:34:10

这就是布局管理器所做的事情;他们为每个组件分配足够的空间。您可以尝试将 HTML 嵌入到 JLabel 实例中以使其换行。否则,你就只能忍受它。

还需要注意的是,JPanel 实例默认情况下是不透明的。

That's what layout managers do; they allocate enough space for every component. You could try embedding HTML into the JLabel instance in order for it to wrap. Otherwise, you're just going to have to live with it.

It's also important to note that a JPanel instance is opaque by default.

灯角 2024-12-26 00:34:10

嗯……首先要做的事情。您正在声明一个 2 行、0 列的 GridLayout。这正是您想要的吗?我想不是。

下一部分是添加内容的地方。网格通常保持动态大小。它将把单元格缩小到尽可能小的值。但是,当某些内容变大(大的jPanels文本)时,它会扩展单元格。

根据单元格的内容,它们将保持静态或尝试填充所有可用空间。

如果我正确阅读您的问题,您需要做的就是使用 JComponents 请参阅此处的 JavaDoc 链接。您可能需要对每个按钮使用 setMaximumSize(new Dimension(width,height));

有道理吗?

附带说明一下。如果您想更好地控制布局,请考虑使用 GrabBagLayout。更多的工作,但更多的控制。

Well... first things first. You're declaring a GridLayout of 2 rows, 0 columns. Is this exactly what you want? I am thinking not.

The next part is where you're adding content. The grid is typically held at a dynamic size. It will shrink the cells to the smallest possible value they can. But when some contents are getting bigger (large jPanels of text) it will expand the cell.

Depending on the contents of the cell they'll either remain static or attempt to fill in all available space.

If I'm reading your question properly, what you'll want to do is set a static size for your JButtons by using the functions for JComponents See JavaDoc link here. You'll probably want to use setMaximumSize(new Dimension(width,height)); for each button.

Makes sense?

On a side note. If you want more control over your layout look towards using a GrabBagLayout. More work, but more control.

何必那么矫情 2024-12-26 00:34:10

这是一篇较旧的帖子,但我在搜索同一问题时偶然发现了这一点。使用frame.pack() 可移动所有内容以适应给定的面板尺寸。因此,如果您增加一件物品的尺寸,其他物品就会缩小并移动以适应面板。

来源:http://www.macs.hw.ac。 uk/guidebook/?name=JFrame&page=2

This is an older post but i just stumbled across this, while searching the same issue. The use of frame.pack() is used to move everything around to fit in to the given panel size. So if you increase the size of one thing, other things shrink and move around to fit in the panel.

source: http://www.macs.hw.ac.uk/guidebook/?name=JFrame&page=2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文