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

时间:2018-03-01 08:52:48

标签: java eclipse swing

我试图在更大的"更大的"上使用bordercolored JPanels对网格进行编码。 JPanel的。 我不知道如何将这些面板放在正确的尺寸和顺序看起来像一个网格。在此之后,我想为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);
}
}

2 个答案:

答案 0 :(得分:2)

尝试使用GridLayout并将面板添加到其中。 类似的东西:

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

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

答案 1 :(得分:1)

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);
}

}