简体   繁体   English

组件在框架中可见但在面板中不可见

[英]Component visible in frame but not in panel

I was doing some JFrame work and I got into this problem.我正在做一些 JFrame 工作,我遇到了这个问题。 Basically I have a component, if I add it to my frame it's visible but if I add it to a panel it doesn't show up, anybody knows why?基本上我有一个组件,如果我将它添加到我的框架中它是可见的,但是如果我将它添加到面板中它不会显示,有人知道为什么吗?

This is my component (basically a geometric shape):这是我的组件(基本上是几何形状):

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


public class RosaDeiVentiComponent extends JComponent {

    private static int rdvRadius = 300;

    private static int centerX = GraphicInterfaceManager.getBorderX() + rdvRadius;
    private static int centerY = GraphicInterfaceManager.getBorderY() + rdvRadius;


    
    private int xN = centerX; private int yN = centerY - rdvRadius;
    private int xS = centerX; private int yS = centerY + rdvRadius;
    private int xE = centerX + rdvRadius; private int yE = centerY;
    private int xO = centerX - rdvRadius; private int yO = centerY;

    private int xNO = centerX - 150; private int yNO = centerY - 150;
    private int xNE = centerX + 150; private int yNE = centerY - 150;
    private int xSO = centerX - 150; private int ySO = centerY + 150;
    private int xSE = centerX + 150; private int ySE = centerY + 150;
    
    private int xNtO = centerX - 55; private int yNtO = centerY - 55;
    private int xNtE = centerX + 55; private int yNtE = centerY - 55;
    private int xStO = centerX - 55; private int yStO = centerY + 55;
    private int xStE = centerX + 55; private int yStE = centerY + 55;
    private int xNOtN = centerX; private int yNOtN = centerY - 55;
    private int xNEtE = centerX + 55; private int yNEtE = centerY;
    private int xSEtS = centerX; private int ySEtS = centerY + 55;
    private int xSOtO = centerX - 55; private int ySOtO = centerY;

    public RosaDeiVentiComponent(){}


    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.decode("#9999ff"));
        int[] x1 = {centerX, xN, xNtO};
        int[] y1 = {centerY, yN, yNtO};
        g2.fillPolygon(x1, y1, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x2 = {centerX, xN, xNtE};
        int[] y2 = {centerY, yN, yNtE};
        g2.fillPolygon(x2, y2, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x3 = {centerX, xE, xNtE};
        int[] y3 = {centerY, yE, yNtE};
        g2.fillPolygon(x3, y3, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x4 = {centerX, xE, xStE};
        int[] y4 = {centerY, yE, yStE};
        g2.fillPolygon(x4, y4, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x5 = {centerX, xS, xStE};
        int[] y5 = {centerY, yS, yStE};
        g2.fillPolygon(x5, y5, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x6 = {centerX, xS, xStO};
        int[] y6 = {centerY, yS, yStO};
        g2.fillPolygon(x6, y6, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x7 = {centerX, xO, xStO};
        int[] y7 = {centerY, yO, yStO};
        g2.fillPolygon(x7, y7, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x8 = {centerX, xO, xNtO};
        int[] y8 = {centerY, yO, yNtO};
        g2.fillPolygon(x8, y8, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x9 = {centerX, xNOtN, xNO};
        int[] y9 = {centerY, yNOtN, yNO};
        g2.fillPolygon(x9, y9, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x10 = {centerX, xNOtN, xNE};
        int[] y10 = {centerY, yNOtN, yNE};
        g2.fillPolygon(x10, y10, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x11 = {centerX, xNEtE, xNE};
        int[] y11 = {centerY, xNEtE, yNE};
        g2.fillPolygon(x11, y11, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x12 = {centerX, xNEtE, xSE};
        int[] y12 = {centerY, yNEtE, ySE};
        g2.fillPolygon(x12, y12, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x13 = {centerX, xSEtS, xSE};
        int[] y13 = {centerY, ySEtS, ySE};
        g2.fillPolygon(x13, y13, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x14 = {centerX, xSEtS, xSO};
        int[] y14 = {centerY, ySEtS, ySO};
        g2.fillPolygon(x14, y14, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x15 = {centerX, xSOtO, xSO};
        int[] y15 = {centerY, ySOtO, ySO};
        g2.fillPolygon(x15, y15, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x16 = {centerX, xSOtO, xNO};
        int[] y16 = {centerY, ySOtO, yNO};
        g2.fillPolygon(x16, y16, 3);

        g2.setColor(Color.BLACK);
        g2.setFont(new Font("Serif", Font.ITALIC, 36));
        g2.drawChars("N".toCharArray(),0,1,338,100);
        g2.drawChars("S".toCharArray(),0,1,341,727);
        g2.drawChars("O".toCharArray(),0,1,21, 411);
        g2.drawChars("E".toCharArray(),0,1,655,411);
    }

Adding it to my frame works:将其添加到我的框架中:

    public static void frameCreator() {

        frame.add(new RosaDeiVentiComponent());
        frame.setSize(width, height);
        frame.setTitle("APP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

If I set my layout as null it doesn't show up.如果我将布局设置为 null 它不会显示。 Neither if I add it to a panel, like here.如果我将它添加到面板中,就像这里一样。

    public static void frameCreator() {

        frame.setSize(width, height);
        frame.setTitle("APP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        rdvPanel.add(new RosaDeiVentiComponent());
        rdvPanel.setBackground(Color.WHITE);
        //Here I basically set the bound based on the shape dimension
        rdvPanel.setBounds(new Rectangle(0,0, borderX + RosaDeiVentiComponent.getRdvRadius()*2 + 200, height));
        rdvPanel.setVisible(true);
        frame.add(rdvPanel);
}

After the frameCreator method I set the frame as visible.在 frameCreator 方法之后,我将框架设置为可见。

Can anybody tell me where I'm making a mistake?谁能告诉我我在哪里犯了错误? And possibly explaining way the absolute layout doesn't work even if I use absolute coordinate?即使我使用绝对坐标,也可能解释绝对布局不起作用的方式? Thx.谢谢。

You have to set prefferedSize to your component.您必须将 prefferedSize 设置为您的组件。

It would be also good, if you'd call super as a first line in your paintComponent:如果您将 super 作为您的paintComponent 的第一行调用,那也很好:

public void paintComponent(Graphics g){
   super.paintComponent(g);
      //your code
}

By the way, you may initialize all your component's variables in contructor, as most probably you won't use them anywhere else:顺便说一句,您可以在构造函数中初始化所有组件的变量,因为很可能您不会在其他任何地方使用它们:

public RosaDeiVentiComponent(){
   int rdvRadius = 300;
   int centerX = 500;
   int centerY = 500;
   //...
}

You may also consider shorten up creation of your Graphics.您也可以考虑缩短图形的创建时间。

Wind rose is made of only 1 triangle (or you can consider them as pair of triangles) only oriented in different directions (and different size or colour, if you want).风玫瑰仅由 1 个三角形(或者您可以将它们视为一对三角形)组成,仅朝向不同的方向(如果您愿意,可以使用不同的大小或颜色)。

You can create only 1 or 2 triangles and add them in a loop, with different orientations.您只能创建 1 或 2 个三角形并将它们添加到一个循环中,具有不同的方向。 Creating each triangle separately looks redundant分别创建每个三角形看起来是多余的

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

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