Como atualizo o texto quando clicar no botao

9 respostas
java
P

Como atualizo o texto lbl_Vida_Player ,quando clicar no botão

package tela;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Window.Type;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import javax.swing.JSplitPane;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTree;

public class Leyaut {

	private JFrame frmCardGame;

	/**
	 * Launch the application.
	 */
	JTabbedPane painelMenus = new JTabbedPane(JTabbedPane.TOP);
	JPanel panelStatus = new JPanel();
	

	JLabel lbl_Vida_Player = new JLabel();
	JLabel lbl_NomePlayer = new JLabel();
	JLabel lbl_Vida_Inimigo = new JLabel();
	JLabel lbl_NomeInimigo = new JLabel();

	JPanel abaDefender = new JPanel();

	// metodos da clase carts

	Cartas cp = new Cartas();
	
	Cartas ci = new Cartas();

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Leyaut window = new Leyaut();
					window.frmCardGame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */

	public Leyaut() {
		// iniciando clases do jogo




		// inicializa componentes!!
		cp.sortear();
		cp.carta_Sorteada();
		
		ci.sortear();
		ci.carta_Sorteada();
		
		initialize();
		
		// chama a clase das cartas
		cp.Quem_Ataca();

		// define as funçoes permitidas nas abas
		if(cp.getVez()=='p'){
			painelMenus.setEnabledAt(2, false);
		}
		// finalizando tudo
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCardGame = new JFrame();
		frmCardGame.getContentPane().setBackground(Color.BLACK);
		frmCardGame.setResizable(false);
		frmCardGame.setTitle("Card Game");
		frmCardGame.setBackground(Color.WHITE);
		frmCardGame.setBounds(100, 100, 510, 600);
		frmCardGame.setLocationRelativeTo(null);
		frmCardGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmCardGame.getContentPane().setLayout(null);

				JLabel lblGifMoeda = new JLabel("");
				lblGifMoeda.setIcon(new ImageIcon(Leyaut.class.getResource("/Layaut/px (1).gif")));
				lblGifMoeda.setBounds(223, 262, 60, 60);
				frmCardGame.getContentPane().add(lblGifMoeda);
				lblGifMoeda.setBackground(Color.YELLOW);
				lblGifMoeda.setForeground(Color.BLACK);

		//Player
		cp.componentes();
		frmCardGame.getContentPane().add(cp.lblPlayer);
		lbl_NomePlayer= new JLabel(cp.getNome(), SwingConstants.CENTER);
		lbl_Vida_Player = new JLabel(""+cp.getVida(),JLabel.CENTER);

		// Inimigo
		ci.componentes();
		frmCardGame.getContentPane().add(ci.lblInimigo);
		lbl_NomeInimigo= new JLabel(ci.getNome(), SwingConstants.CENTER);
		


		painelMenus.setBounds(1, 284, 501, 280);
		frmCardGame.getContentPane().add(painelMenus);

		
		painelMenus.addTab("STATUS", null, panelStatus, null);
		panelStatus.setLayout(null);

		
		lbl_NomeInimigo.setForeground(Color.BLUE);
		lbl_NomeInimigo.setFont(new Font("Tahoma", Font.BOLD, 20));
		lbl_NomeInimigo.setBounds(350, 24, 147, 31);
		panelStatus.add(lbl_NomeInimigo);

		lbl_Vida_Inimigo = new JLabel(""+ci.getVida(),JLabel.CENTER);
		lbl_Vida_Inimigo.setForeground(Color.GREEN);
		lbl_Vida_Inimigo.setFont(new Font("Tahoma", Font.BOLD, 20));
		lbl_Vida_Inimigo.setBounds(336, 71, 147, 23);
		panelStatus.add(lbl_Vida_Inimigo);

		JLabel label_6 = new JLabel("0", SwingConstants.CENTER);
		label_6.setForeground(Color.WHITE);
		label_6.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_6.setBounds(336, 93, 147, 23);
		panelStatus.add(label_6);

		JLabel label_7 = new JLabel("0", SwingConstants.CENTER);
		label_7.setForeground(Color.GREEN);
		label_7.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_7.setBounds(336, 136, 147, 23);
		panelStatus.add(label_7);

		JLabel label_8 = new JLabel("0", SwingConstants.CENTER);
		label_8.setForeground(Color.WHITE);
		label_8.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_8.setBounds(336, 158, 147, 23);
		panelStatus.add(label_8);

		JLabel label_2 = new JLabel("0", SwingConstants.CENTER);
		label_2.setForeground(Color.GREEN);
		label_2.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_2.setBounds(81, 136, 147, 23);
		panelStatus.add(label_2);

		JLabel label_3 = new JLabel("0", SwingConstants.CENTER);
		label_3.setForeground(Color.WHITE);
		label_3.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_3.setBounds(81, 158, 147, 23);
		panelStatus.add(label_3);
		// Texto centralizado!!!!
		JLabel label_1 = new JLabel("0", SwingConstants.CENTER);
		label_1.setForeground(Color.WHITE);
		label_1.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_1.setBounds(81, 93, 147, 23);
		panelStatus.add(label_1);

		// bara de vida player

		lbl_Vida_Player.setFont(new Font("Tahoma", Font.BOLD, 20));
		lbl_Vida_Player.setForeground(new Color(0, 255, 0));
		lbl_Vida_Player.setBounds(81, 71, 147, 23);
		panelStatus.add(lbl_Vida_Player);

	
		lbl_NomePlayer.setFont(new Font("Tahoma", Font.BOLD, 20));
		lbl_NomePlayer.setForeground(new Color(0, 0, 255));
		lbl_NomePlayer.setBounds(95, 24, 147, 31);
		panelStatus.add(lbl_NomePlayer);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setIcon(new ImageIcon(Leyaut.class.getResource("/Layaut/PlayerLeyaut.jpg")));
		lblNewLabel.setOpaque(true);
		lblNewLabel.setBackground(Color.BLUE);
		lblNewLabel.setBounds(0, 0, 250, 252);
		panelStatus.add(lblNewLabel);

		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Leyaut.class.getResource("/Layaut/InimigoLayaut.jpg")));
		label.setOpaque(true);
		label.setBackground(Color.BLUE);
		label.setBounds(252, -1, 250, 252);
		panelStatus.add(label);

		JPanel abaAtacar = new JPanel();
		abaAtacar.setLayout(null);
		painelMenus.addTab("Ataque", null, abaAtacar, null);

		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Normal", "Eskil_1", "Eskil_2", "Utimate"}));
		comboBox_1.setBounds(0, 0, 103, 27);
		abaAtacar.add(comboBox_1);
		
		JButton btnNewButton = new JButton("CONFIRMAR");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				inimigoApanha();
			
				
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnNewButton.setBounds(48, 205, 159, 36);
		abaAtacar.add(btnNewButton);
		
		// botao 
		

		JLabel label_10 = new JLabel("");
		label_10.setIcon(new ImageIcon(Leyaut.class.getResource("/Layaut/AtaqueLeyaut.jpg")));
		label_10.setBounds(0, 0, 496, 252);
		abaAtacar.add(label_10);

		//painel Menus
		painelMenus.addTab("DEFENDER", null, abaDefender, null);
		abaDefender.setLayout(null);

		JComboBox cbox_Defesas = new JComboBox();
		cbox_Defesas.setBounds(0, 0, 103, 27);
		abaDefender.add(cbox_Defesas);
		cbox_Defesas.setModel(new DefaultComboBoxModel(new String[] {"Defesa", "Esquiva"}));

		JLabel lbl_BK_defesa = new JLabel("");
		lbl_BK_defesa.setBounds(0, 0, 496, 252);
		abaDefender.add(lbl_BK_defesa);
		lbl_BK_defesa.setIcon(new ImageIcon(Leyaut.class.getResource("/Layaut/DefesaLeyaut.jpg")));
	}
	//

	public void inimigoApanha(){
		ci.setVida(ci.getVida()-cp.getAtaque());
		lbl_Vida_Inimigo = new JLabel(""+ci.getVida(),JLabel.CENTER);
		lbl_Vida_Inimigo.setForeground(Color.GREEN);
		lbl_Vida_Inimigo.setFont(new Font("Tahoma", Font.BOLD, 20));
		lbl_Vida_Inimigo.setBounds(336, 71, 147, 23);
		panelStatus.add(lbl_Vida_Inimigo);
		
		System.out.println("vida player "+cp.getVida());
		System.out.println("at Inimigo "+cp.getAtaque());
		System.out.println("vida Inimigo "+ci.getVida());
		System.out.println("at Inimigo "+ci.getAtaque());

	}
}


package tela;

import java.awt.Color;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;



public class Cartas {
	private String nome;
	private int vida= 0;
	private int mana= 0;
	private int ataque= 0;
	private int defesa= 0;
	private int carta= 0;
	
	int cartaSorteada;
	
	private Random random= new Random();
	private int agilidade= 0;
	private char vez =' ';
	// deck das cartas
	
	JLabel lblPlayer= new JLabel();
	JLabel lbl_Vida_Player = new JLabel();
	JLabel lblInimigo = new JLabel();
	
	public void componentes(){
	lblPlayer.setBounds(1, 9, 250, 250);
	lblPlayer.setOpaque(true);
	lblPlayer.setForeground(Color.WHITE);
	lblPlayer.setBackground(new Color(0, 0, 153));
	
	lbl_Vida_Player = new JLabel(""+vida,JLabel.CENTER);
	//
	lblInimigo.setBounds(253, 9, 250, 250);
	lblInimigo.setOpaque(true);
	lblInimigo.setForeground(Color.WHITE);
	lblInimigo.setBackground(new Color(153, 0, 0));
		
	}
	
	// metodo sortera carta
	public void sortear(){
		cartaSorteada=random.nextInt(2)+1;
		System.out.println(cartaSorteada);
	}
	
	public void carta_Sorteada(){
		
		if(cartaSorteada==1){
			
		this.vida=200;
		lblPlayer.setIcon(new ImageIcon(Leyaut.class.getResource("/cartas/Fayre_Master.gif")));
			lblInimigo.setIcon(new ImageIcon(Leyaut.class.getResource("/cartas/Fayre_Master.gif")));
			this.nome= "Fayre_Master";
			this.vida = 200;
			this.ataque =10;
			this.mana= 5;
		}
		else{
			lblPlayer.setIcon(new ImageIcon(Leyaut.class.getResource("/cartas/Zel.gif")));
			lblInimigo.setIcon(new ImageIcon(Leyaut.class.getResource("/cartas/Zel.gif")));
			this.nome= "Zel";
			this.vida = 150;
			this.ataque =10;
			this.mana= 10;
		}
		
	}
	
	
	
	
	


	// ataques
	//eskil-1
	public void socode_lava(){
		ataque= random.nextInt(10)+6;
		mana+=-1;
	}

	
	
	
	// geters
	
	public int getAtaque() {
		return ataque;
	}

	public void setAtaque(int ataque) {
		this.ataque = ataque;
	}

	public int getVida() {
		return vida;
	}

	public void setVida(int vida) {
		this.vida = vida;
	}
	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public int getMana() {
		return mana;
	}

	public void setMana(int mana) {
		this.mana = mana;
	}

	public int getAgilidade() {
		return agilidade;
	}

	public void setAgilidade(int agilidade) {
		this.agilidade = agilidade;
	}

	public void Quem_Ataca(){
		agilidade = random.nextInt(5)+1;
		
		if (this.agilidade >= 4){
			vez ='p';
		}
	
	}

	public char getVez() {
		return vez;
	}

	public void setVez(char vez) {
		this.vez = vez;
	}
	
	
	
	
}

9 Respostas

R

que botão??

O mais básico é:

lbl_Vida_Player.setText("texto");
P

tb achei mas não quando aperta o botão não muda

R

botao ta na mesma classe? da erro?

P

ta na mesma classe nao avisa erro nenhum. apenas na janela do programa nao muda, porem muda os valores na tela do console

P

vi aqui q o player troca maisdo outro lbl nao

R

Qual é o botão?

P

aa consegui montar meu Frankenstein, sou péssimo em arrumar as linhas de códigos, mas corrigi nao sei bem o que fiz mas funcionou

S

Isso não é bom, aparentemente estás programando na tentativa e erro. :frowning:

P

rsrs to fazendo 3 cursos e tentando ler e entender os métodos ja aprendi muita coisa mas parece que quanto mais aprendo mais as lições dobram kkkk algumas coisas confesso que vou no chutometro

Criado 9 de dezembro de 2020
Ultima resposta 10 de dez. de 2020
Respostas 9
Participantes 3