简体   繁体   English

在Java Swing中排列非矩形JPanels

[英]arrange non rectangular JPanels in Java Swing

I've defined a new class LShapePanel which extends JPanel and which looks like an L. 我定义了一个新类LShapePanel,它扩展了JPanel并看起来像L。

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class LShapePanel extends JPanel{

    public Color color;

    public LShapePanel(Color color) {
        this.color = color;
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(color);

        /* coordinates for polygon  */
        int[] xPoints = {0,100,100,20,20,0};
        int[] yPoints = {0,0,20,20,100,100};

        /* draw polygon */
        g2d.fillPolygon(xPoints, yPoints, 6);
    }
}

I'd like to arrange two of these LShapePanels like this: 我想像这样安排两个LShapePanel:

在此处输入图片说明

But I don't know how? 但是我不知道怎么办? Here is my code for arranging two LShapePanels in a row. 这是我的代码,用于连续排列两个LShapePanels。

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;

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

public class DifferentShapes extends JFrame {

    public DifferentShapes() {

        setTitle("different shapes");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(500, 300);

        JPanel panel = new JPanel();

        /* create and add first L in red */
        LShapePanel lsp1 = new LShapePanel(new Color(255,0,0));
        lsp1.setPreferredSize(new Dimension(100,100));
        panel.add(lsp1);

        /* create and add second L in green*/
        LShapePanel lsp2 = new LShapePanel(new Color(0,255,0));
        lsp2.setPreferredSize(new Dimension(100,100));
        panel.add(lsp2);

        add(panel);

        pack();

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DifferentShapes df = new DifferentShapes();
                df.setVisible(true);
            }
        });
    }
}

And the result: 结果:

在此处输入图片说明

You need to use layout manager to arrange the components in the JFrame. 您需要使用layout manager在JFrame中安排组件。 According to this turorial , the content pane, which actually contains the components you put into JFrame , uses Borderlayout by default. 根据这种说法 ,内容窗格实际上包含您放入JFrame中的组件,默认情况下使用Borderlayout In an "L" shape as the LShapePanel looks, it's actually a rectangle(every component in swing is a rectangle, as a matter of fact) with part of it transplant. LShapePanel看起来像“ L”形,它实际上是一个矩形(实际上,挥杆中的每个分量都是一个矩形),并且移植了一部分。 So if you want to arrange the panels in the way you want, they will have to overlap with each other. 因此,如果您要按自己的方式排列面板,则它们必须彼此重叠。 Different kinds of layout managers use different layout strategies, and Borderlayout won't allow components to overlap, so you have to change to another layout manager. 不同种类的布局管理器使用不同的布局策略,并且Borderlayout不允许组件重叠,因此您必须更改为其他布局管理器。 Sorry that I don't know any layout manager that allows components to overlap, but you can use JLayeredPane to achieve you goal. 抱歉,我不知道任何允许组件重叠的布局管理器,但是您可以使用JLayeredPane实现目标。 Add a JLayeredPane to the JFrame and then add the LShapePanels to the JLayeredPane . JLayeredPane添加到JFrame ,然后将LShapePanels添加到JLayeredPane

Sorry, layout manager enthusiasts, but I can't think of any way other than using setLocation and a null layout manager. 对不起,布局管理器爱好者,但是除了使用setLocationnull布局管理器外,我setLocation Here's a demonstration: 这是一个示范:

setLayout(null);
LShapePanel lsp1 = new LShapePanel(new Color(255,0,0));
lsp1.setPreferredSize(new Dimension(100,100));
lsp1.setLocation(0,0);
add(lsp1);

LShapePanel lsp2 = new LShapePanel(new Color(0,255,0));
lsp2.setPreferredSize(new Dimension(100,100));
lsp2.setLocation(30,30);
add(lsp2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM