【问题标题】:Adding elements (i.e. JLabels) outside of the set layout在集合布局之外添加元素(即 JLabels)
【发布时间】:2013-04-04 15:42:44
【问题描述】:

作为项目的一部分,我们必须有 9 个盒子,在这里我刚刚实现了交替颜色作为示例来代替我们应该使用的图像。但是,虽然我希望这 9 个 JLabels 在此网格 layout (3,3) 中,但我还希望在顶部有一条消息(JLabel),我可以集中起来,比如欢迎消息以及大约四个 @987654324 @下面?有人可以为我指出如何实现这一目标的正确方向吗?

谢谢!

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

class HomeController extends JPanel implements MouseListener
{
    HomeController()
    {
        setLayout(new GridLayout(3,3));

        JLabel apl1 = new JLabel("");
        apl1.setBackground(Color.WHITE);
        apl1.setOpaque(true);
        this.add(apl1);

        JLabel apl2 = new JLabel("");
        apl2.setBackground(Color.BLACK);
        apl2.setOpaque(true);
        this.add(apl2);

        JLabel apl3 = new JLabel("");
        apl3.setBackground(Color.WHITE);
        apl3.setOpaque(true);
        this.add(apl3);

        JLabel apl4 = new JLabel("");
        apl4.setBackground(Color.BLACK);
        apl4.setOpaque(true);
        this.add(apl4);

        JLabel apl5 = new JLabel("");
        apl5.setBackground(Color.WHITE);
        apl5.setOpaque(true);
        this.add(apl5);

        JLabel apl6 = new JLabel("");
        apl6.setBackground(Color.BLACK);
        apl6.setOpaque(true);
        this.add(apl6);

        JLabel apl7 = new JLabel("");
        apl7.setBackground(Color.WHITE);
        apl7.setOpaque(true);
        this.add(apl7);

        JLabel apl8 = new JLabel("");
        apl8.setBackground(Color.BLACK);
        apl8.setOpaque(true);
        this.add(apl8);

        JLabel apl9 = new JLabel("");
        apl9.setBackground(Color.WHITE);
        apl9.setOpaque(true);
        this.add(apl9);

        JLabel message = new JLabel("hello world");
        this.add(message);
    }
}

【问题讨论】:

  • 你能用任何图片向我们展示它目前的样子以及你想要它吗?
  • 呃,目前它有一个 3 x 3 的网格,每个正方形在黑色和白色之间交替。我希望它有那个,但上面有一个标签,下面有一个按钮
  • 我的建议是创建 9 个 Jpanel 并使用 Boxlayout 或 cardlayout 添加您的组件(jlable 和 jbutton)。完成后,使用 gridlayout 将这 9 个 jpanel 添加到 3x3 网格中。
  • 要扩展 Smit 的评论,您可以在面板中嵌入面板,在布局中嵌入布局。您可以混合搭配以获得您想要的结果。您可能有一个 2x1 Gridlayout,第 1 行带有顶部标签,第 2 行有一个单独的面板,其中包含 3x3 gridlayout。

标签: java swing layout jpanel layout-manager


【解决方案1】:

您可以组合具有不同布局的多个面板。详情请查看A Visual Guide to Layout Managers。

例如,JFrame 的默认布局是BorderLayout。使用BorderLayout,您可以将标题放在BorderLayout.NORTH,将带有按钮的面板放在BorderLayout.SOUTH,将带有标签网格的面板放在BorderLayout.CENTER。每个面板可能有自己更复杂的布局。例如,标签网格使用GridLayout,按钮面板使用FlowLayout。

下面是一个基于已发布代码的非常简单的示例,演示了这种方法:

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

public class TestGrid {
    public TestGrid() {
        final JFrame frame = new JFrame("Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel(new GridLayout(3, 3));

        for (int idx = 0; idx < 9; idx++) {
            JLabel label = new JLabel();
            label.setBackground(idx % 2 == 0 ? Color.WHITE : Color.BLACK);
            label.setOpaque(true);
            mainPanel.add(label);
        }
        mainPanel.setPreferredSize(new Dimension(200, 200));
        frame.add(mainPanel, BorderLayout.CENTER);

        frame.add(new JLabel("Title", JLabel.CENTER), BorderLayout.NORTH);

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(new JButton("Start"));
        buttonsPanel.add(new JButton("Stop"));
        frame.add(buttonsPanel, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestGrid();
            }
        });
    }
}   

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 1970-01-01
    • 2022-12-18
    • 2017-12-21
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多