Bom dia
Pessoal como eu faria para criar 4 paineis com cores diferentes e quando eu iniciar o programa ele carregue exibindo o painel um e a cada clique no painel ele mude do 1 para o 2, clicando no 2 ele mude para o 3, clicando no 3 muda para o 4 e clicando no 4 volta para o um? Sei como criar um painel e adicionar ele na tela mas esse movimento com click não tenho idéia de como fazer, vejam como esta o meu codigo:
import java.awt.*;
import javax.swing.*;
public class Main
{
private Frame f;
private Panel p,p2,p3,p4;
private String title = "";
private Integer height = new Integer(400);
private Integer width = new Integer(400);
private JLabel vermelho;
private JLabel azul;
private JLabel amarelo;
private JLabel verde;
public Main() {
f = new Frame();
p = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
vermelho = new JLabel("Este é o segundo painel");
azul = new JLabel("Este é o primeiro painel");
amarelo = new JLabel("Este é o quarto painel");
verde = new JLabel("Este é o terceiro painel");
f.setSize(width.intValue(), height.intValue());
}
public Main(String title) {
this();
f.setTitle(title);
this.title = title;
}
public Main(String title, Integer width, Integer height) {
this(title);
this.width = width;
this.height = height;
}
public void launchFrame() {
f.setLayout(null); //override default layout manager
p.setBackground(Color.blue);
p2.setBackground(Color.green);
p3.setBackground(Color.red);
p4.setBackground(Color.yellow);
f.setBackground(Color.white);
p.setSize(width, height );
p2.setSize(width, height );
p3.setSize(width, height );
p4.setSize(width, height );
vermelho.setBackground(Color.black);
vermelho.setLocation(60,60);
azul.setBackground(Color.black);
azul.setLocation(150,150);
amarelo.setBackground(Color.black);
amarelo.setLocation(60,60);
verde.setBackground(Color.black);
verde.setLocation(60,60);
p.setLayout(new GridBagLayout());
p.add(azul);
p2.setLayout(new GridBagLayout());
p2.add(vermelho);
p3.setLayout(new GridBagLayout());
p3.add(verde);
p4.setLayout(new GridBagLayout());
p4.add(amarelo);
//posiciona
p.setLocation(0,0);
p2.setLocation(0,height/2);
p3.setLocation(width/2,0);
p4.setLocation(width/2,height/2);
// p.setSize(100, 100);
f.add(p);
f.setVisible(true);
}
public static void main(String args[]) {
Main window = new Main("Frame com painel");
window.launchFrame();
}
}
Dá uma pesquisada em Mouselistener
você terá que implementar esse interface que será responsável pelos eventos do mouse.
Bom dia
Então cara tentei algo como : public void actionPerformed(ActionEvent e){…} para ativar no click mas não consegui fazer. VocÊ tem como montar um exemplo ?
Infelizmente não tenho como fazer agora.
Mas tem um exemplo nesse link: http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
Acredito que é bastante simples o entendimento caso não consigo me fale que de tarde faço um exemplo e te envio.
no seu panel vc pode adicionar as linhas:
jPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
// ações do panel
}
});
para poder fazer o evento de quando clicado em determinado panel, vc poder fazer as ações, tipo mostrar o 1º, 2º…
Velho valeu pelo link, to quase lá meu problema é que eu iniciei o frame vazio e o iniciar clico e ele carrega um painel, mas não consigo carregar os outros e na verdade precisava iniciar já com o primeiro painel carregado, olha como esta fiz um mouseclicked:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Main implements MouseListener
{
private Frame f;
private Panel p,p2,p3,p4,p5;
private String title = "";
private Integer height = new Integer(400);
private Integer width = new Integer(400);
private JLabel vermelho;
private JLabel azul;
private JLabel amarelo;
private JLabel verde;
private JLabel branco;
public Main() {
f = new Frame();
p = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
vermelho = new JLabel("Este é o segundo painel");
azul = new JLabel("Este é o primeiro painel");
amarelo = new JLabel("Este é o quarto painel");
verde = new JLabel("Este é o terceiro painel");
branco = new JLabel("Este é o quinto painel");
f.setSize(width.intValue(), height.intValue());
}
public Main(String title) {
this();
f.setTitle(title);
this.title = title;
}
public Main(String title, Integer width, Integer height) {
this(title);
this.width = width;
this.height = height;
}
public void launchFrame() {
f.setLayout(null); //override default layout manager
p.setBackground(Color.blue);
p2.setBackground(Color.green);
p3.setBackground(Color.red);
p4.setBackground(Color.yellow);
p5.setBackground(Color.white);
f.setBackground(Color.white);
p.setSize(width, height );
p2.setSize(width, height );
p3.setSize(width, height );
p4.setSize(width, height );
p5.setSize(width, height );
vermelho.setBackground(Color.black);
vermelho.setLocation(150,150);
azul.setBackground(Color.black);
azul.setLocation(150,150);
amarelo.setBackground(Color.black);
amarelo.setLocation(150,150);
verde.setBackground(Color.black);
verde.setLocation(150,1500);
branco.setBackground(Color.black);
branco.setLocation(150,150);
p.setLayout(new GridBagLayout());
p.add(azul);
p2.setLayout(new GridBagLayout());
p2.add(vermelho);
p3.setLayout(new GridBagLayout());
p3.add(verde);
p4.setLayout(new GridBagLayout());
p4.add(amarelo);
p4.setLayout(new GridBagLayout());
p4.add(branco);
//posiciona
p.setLocation(0,0);
p2.setLocation(0,0);
p3.setLocation(0,0);
p4.setLocation(0,0);
p5.setLocation(0,0);
f.add(p);
f.addMouseListener(this);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
mudarPainel();
}
void mudarPainel() {
f.add(p);
}
public static void main(String args[]) {
Main window = new Main("Frame com painel");
window.launchFrame();
}
@Override
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Consegui fazer, valeu
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Main implements MouseListener
{
private Frame f;
private Panel p,p2,p3,p4,p5;
private String title = "";
private Integer height = new Integer(400);
private Integer width = new Integer(400);
private JLabel vermelho;
private JLabel azul;
private JLabel amarelo;
private JLabel verde;
private JLabel branco;
public Main() {
f = new Frame();
p = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
vermelho = new JLabel("Este é o segundo painel");
azul = new JLabel("Este é o primeiro painel");
amarelo = new JLabel("Este é o quarto painel");
verde = new JLabel("Este é o terceiro painel");
branco = new JLabel("Este é o quinto painel");
f.setSize(width.intValue(), height.intValue());
}
public Main(String title) {
this();
f.setTitle(title);
this.title = title;
}
public Main(String title, Integer width, Integer height) {
this(title);
this.width = width;
this.height = height;
}
public void launchFrame() {
f.setLayout(null); //override default layout manager
p.setBackground(Color.blue);
p2.setBackground(Color.green);
p3.setBackground(Color.red);
p4.setBackground(Color.yellow);
p5.setBackground(Color.white);
f.setBackground(Color.white);
p.setSize(width, height );
p2.setSize(width, height );
p3.setSize(width, height );
p4.setSize(width, height );
p5.setSize(width, height );
vermelho.setBackground(Color.black);
vermelho.setLocation(150,150);
azul.setBackground(Color.black);
azul.setLocation(150,150);
amarelo.setBackground(Color.black);
amarelo.setLocation(150,150);
verde.setBackground(Color.black);
verde.setLocation(150,1500);
branco.setBackground(Color.black);
branco.setLocation(150,150);
p.setLayout(new GridBagLayout());
p.add(azul);
p2.setLayout(new GridBagLayout());
p2.add(vermelho);
p3.setLayout(new GridBagLayout());
p3.add(verde);
p4.setLayout(new GridBagLayout());
p4.add(amarelo);
p5.setLayout(new GridBagLayout());
p5.add(branco);
//posiciona
p.setLocation(0,0);
p2.setLocation(0,0);
p3.setLocation(0,0);
p4.setLocation(0,0);
p5.setLocation(0,0);
p.addMouseListener(this);
p2.addMouseListener(this);
p3.addMouseListener(this);
p4.addMouseListener(this);
p5.addMouseListener(this);
f.add(p);
f.add(p2);
f.add(p3);
f.add(p4);
f.add(p5);
f.addMouseListener(this);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
mudarPainel();
}
void mudarPainel() {
if(p.isVisible())
{
p.setVisible(false);
f.removeAll();
p2.setVisible(true);
f.add(p2);
}
else if(p2.isVisible())
{
p2.setVisible(false);
f.removeAll();
p3.setVisible(true);
f.add(p3);
}
else if(p3.isVisible())
{
p3.setVisible(false);
f.removeAll();
p4.setVisible(true);
f.add(p4);
}
else if(p4.isVisible())
{
p4.setVisible(false);
f.removeAll();
p5.setVisible(true);
f.add(p5);
}
else
{
p5.setVisible(false);
f.removeAll();
p.setVisible(true);
f.add(p);
}
}
public static void main(String args[]) {
Main window = new Main("Frame com painel");
window.launchFrame();
}
@Override
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
vc tem que colocar os listener nos panels…
e depois verificar qual foi o selecionado…
package dsa23dsa;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
public class Main implements MouseListener
{
private Frame f;
private Panel p,p2,p3,p4,p5;
private String title = "";
private Integer height = new Integer(400);
private Integer width = new Integer(400);
private JLabel vermelho;
private JLabel azul;
private JLabel amarelo;
private JLabel verde;
private JLabel branco;
public Main() {
f = new Frame();
p = new Panel();
p.addMouseListener(this);
p2 = new Panel();
p2.addMouseListener(this);
p3 = new Panel();
p3.addMouseListener(this);
p4 = new Panel();
p4.addMouseListener(this);
p5 = new Panel();
p5.addMouseListener(this);
vermelho = new JLabel("Este é o segundo painel");
azul = new JLabel("Este é o primeiro painel");
amarelo = new JLabel("Este é o quarto painel");
verde = new JLabel("Este é o terceiro painel");
branco = new JLabel("Este é o quinto painel");
f.setSize(width.intValue(), height.intValue());
}
public Main(String title) {
this();
f.setTitle(title);
this.title = title;
}
public Main(String title, Integer width, Integer height) {
this(title);
this.width = width;
this.height = height;
}
public void launchFrame() {
f.setLayout(null); //override default layout manager
p.setBackground(Color.blue);
p2.setBackground(Color.green);
p3.setBackground(Color.red);
p4.setBackground(Color.yellow);
p5.setBackground(Color.white);
f.setBackground(Color.white);
p.setSize(width, height );
p2.setSize(width, height );
p3.setSize(width, height );
p4.setSize(width, height );
p5.setSize(width, height );
vermelho.setBackground(Color.black);
vermelho.setLocation(150,150);
azul.setBackground(Color.black);
azul.setLocation(150,150);
amarelo.setBackground(Color.black);
amarelo.setLocation(150,150);
verde.setBackground(Color.black);
verde.setLocation(150,1500);
branco.setBackground(Color.black);
branco.setLocation(150,150);
p.setLayout(new GridBagLayout());
p.add(azul);
p2.setLayout(new GridBagLayout());
p2.add(vermelho);
p3.setLayout(new GridBagLayout());
p3.add(verde);
p4.setLayout(new GridBagLayout());
p4.add(amarelo);
p4.setLayout(new GridBagLayout());
p4.add(branco);
//posiciona
p.setLocation(0,0);
p2.setLocation(0,0);
p3.setLocation(0,0);
p4.setLocation(0,0);
p5.setLocation(0,0);
f.add(p);
f.setVisible(true);
}
void mudarPainel() {
f.add(p);
}
public static void main(String args[]) {
Main window = new Main("Frame com painel");
window.launchFrame();
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
if(e.getComponent().getName().equals("panel0")){
f.remove(p);
f.add(p2);
} else if(e.getComponent().getName().equals("panel1")){
f.remove(p2);
f.add(p3);
} else if(e.getComponent().getName().equals("panel2")){
f.remove(p3);
f.add(p4);
} else if(e.getComponent().getName().equals("panel3")){
f.remove(p4);
f.add(p5);
} else if(e.getComponent().getName().equals("panel4")){
f.remove(p5);
f.add(p);
}
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
}
haha!!
eu nem dei F5 na página, e vc já tinha resolvido…
bom pelo ficou a dica ae, e que bom que vc conseguiu…
Movido para o fórum de interface gráfica. Por favor, leia com atenção a descrição dos fóruns antes de postar.