Shapes are not drawn on JFrame (JPanel) when Layout(null)

enter a description of the image hereYou need to display buttons and shapes(arrows/lines) on one JFrame. For the location of the buttons, I set layout null, but the elements from the paintComponent method are not drawn. If layout is the default, then the line is drawn, but the layout of the buttons is spread out, as I don't need it. How do I draw a shape (a line, in particular) with layout null ? Goal: implementation of a similar algorithm https://www.scss.tcd.ie/Jeremy.Jones/vivio/caches/MESI.htm

public class Check extends JPanel {

public Check() {
}

public void draw(){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLayout(null); // ??????
    frame.add(this);
    frame.setSize(600,600);
    frame.setVisible(true);
}

@Override
public void paintComponent(Graphics g){
    super.paintComponents(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawLine(10,10,500,500);

}

public static void main(String[] args) {
    Check inst = new Check();
    inst.draw();
}
}
Author: Ilya7cc, 2018-05-04

1 answers

You need to change the approach a little, namely to split the panel for drawing components and graphics into several. In the example below:

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

    public class Check {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(new MyButtonPanel(), BorderLayout.NORTH);
                frame.add(new MyDrawPanel(), BorderLayout.CENTER);
                frame.setSize(600, 600);
                frame.setVisible(true);
            });
        }

        static class MyButtonPanel extends JPanel {
            private JButton button = new JButton("1");
            private JButton button1 = new JButton("2");
            private JButton button2 = new JButton("3");
            private JButton button3 = new JButton("4");
            private JButton button4 = new JButton("5");

            public MyButtonPanel() {
                add(button);
                add(button1);
                add(button2);
                add(button3);
                add(button4);
            }
        }

        static class MyDrawPanel extends JPanel {
            public MyDrawPanel() {
                setLayout(null);
            }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponents(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.drawLine(10, 10, 500, 500);
            }
        }
    }

A panel with buttons is one panel, and a line is another.

UPD Example on one panel:

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

    public class Check {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(new MyDrawPanel(), BorderLayout.CENTER);
                frame.setSize(600, 600);
                frame.setVisible(true);
            });
        }

        static class MyDrawPanel extends JPanel {
            private JButton button = new JButton("1");
            private JButton button1 = new JButton("2");
            private JButton button2 = new JButton("3");
            private JButton button3 = new JButton("4");
            private JButton button4 = new JButton("5");

            MyDrawPanel() {
                setLayout(null);
                button.setSize(50, 20);
                button.setLocation(10, 10);
                add(button);
                button1.setSize(50, 20);
                button1.setLocation(40, 40);
                add(button1);
                button2.setSize(50, 20);
                button2.setLocation(50, 50);
                add(button2);
                button3.setSize(50, 20);
                button3.setLocation(80, 80);
                add(button3);
                button4.setSize(50, 20);
                button4.setLocation(10, 10);
                add(button4);
            }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponents(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.drawLine(10, 10, 500, 500);
            }
        }
    }
 0
Author: ezhov_da, 2018-05-04 08:49:14