My Jlabel does not appear

Asked

Viewed 185 times

1

I’ve used several commands to make this label appear, but I can’t, in this object I’m using Paint and ActionPerformed, but no Drawing this overlapping this label and even then it does not appear, someone has idea why?

package scenes;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Title extends JPanel implements ActionListener{

    JLabel press_enter = new JLabel("Pressione ENTER para começar", JLabel.CENTER);
    Font font = new Font("arial", Font.BOLD, 32);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    private BufferedImage city1;
    private BufferedImage city2;
    private BufferedImage tipography;
    private int phase = 1;
    private int city1_h = 1;
    private int city1_h_final = 5;
    private int city2_h = 1;
    private int city2_h_final = 5;
    private int city1_y = 220;
    private int city1_y_final = 0;
    private int city2_y = 218;
    private int city2_y_final = 0;
    private int bloco1 = 0;
    private int bloco2 = 0;
    private boolean loaded = false;
    private float alpha = 1.0f;

    public Title(){

        setSize( 800, 600 );
        setBackground(Color.WHITE);
        Timer t = new Timer(20, this);
        t.start();
        press_enter.setAlignmentX(0);
        press_enter.setAlignmentY(0);
        press_enter.setBounds(130, 280, 200, 60);
        press_enter.setVisible(true);
        press_enter.setForeground(Color.BLACK);
        add(press_enter);

    }

    public void paint(Graphics g){
        super.paintComponent(g);
        if (loaded == false) {
            try {
                tipography = ImageIO.read(new File("images/scene/tipo.png"));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("imagem não encontrada!");
            }
            try {
                city1 = ImageIO.read(new File("images/scene/logo2.png"));
                city1_h_final = city1.getHeight();
                city1_h_final = city1_h_final - (city1_h_final * 2);
                city1_y = 150 + (tipography.getHeight()/2);
                city1_y_final = 148;
                bloco1 = 150 + (tipography.getHeight()/2);

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("imagem não encontrada!");
            }
            try {
                city2 = ImageIO.read(new File("images/scene/logo2.png"));
                city2_h_final = city2.getHeight();
                city2_y = 150 + (tipography.getHeight()/2) - 2;
                city2_y_final = 150 + (tipography.getHeight()/2) * 2;
                bloco2 = 150 + (tipography.getHeight()/2) - 2;
                loaded = true;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("imagem não encontrada!");
            }
        }
        Graphics2D city1_2d = (Graphics2D) g;
        Graphics2D city2_2d = (Graphics2D) g;
        Graphics2D tipography_2d = (Graphics2D) g;
        city1_2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        tipography_2d.drawImage(tipography, (800/2) - (tipography.getWidth()/2), 150, tipography.getWidth(), tipography.getHeight(), null);
        g.setColor(Color.WHITE);
        g.fillRect((800/2) - (city1.getWidth()/2), bloco1, city1.getWidth(), city1_h_final);
        g.fillRect((800/2) - (city1.getWidth()/2), bloco2, city1.getWidth(), city2_h_final);
        city1_2d.drawImage(city1, (800/2) - (city1.getWidth()/2), city1_y, city1.getWidth(), city1_h, null);
        city2_2d.drawImage(city2, (800/2) - (city2.getWidth()/2), city2_y, city2.getWidth(), city2_h, null);
    }

    public void actionPerformed(ActionEvent e) {
        if (phase == 1){
            if (city1_h > city1_h_final)
                city1_h -= 1;
            else if (city1_h == city1_h_final)
                phase = 2;
            if (city2_h < city2_h_final)
                city2_h += 1;

            repaint();
        } else if (phase == 2) {
            if (city1_y > city1_y_final){
                city1_y -= 1;
                bloco1 -= 1;
            }
            if (city2_y < city2_y_final){
                city2_y += 1;
                bloco2 += 1;
                add(press_enter);
            }
            repaint();
        }
    }   
}

package engine;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class Display extends JFrame {

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    public Display(){
        setTitle("BlackJack");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800,600);
        setResizable(false);
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        setVisible(true);
        setLayout(new GridLayout(1,1,0,0));
    }

}

public class Title extends JPanel implements ActionListener{

    JLabel press_enter = new JLabel("Pressione ENTER para começar", JLabel.CENTER);
    Font font = new Font("arial", Font.BOLD, 32);

    public Title(){

        setVisible(true);
        setLocation (0,0);
        setSize( 800, 600 );
        setBackground(Color.WHITE);
        Timer t = new Timer(20, this);
        //t.start();
        press_enter.setAlignmentX(0);
        press_enter.setAlignmentY(0);
        //press_enter.setFont(font);
        press_enter.setBounds(130, 280, 200, 60);
        press_enter.setVisible(true);
        press_enter.setForeground(Color.BLACK);
        add(press_enter);

    }

https://github.com/Forsaiken/BlackJack

  • I am voting to close this question because the problem described by the author has already been solved in the answer, and the same is either not able to demonstrate the real problem or is not something reproducible.

1 answer

1


The cause of JLabel not being displayed is exactly in this section below the class Title:

public void paint(Graphics g){
    super.paintComponent(g);

Can you fix something wrong there? You wrongly overwrite the method paint(), but invokes the paintComponent(). I believe it was a typo, because changing the signature of the method to public void paintComponent(Graphics g) the label is displayed normally.

However, even so I suggest you read the links below to understand why this occurred and how to properly start a swing application:

If the goal is to make a splash screen, I will also leave below an example answered right here on the site, the way you are doing is not correct:

  • I already put the Jpanel in a Jframe, this is just an object, the main function is not in it and the main function already adds the panel in the frame. The Drawings I used in the Paint of this class appear normally, so the panel is in the frame.

  • @Forsaiken for me worked normally, just run the answer code.

  • I put in question the codes of Jframe and Main the application continues to ignore Jlabel

  • @Forsaiken see the answer edition, if you still want something more specific, edit the question and provide more details of the problem and also access the following link and provide a [mcve] of your code.

  • follows the repository of this project: https://github.com/Forsaiken/BlackJack I used thread.Sleep to hold the animation of the intro that lasts 7 seconds and then move on to Title. I’m still learning this part of animation, but I’ll give a read, the thread control part I haven’t learned yet.

  • @Forsaiken the solution is in the answer, just a read. What you are doing of intro is called SPLASH SCREEN, and also has in my answer. If that’s not what you wanted, then your question is unclear.

  • I read, you asked to remove the thread.Leep, I removed, the label still does not appear. Invokelater did not understand its function well, but when I insert it into main, it makes the intro get bugged and does not show the Drawing of it. i removed setvisible and setlocation from Jpanel and remains the same. The frame setsize is already on display.java. The actionperfomed I did not insert in the question because I found it unnecessary, because the label does not go in it, the actionperfomed is doing help in the animation of Title. Anyway Label still not appearing in the panel.

  • @Forsaiken looks at the question print. the label is at the top!

  • Yes I understand @Articuno, your code being made of a zero project it works, because it normally creates the Label, in my project and in this class it is not created, even if it is totally correct. I edited the question and put all the code you have in the Title class. Class Title is running normally but the label does not appear at all. The class Intro I think it is not necessary to put, because it is running normally. I need to know what’s causing this bug on Jlabel so it doesn’t appear.

  • @Forsaiken the images you are adding are overwriting the label, try to remove all the images, run the code and see if the label displays. Then reset the images and run and see if the label goes missing. Do this ai test.

  • @Forsaiken there is no problem in your project, just removing the excerpt I suggested in the reply, appeared here normally, therefore, I am voting to close your question because it is not possible to reproduce the problem or you do not know demonstrate it on the site.

  • I removed all the drawimages and tested and it still doesn’t show up. I removed the timer (Actionperformed) and Paint, but it still doesn’t show up. I have removed the thread.Sleep and it does not appear either. If there is no solution I can close, I can use the Paint to draw the label and solve the problem.

  • @Forsaiken found the cause, it was an error in your method of drawing the Title class, please review the answer and apply the suggested modifications.

  • Finally, Thanks a lot @Articuno, now it worked.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.