I want to inherit JLabel swing components and make and use custom components

Asked 1 years ago, Updated 1 years ago, 104 views

Hello, I'm an undergraduate who is taking a computer program. These days, I'm studying GUI to make a board game assignment. Even if I google it, I don't know the cause of the problem.

The board game you're trying to make has 37 circular vertices, and the game horse is located at these vertices, and you decide to inherit the swing component JLabel to get these circular vertices processed as well.

But it's not being implemented as I want. Once tested, we aimed to float three circles with radius 17 on (0,0), (100,100), (200,200), respectively, but as shown in the image below, only the circles are visible on (0,0) and (200,200), and the other one is not visible. In the code, unannotating the annotated setLayout (null) will only show the circle located at (0,0), but not the rest. As far as I know, the default batch manager of the cont fan is known as FlowLayout, and he used setLayout(null) to use the absolute position of the component, so I'm curious about this result.

Also, I thought there was a problem with the paint method, but I think there is no problem because this method is simply called by JVM to draw pictures at the specified location when creating components. I don't know if I'm missing something or if I'm misunderstanding it. Please give me some advice. Thank you!

public class CircleFrame extends JFrame{    
    public CircleFrame() {
        Container c = getContentPane();
        //c.setLayout(null);

        BoardCircle c1 = new BoardCircle(0,0);
        c1.setLocation(c1.getCircleX(), c1.getCircleY());
        c1.setSize(34, 34);
        c.add(c1);

        BoardCircle c2 = new BoardCircle(100,100);
        c2.setLocation(c2.getCircleX(), c2.getCircleY());
        c2.setSize(34, 34);
        c.add(c2);

        BoardCircle c3 = new BoardCircle(200,200);
        c3.setLocation(c3.getCircleX(), c3.getCircleY());
        c3.setSize(34, 34);
        c.add(c3);

        setSize(500, 500);
        setVisible(true);
        setTitle("Circle Class Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new CircleFrame();
    }
}


class BoardCircle extends JLabel implements Serializable{
    int radius = 17;
    int x, y;

    BoardCircle( int x, int y){
        this.x = x;
        this.y = y;
        setSize(radius * 2, radius * 2);
        setVisible(true);
    }

    public int getCircleX() {
        return x;
    }

    public int getCircleY() {
        return y;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g) ;
        g.setColor(Color.BLUE);
        g.fillOval(x, y, radius * 2, radius * 2);
    }
}

java swing

2022-09-22 18:06

1 Answers

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

public class CircleFrame extends JFrame{    
    public CircleFrame() {
        Container c = getContentPane();
        c.setLayout(null);

        BoardCircle c1 = new BoardCircle(0,0);
        c.add(c1);

        BoardCircle c2 = new BoardCircle(100,100);
        c.add(c2);

        BoardCircle c3 = new BoardCircle(200,200);
        c.add(c3);

        setSize(500, 500);
        setVisible(true);
        setTitle("Circle Class Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new CircleFrame();
    }
}


class BoardCircle extends JLabel{
    int radius = 17;

    BoardCircle(int x, int y){
        setLocation(x, y);
        setSize(radius * 2, radius * 2);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillOval(0, 0, radius * 2, radius * 2);
    }
}

In each component, the graphics context obtained by the paint method is a separate context and seems to be positioned based on the component's own location. (I don't know the structure of the swing components... I don't know exactly.)

Assuming that the above guess is correct,

The component has already been positioned with setLocation() g.fillOval() must contain the value (0, 0) considering the relative position.


2022-09-22 18:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.