将具有彩色边框的JPanel按特定顺序放置,使它们看起来像网格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将具有彩色边框的JPanel按特定顺序放置,使它们看起来像网格相关的知识,希望对你有一定的参考价值。

我正在尝试在“更大”的JPanel上使用bordercolored JPanels编写网格。我不知道如何将这些面板放在正确的尺寸和顺序看起来像一个网格。在此之后,我想为Button添加一个Action Listener,以便在网格上的特定Panel上放置一个obeject,但是现在这不是我的问题。我希望你明白我的意思,可以帮助我。谢谢

package feld;

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;


public class Spielplan {
public static void main(String[] args) {

JFrame f1 = new JFrame();
f1.setSize(600,600);
    JPanel p1 = new JPanel();
JButton tokens = new JButton("Spielsteine setzen");
p1.setLayout(new BorderLayout());
p1.add(tokens, BorderLayout.NORTH);
f1.setVisible(true);
f1.add(p1);

JPanel g1 = new JPanel();
g1.setBorder(BorderFactory.createLineBorder(Color.RED));
g1.setPreferredSize(new Dimension(100, 100));
g1.setVisible(true);
p1.add(g1);

JPanel g2 = new JPanel();
g2.setBorder(BorderFactory.createLineBorder(Color.RED));
g2.setPreferredSize(new Dimension(100, 100));
g2.setVisible(true);
p1.add(g2);

JPanel g3 = new JPanel();
g3.setBorder(BorderFactory.createLineBorder(Color.RED));
g3.setPreferredSize(new Dimension(100, 100));
g3.setVisible(true);
p1.add(g3);

JPanel g4 = new JPanel();
g4.setBorder(BorderFactory.createLineBorder(Color.RED));
g4.setPreferredSize(new Dimension(100, 100));
g4.setVisible(true);
p1.add(g4);

JPanel g5 = new JPanel();
g5.setBorder(BorderFactory.createLineBorder(Color.RED));
g5.setPreferredSize(new Dimension(100, 100));
g5.setVisible(true);
p1.add(g5);

JPanel g6 = new JPanel();
g6.setBorder(BorderFactory.createLineBorder(Color.RED));
g6.setPreferredSize(new Dimension(100, 100));
g6.setVisible(true);
p1.add(g6);

JPanel g7 = new JPanel();
g7.setBorder(BorderFactory.createLineBorder(Color.RED));
g7.setPreferredSize(new Dimension(100, 100));
g7.setVisible(true);
p1.add(g7);

JPanel g8 = new JPanel();
g8.setBorder(BorderFactory.createLineBorder(Color.RED));
g8.setPreferredSize(new Dimension(100, 100));
g8.setVisible(true);
p1.add(g8);

JPanel g9 = new JPanel();
g9.setBorder(BorderFactory.createLineBorder(Color.RED));
g9.setPreferredSize(new Dimension(100, 100));
g9.setVisible(true);
p1.add(g9);
}
}
答案

enter image description here

您应该将BorderLayout分配给JFrame。然后将JButton分配到JFrame的NORTH位置,将JPanel分配到CENTER位置。请记住在JFrame上使用.pack()以确保元素正确匹配。最后,记住一个默认的关闭方法,我在这里提供了一个.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

通过将面板布局设置为GridLayout来提供实际网格。希望这对你有所帮助。

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

public class Spielplan {

public static void main(String[] args) {

    JFrame f1 = new JFrame();
    f1.setLayout(new BorderLayout());
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setPreferredSize(new Dimension(960, 800));

    JButton tokens = new JButton("Spielsteine setzen");
    f1.add(tokens, BorderLayout.NORTH);

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3,3));
    f1.add(p1, BorderLayout.CENTER);

    JPanel g1 = new JPanel();
    g1.setBorder(BorderFactory.createLineBorder(Color.RED));
    g1.setPreferredSize(new Dimension(100, 100));
    g1.setVisible(true);
    p1.add(g1);

    JPanel g2 = new JPanel();
    g2.setBorder(BorderFactory.createLineBorder(Color.RED));
    g2.setPreferredSize(new Dimension(100, 100));
    g2.setVisible(true);
    p1.add(g2);

    JPanel g3 = new JPanel();
    g3.setBorder(BorderFactory.createLineBorder(Color.RED));
    g3.setPreferredSize(new Dimension(100, 100));
    g3.setVisible(true);
    p1.add(g3);

    JPanel g4 = new JPanel();
    g4.setBorder(BorderFactory.createLineBorder(Color.RED));
    g4.setPreferredSize(new Dimension(100, 100));
    g4.setVisible(true);
    p1.add(g4);

    JPanel g5 = new JPanel();
    g5.setBorder(BorderFactory.createLineBorder(Color.RED));
    g5.setPreferredSize(new Dimension(100, 100));
    g5.setVisible(true);
    p1.add(g5);

    JPanel g6 = new JPanel();
    g6.setBorder(BorderFactory.createLineBorder(Color.RED));
    g6.setPreferredSize(new Dimension(100, 100));
    g6.setVisible(true);
    p1.add(g6);

    JPanel g7 = new JPanel();
    g7.setBorder(BorderFactory.createLineBorder(Color.RED));
    g7.setPreferredSize(new Dimension(100, 100));
    g7.setVisible(true);
    p1.add(g7);

    JPanel g8 = new JPanel();
    g8.setBorder(BorderFactory.createLineBorder(Color.RED));
    g8.setPreferredSize(new Dimension(100, 100));
    g8.setVisible(true);
    p1.add(g8);

    JPanel g9 = new JPanel();
    g9.setBorder(BorderFactory.createLineBorder(Color.RED));
    g9.setPreferredSize(new Dimension(100, 100));
    g9.setVisible(true);
    p1.add(g9);

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

}

另一答案

尝试使用GridLayout并将面板添加到其中。就像是:

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

如果你想仍然使用顶部的按钮,那么就像使用BorderLayout一样为整个屏幕创建一个面板。然后使用GridLayout将Button添加到NORTH和另一个JPanel到CENTER。使用GridLayout将带有彩色边框的JPanel放在JPanel中。

以上是关于将具有彩色边框的JPanel按特定顺序放置,使它们看起来像网格的主要内容,如果未能解决你的问题,请参考以下文章

如何将两个 JPanel 添加到中心的 JFrame?

如何使用标志在 Drupal 视图中为节点提供特定的放置顺序

如何在 IAR 编译器中按特定顺序放置变量?

如何对大量 csv 文件进行排序以按特定顺序读取它们?

WPF 网格 — 按特定顺序调整列的大小

Java OverlayLayout 项目排列问题