Label is not rendering | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Label is not rendering

Can someone tell me what is the cause of the fact that I am adding a JLabel to my JFrame but it doesn't render anything? Here is my code: public static class Frame extends javax.swing.JFrame { public Canvas canvas = new Canvas(); // canvas is an extended version of JPanel public Frame(Vector dimension, String title) { this.setSize((int)dimension.x, (int)dimension.y); this.setTitle(title); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.setBounds(0, 0, (int)dimension.x, (int)dimension.y); this.setResizable(true); javax.swing.JLabel label = new javax.swing.JLabel("Cancel"); // doesn't render for some reason this.add(label); this.setLocationRelativeTo(null); this.add(canvas); this.setVisible(true); } private static final long serialVersionUID = 1L; }

27th Dec 2020, 11:21 AM
Frumkin
Frumkin - avatar
2 Answers
0
Here is the code to my Panel: Now will explain what I have done: "Functionality is a class that holds a Graphics component to draw to the JPanel, the reason there is also a buffered image is everything that I am drawing to the panel, is save to an image so I can save it to my desktop. Then to render it basically draws the image with all the graphic contents. And the panel does render correctly. In Functionality there are functions that simplify the java awt drawing methods for instance: "rect(x, y, width, height) { graphics.drawRectangle(x, y, width, height); }". In Handler.draw() I am calling Functionality methods like rect(). public static class Canvas extends javax.swing.JPanel { @Override public void paintComponent(java.awt.Graphics graphics) { super.paintComponent(graphics); Functionality.graphics = (java.awt.Graphics2D)image.createGraphics(); graphics.clearRect(0, 0, (int)width(), (int)height()); Handler.draw(); graphics.drawImage(image, 0, 0, this); graphics.dispose(); image = new java.awt.image.BufferedImage((int)width(), (int)height(), java.awt.image.BufferedImage.TYPE_INT_RGB); } private static final long serialVersionUID = 1L; } Also, width() and height() return the dimensions of the frame.
27th Dec 2020, 11:56 AM
Frumkin
Frumkin - avatar
0
After a lot of research I realized that there was a conflict in my panel between awt graphics and swing components. I found out there was a drawString method.
27th Dec 2020, 12:20 PM
Frumkin
Frumkin - avatar