Você está na página 1de 3

Primeiros commandos para criar a janela

import java.awt.*;

import javax.swing.*;

public Tela(){

this.setSize(500, 250);

this.setTitle("Janela");

this.setLocation(400, 400);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

Declaracao de campos , butoes ..

ttf1= new JTextField(14);

ttf1.setText("Texto Inicial 1");

ttf2= new JTextField(14);

ttf2.setText("Texto Inicial 2");

btn1 = new JButton("Botão 1");

btn2= new JButton("Botão 2");

lbl1 = new JLabel("Muda cor e imprime texto das caixas: ");

lbl2 = new JLabel("Muda cor e imprime texto dos botões: ");

lbl3 = new JLabel(" Oeste!");

lbl4 = new JLabel("Leste! ");

como usar o GridLayout, que organiza os dados em forma de grelha um ao lado e embaixo

tL = new JPanel();

tL.setLayout(new GridLayout(2,2));

tL.add(lbl1); na lina 1 coluna 1

tL.add(btn1); linha 1 coluna 2

tL.add(lbl2); linha 2 coluna 1

tL.add(btn2);
Em seguida temos o border layout, organiza os componetes as bordas da janela

Container ctn = janela.getContentPane();

ctn.setLayout(new BorderLayout());

ctn.add(tL, BorderLayout.NORTH); esta linha e responsavel por colocar o gridlayout na parte


superiro da janela

ctn.add(lbl3, BorderLayout.WEST);

ctn.add(lbl4, BorderLayout.EAST);

Temos o Flow layout que e responsavel por colocar lado a lado os componentes , muito usado para criar
menus superiores

bL= new JPanel();

bL.setLayout(new FlowLayout());

bL.add(ttf1);

bL.add(ttf2);

ctn.add(bL, BorderLayout.SOUTH); adicionamos a borda que esta na parte inferiror

NO caso de queremos usar os actionListner temos que adicionar

btn1.addActionListener(this);

btn2.addActionListener(this);

janela.addWindowListener(this);//Isso e muito importante para os Windows listner funcionarem

janela.setVisible(true);

Alguns actionlistner apos o botam ser clicado

public void actionPerformed(ActionEvent e) {

if(e.getSource()==btn1){

String msg="Caixa de texto 1 contem: "+ttf1.getText()+"\nCaixa de texto 2 contem:


"+ttf2.getText();

JOptionPane.showMessageDialog(null, msg);

ttf1.setText("Apertou Botao 1");

ttf2.setText("Tente botao 2");

btn1.setBackground(new java.awt.Color(0,0,255));
btn2.setBackground(new java.awt.Color(0,191,255));

if(e.getSource()==btn2){

String msg="Modificando a Interface";

JOptionPane.showMessageDialog(null, msg, "Aviso", JOptionPane.WARNING_MESSAGE);

ttf1.setText("Apertou Botao 2");

ttf2.setText("Tente botao 1");

tL.setBackground(Color.yellow);

bL.setBackground(new java.awt.Color(255, 215, 0));

btSave.add ActionListner (new ActionListner()){

public void actionPerfotmed (ActionEvent evento){

double nota1 = Double.parseDouble(txtNota.getText());

double nota2 = Double.parseDouble(txtNota.getText());

int nuit= Integer.parseInt(txtNuit.getText());

String nome= txtNome.getText();

Double soma= nota+nota2;

txtResultado.setText(string.valueOf(soma));

Trabalhador trqab= new Trabalhador (nuit, nome);

Model.addRow(new Object[]{nuit, nome};

Você também pode gostar