Vilarejo PK2P
Bem-vindo ao fórum Vilarejo PK2P! O fórum oficial do canal Press Key to Play do Youtube!

Participe do fórum, é rápido e fácil

Vilarejo PK2P
Bem-vindo ao fórum Vilarejo PK2P! O fórum oficial do canal Press Key to Play do Youtube!
Vilarejo PK2P
Gostaria de reagir a esta mensagem? Crie uma conta em poucos cliques ou inicie sessão para continuar.

Grupo para reunir as pessoas que acompanham os vídeos no canal Press Key to Play e discutir os assuntos abrangidos pelo Blog PK2P.

Últimos assuntos

» RPG de Mesa: A fronteira Volume 4: O Castelo Owlrick. (D&D5e)
por Ariano 10/12/2020, 23:18

» [tutorial] hash em C/C++ com openssl
por kodo no kami 21/9/2020, 13:37

» ping lista grafico
por kodo no kami 24/7/2020, 07:53

» [Discord] Campanha de RPG D-cyber
por Jonatan15 17/7/2020, 11:11

» [tradução] 1869 (msdos)
por kodo no kami 26/6/2020, 13:43

» [source] exemplo filtro mediano
por kodo no kami 1/6/2020, 00:24

» [tutorial] criar filtro nas imagens (python)
por kodo no kami 22/5/2020, 19:29

» Wireless IDS Sagemcom
por kodo no kami 22/4/2020, 23:50

» [source] detectar dispositivos no wifi (sagemcom)
por kodo no kami 9/3/2020, 20:25

» [tutorial] wla assemblador (65xx, 68xx, z80, 8008, 8080 e huc6280)
por kodo no kami 2/2/2020, 16:42


Você não está conectado. Conecte-se ou registre-se

[tutorial] java swing: parte 5

Ir para baixo  Mensagem [Página 1 de 1]

kodo no kami

kodo no kami

continuando o tutorial de java, nessa parte vamos aprender a manipular o componente JTextArea que nos permite escrever uma quantidade maior de texto, para começar a gente cria a nossa janela basica como nos tutoriais anteriores

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
    }
}

[tutorial] java swing: parte 5 OvqXEZh

agora a gente instancia um objeto do tipo JTextArea (javax.swing.JTextArea)

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
    }
}

como o layout do nosso programa é absoluto temos que especificar a posição, largura e altura do nosso objeto para adicionar ele na nossa janela

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   
   janela.add(texto);
    }
}

[tutorial] java swing: parte 5 7BiGXzv

é possivel passar uma string assim que instanciar o objeto

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea("by kodo no kami");
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   
   janela.add(texto);
    }
}

ou alterar o texto usando o metodo setText (para ler usamos o getText)

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   texto.setText("kodo no kami");
   
   janela.add(texto);
    }
}

[tutorial] java swing: parte 5 FsUvMQD

com o metodo setForeground alteramos a cor do texto

Código:
package kodo;

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

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   texto.setForeground(Color.red);
   
   janela.add(texto);
    }
}

com o setBackground alteramos a cor do fundo

Código:
package kodo;

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

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   texto.setBackground(Color.blue);
   
   janela.add(texto);
    }
}

para mudar a fonte e o tamaho dela usamos o metodo setFont, passamos como argumento para ela um objeto do tipo Font

Código:
package kodo;

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

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   Font minhafonte = new Font("arial",Font.BOLD,15);
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   texto.setFont(minhafonte);
   
   janela.add(texto);
    }
}

um outro exemplo mudando a fonte, cor do texto e background

Código:
package kodo;

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

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   Font minhafonte = new Font("arial",Font.BOLD,15);
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   janela.getContentPane().setBackground(Color.black);
   texto.setBackground(Color.darkGray);
   texto.setForeground(Color.white);
   texto.setFont(minhafonte);
   
   janela.add(texto);
    }
}

[tutorial] java swing: parte 5 MhQK6QR

podemos desativar o componente com o metodo setEnabled passando como argumento false com isso não sera possivel escrever diretamente nele, ou ativar ele novamente passando como argumento true

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   texto.setEnabled(false);
   
   janela.add(texto);
    }
}

tambem podemos deixar o componente invisivel com o metodo setVisible

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   texto.setVisible(false);
   
   janela.add(texto);
    }
}

para a gente colocar um scroll temos que instanciar outro objeto sendo ele o JScrollPane (javax.swing.JScrollPane), passamos como argumento o objeto do JTextArea e adicionamos na janela o objeto do JScrollPane e não do JTextArea

Código:
package kodo;

import javax.swing.*;

public class Principal {
    private static JFrame janela;
    private static JTextArea texto;
    private static JScrollPane rolagem;
   
    public static void main(String[] args){
   janela = new JFrame("minha janela");
   texto = new JTextArea();
   rolagem = new JScrollPane(texto);
   
   janela.setBounds(400,350,350,200);
   janela.setDefaultCloseOperation(janela.EXIT_ON_CLOSE);
   janela.setVisible(true);
   janela.setLayout(null);
   
   texto.setBounds(10,10,300,150);
   rolagem.setBounds(10,10,300,150);
   
   janela.add(rolagem);
    }
}

[tutorial] java swing: parte 5 L03Ukgc

bom galera com JTextArea a gente pode criar um editor de texto com muita facilidade bastando apenas salvar o texto dele um em arquivo ou carregar o texto para ele de um arquivo, ate a proxima parte desse tutorial então "ja ne minna-san" \o

by kodo no kami

https://eofclub.in/forum

Ir para o topo  Mensagem [Página 1 de 1]

Permissões neste sub-fórum
Não podes responder a tópicos