My JLabel does not appear [closed]

1

I've used a couple of commands to make this Label appear, but I can not, in that object I'm using paint and ActionPerformed , but no drawing is overlapping this label and even then it does not appear, p>

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);

    }

link

    
asked by anonymous 11.12.2017 / 19:31

1 answer

1

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

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

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

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

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

11.12.2017 / 23:44