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:
importjava.awt.*;importjavax.swing.*;publicclassMain{privateFramef;privatePanelp,p2,p3,p4;privateStringtitle="";privateIntegerheight=newInteger(400);privateIntegerwidth=newInteger(400);privateJLabelvermelho;privateJLabelazul;privateJLabelamarelo;privateJLabelverde;publicMain(){f=newFrame();p=newPanel();p2=newPanel();p3=newPanel();p4=newPanel();vermelho=newJLabel("Este é o segundo painel");azul=newJLabel("Este é o primeiro painel");amarelo=newJLabel("Este é o quarto painel");verde=newJLabel("Este é o terceiro painel");f.setSize(width.intValue(),height.intValue());}publicMain(Stringtitle){this();f.setTitle(title);this.title=title;}publicMain(Stringtitle,Integerwidth,Integerheight){this(title);this.width=width;this.height=height;}publicvoidlaunchFrame(){f.setLayout(null);//override default layout managerp.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(newGridBagLayout());p.add(azul);p2.setLayout(newGridBagLayout());p2.add(vermelho);p3.setLayout(newGridBagLayout());p3.add(verde);p4.setLayout(newGridBagLayout());p4.add(amarelo);//posicionap.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);}publicstaticvoidmain(Stringargs[]){Mainwindow=newMain("Frame com painel");window.launchFrame();}}
Dá uma pesquisada em Mouselistener
você terá que implementar esse interface que será responsável pelos eventos do mouse.
A
alexandreccarmo
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 ?
para poder fazer o evento de quando clicado em determinado panel, vc poder fazer as ações, tipo mostrar o 1º, 2º…
A
alexandreccarmo
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:
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.*;publicclassMainimplementsMouseListener{privateFramef;privatePanelp,p2,p3,p4,p5;privateStringtitle="";privateIntegerheight=newInteger(400);privateIntegerwidth=newInteger(400);privateJLabelvermelho;privateJLabelazul;privateJLabelamarelo;privateJLabelverde;privateJLabelbranco;publicMain(){f=newFrame();p=newPanel();p2=newPanel();p3=newPanel();p4=newPanel();p5=newPanel();vermelho=newJLabel("Este é o segundo painel");azul=newJLabel("Este é o primeiro painel");amarelo=newJLabel("Este é o quarto painel");verde=newJLabel("Este é o terceiro painel");branco=newJLabel("Este é o quinto painel");f.setSize(width.intValue(),height.intValue());}publicMain(Stringtitle){this();f.setTitle(title);this.title=title;}publicMain(Stringtitle,Integerwidth,Integerheight){this(title);this.width=width;this.height=height;}publicvoidlaunchFrame(){f.setLayout(null);//override default layout managerp.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(newGridBagLayout());p.add(azul);p2.setLayout(newGridBagLayout());p2.add(vermelho);p3.setLayout(newGridBagLayout());p3.add(verde);p4.setLayout(newGridBagLayout());p4.add(amarelo);p4.setLayout(newGridBagLayout());p4.add(branco);//posicionap.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);}publicvoidmouseClicked(MouseEvente){mudarPainel();}voidmudarPainel(){f.add(p);}publicstaticvoidmain(Stringargs[]){Mainwindow=newMain("Frame com painel");window.launchFrame();}@OverridepublicvoidmousePressed(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}@OverridepublicvoidmouseReleased(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}@OverridepublicvoidmouseEntered(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}@OverridepublicvoidmouseExited(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}}
A
alexandreccarmo
Consegui fazer, valeu
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.*;publicclassMainimplementsMouseListener{privateFramef;privatePanelp,p2,p3,p4,p5;privateStringtitle="";privateIntegerheight=newInteger(400);privateIntegerwidth=newInteger(400);privateJLabelvermelho;privateJLabelazul;privateJLabelamarelo;privateJLabelverde;privateJLabelbranco;publicMain(){f=newFrame();p=newPanel();p2=newPanel();p3=newPanel();p4=newPanel();p5=newPanel();vermelho=newJLabel("Este é o segundo painel");azul=newJLabel("Este é o primeiro painel");amarelo=newJLabel("Este é o quarto painel");verde=newJLabel("Este é o terceiro painel");branco=newJLabel("Este é o quinto painel");f.setSize(width.intValue(),height.intValue());}publicMain(Stringtitle){this();f.setTitle(title);this.title=title;}publicMain(Stringtitle,Integerwidth,Integerheight){this(title);this.width=width;this.height=height;}publicvoidlaunchFrame(){f.setLayout(null);//override default layout managerp.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(newGridBagLayout());p.add(azul);p2.setLayout(newGridBagLayout());p2.add(vermelho);p3.setLayout(newGridBagLayout());p3.add(verde);p4.setLayout(newGridBagLayout());p4.add(amarelo);p5.setLayout(newGridBagLayout());p5.add(branco);//posicionap.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);}publicvoidmouseClicked(MouseEvente){mudarPainel();}voidmudarPainel(){if(p.isVisible()){p.setVisible(false);f.removeAll();p2.setVisible(true);f.add(p2);}elseif(p2.isVisible()){p2.setVisible(false);f.removeAll();p3.setVisible(true);f.add(p3);}elseif(p3.isVisible()){p3.setVisible(false);f.removeAll();p4.setVisible(true);f.add(p4);}elseif(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);}}publicstaticvoidmain(Stringargs[]){Mainwindow=newMain("Frame com painel");window.launchFrame();}@OverridepublicvoidmousePressed(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}@OverridepublicvoidmouseReleased(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}@OverridepublicvoidmouseEntered(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}@OverridepublicvoidmouseExited(MouseEvente){thrownewUnsupportedOperationException("Not supported yet.");}}
JonaTHANNM
vc tem que colocar os listener nos panels…
e depois verificar qual foi o selecionado…
packagedsa23dsa;importjava.awt.Color;importjava.awt.Frame;importjava.awt.GridBagLayout;importjava.awt.Panel;importjava.awt.event.MouseListener;importjavax.swing.JLabel;publicclassMainimplementsMouseListener{privateFramef;privatePanelp,p2,p3,p4,p5;privateStringtitle="";privateIntegerheight=newInteger(400);privateIntegerwidth=newInteger(400);privateJLabelvermelho;privateJLabelazul;privateJLabelamarelo;privateJLabelverde;privateJLabelbranco;publicMain(){f=newFrame();p=newPanel();p.addMouseListener(this);p2=newPanel();p2.addMouseListener(this);p3=newPanel();p3.addMouseListener(this);p4=newPanel();p4.addMouseListener(this);p5=newPanel();p5.addMouseListener(this);vermelho=newJLabel("Este é o segundo painel");azul=newJLabel("Este é o primeiro painel");amarelo=newJLabel("Este é o quarto painel");verde=newJLabel("Este é o terceiro painel");branco=newJLabel("Este é o quinto painel");f.setSize(width.intValue(),height.intValue());}publicMain(Stringtitle){this();f.setTitle(title);this.title=title;}publicMain(Stringtitle,Integerwidth,Integerheight){this(title);this.width=width;this.height=height;}publicvoidlaunchFrame(){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(newGridBagLayout());p.add(azul);p2.setLayout(newGridBagLayout());p2.add(vermelho);p3.setLayout(newGridBagLayout());p3.add(verde);p4.setLayout(newGridBagLayout());p4.add(amarelo);p4.setLayout(newGridBagLayout());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);}voidmudarPainel(){f.add(p);}publicstaticvoidmain(Stringargs[]){Mainwindow=newMain("Frame com painel");window.launchFrame();}@OverridepublicvoidmouseClicked(java.awt.event.MouseEvente){if(e.getComponent().getName().equals("panel0")){f.remove(p);f.add(p2);}elseif(e.getComponent().getName().equals("panel1")){f.remove(p2);f.add(p3);}elseif(e.getComponent().getName().equals("panel2")){f.remove(p3);f.add(p4);}elseif(e.getComponent().getName().equals("panel3")){f.remove(p4);f.add(p5);}elseif(e.getComponent().getName().equals("panel4")){f.remove(p5);f.add(p);}}@OverridepublicvoidmouseEntered(java.awt.event.MouseEvente){// TODO Auto-generated method stub}@OverridepublicvoidmouseExited(java.awt.event.MouseEvente){// TODO Auto-generated method stub}@OverridepublicvoidmousePressed(java.awt.event.MouseEvente){// TODO Auto-generated method stub}@OverridepublicvoidmouseReleased(java.awt.event.MouseEvente){// TODO Auto-generated method stub}}
JonaTHANNM
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…
ViniGodoy
Movido para o fórum de interface gráfica. Por favor, leia com atenção a descrição dos fóruns antes de postar.