Can't see a single element in a JFrame?

Below is the code where I am trying to draw some graphics on a JFrame. I am trying to install a JFrame layout that gives an error. But if I do not configure the layout, the code works fine, but it will not be what I want. I can’t understand what the problem is. Please help! =)

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

class GuiForJFrame extends JFrame {
    private FlowLayout layout;
    private Container container;

    public GuiForJFrame() {
        super("Drawing Graphics");

        // Setting the Layout
        layout = new FlowLayout(FlowLayout.LEFT);
        container = getContentPane();
        setLayout(layout);
    }
}

class GuiForDrawingGraphics extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        g.setColor(Color.RED);
        g.fillRect(25, 25, 150, 50);

        g.setColor(new Color(156, 32, 111));
        g.fillRect(25, 80, 150, 50);

        g.setColor(Color.BLACK);
        g.drawString("Drawing Graphics in JAVA", 25, 150);

    }
}

public class Application {
    public static void main(String[] args) {
        // Creating the JFrame object
        GuiForJFrame jFrame = new GuiForJFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Adding Graphics to JFrame
        GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
        jFrame.add(graphics);
        jFrame.setSize(500, 200);
        jFrame.setVisible(true);
    }
}
+4
source share
3 answers

As mentioned in another answer, a custom drawing panel does not have its own size. It should return the preferred size suitable for the content. Then we need pack()to create a frame after adding it.

enter image description here

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

class GuiForJFrame extends JFrame {
    private FlowLayout layout;
    private Container container;

    public GuiForJFrame() {
        super("Drawing Graphics");

        // Setting the Layout
        layout = new FlowLayout(FlowLayout.LEFT);
        container = getContentPane();
        setLayout(layout);
    }

    public static void main(String[] args) {
        // Creating the JFrame object
        GuiForJFrame jFrame = new GuiForJFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Adding Graphics to JFrame
        GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
        jFrame.add(graphics);
        //jFrame.setSize(500, 200);
        jFrame.pack();
        jFrame.setVisible(true);
    }
}

class GuiForDrawingGraphics extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        g.setColor(Color.RED);
        g.fillRect(25, 25, 150, 50);

        g.setColor(new Color(156, 32, 111));
        g.fillRect(25, 80, 150, 50);

        g.setColor(Color.BLACK);
        g.drawString("Drawing Graphics in JAVA", 25, 150);

    }

    public Dimension getPreferredSize() {
        return new Dimension(300,200);
    }
}
+2
source

jFrame.pack(); jFrame.validate();. ( jFrame), jFrame , no Exception .

, ( ).

, Application :

public class Application {
    public static void main(String[] args) {
        // Creating the JFrame object
        GuiForJFrame jFrame = new GuiForJFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Adding Graphics to JFrame
        GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
        jFrame.add(graphics);
        jFrame.setSize(500, 200);

        // Displaying the JFrame properly.
        jFrame.pack();
        jFrame.validate();
        jFrame.setVisible(true);
    }
}

, setPreferredSize(Dimension dimension), , JComponent.

JComponent JFrame http://docs.oracle.com/javase/7/docs/api/ .

+1

JFrame BorderLayout, , - , , ​​ BorderLayout.

FlowLayout, , exapend , . , FlowLayout , .

() .

http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html

, , paintComponent:

setSize(200, 100); // values depend on your components

, , (BorderLayout) , BoxLayout, , ,

see this for other types of layouts: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

+1
source

Source: https://habr.com/ru/post/1547456/


All Articles