Alquem me ajuda a criar um programa em q quando eu clico em um botao abra uma janela

8 respostas
Rafael_N

por favorrr é muito importante para minha vida depende disso :shock: :shock: :cry: :cry:

8 Respostas

ViniGodoy

Poste aí o que você já fez e onde tem dúvida.

L

meio complicado assim!!!

pedir desse jeito…quase certeza que não vão dar em mãos prontas!!!

Anime

Aff,dramático… :shock:

Explique melhor…

eliangela

Primeiro: vc adiciona um listener no botão que vc quer que execute a ação;
Segundo: vc coloca o código para abrir a Janela.

ex.:

meuJButton.addActionListener(new ActionListener() { //esse é o listener
    public void actionPerformed(ActionEvent evt) {
        new MinhaJanela().setVisible(true); // essa é sua janela abrindo
    }
});

http://www.guj.com.br/posts/list/194455.java
http://forum.clubedohardware.com.br/abrir-janela-botao/714882?s=85e9384a77864edabe908ad389bceb80&

No site da SUN / ORACLE tem muitos tutoriais bons sobre como usar o Swing. Dá uma olhada melhor nesse tutorial sobre Listeners.
http://download.oracle.com/javase/tutorial/uiswing/events/index.html

Rafael_N
/****************************************************************/
/*                      Filosofia	                            */
/*                                                              */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Summary description for Filosofia
 *
 */
public class Filosofia extends JFrame
{
	// Variables declaration
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton aristoteles;
	private JButton platao;
	private JButton socrates;
	private JButton arquimedes;
	private JPanel contentPane;
	// End of variables declaration


	public Filosofia()
	{
		super();
		initializeComponent();
		//
		// TODO: Add any constructor code after initializeComponent call
		//

		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always regenerated
	 * by the Windows Form Designer. Otherwise, retrieving design might not work properly.
	 * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
	 * to retrieve your design properly in future, before revising this method.
	 */
	private void initializeComponent()
	{
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		aristoteles = new JButton();
		platao = new JButton();
		socrates = new JButton();
		arquimedes = new JButton();
		contentPane = (JPanel)this.getContentPane();

		//
		// jLabel1
		//
		jLabel1.setBackground(new Color(255, 255, 255));
		jLabel1.setIcon(new ImageIcon("F:\\Meu Projeto\\Imagens\\Filosofos.jpg"));
		jLabel1.setText("jLabel1");
		jLabel1.addKeyListener(new KeyAdapter() {

		});
		//
		// jLabel2
		//
		jLabel2.setIcon(new ImageIcon("F:\\Meu Projeto\\Imagens\\imagem.JPG"));
		//
		// aristoteles
		//
		aristoteles.setText("Aristóteles");
		aristoteles.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				aristoteles_actionPerformed(e);
			}

		});
		//
		// platao
		//
		platao.setText("Platão");
		platao.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				platao_actionPerformed(e);
			}

		});
		//
		// socrates
		//
		socrates.setText("Sócrates");
		socrates.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				socrates_actionPerformed(e);
			}

		});
		//
		// arquimedes
		//
		arquimedes.setText("Arquimédes");
		arquimedes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				arquimedes_actionPerformed(e);
			}

		});
		//
		// contentPane
		//
		contentPane.setLayout(null);
		contentPane.setBackground(new Color(255, 255, 255));
		addComponent(contentPane, jLabel1, 90,77,703,498);
		addComponent(contentPane, jLabel2, 194,-6,469,85);
		addComponent(contentPane, aristoteles, 58,593,133,28);
		addComponent(contentPane, platao, 265,593,133,28);
		addComponent(contentPane, socrates, 482,593,133,28);
		addComponent(contentPane, arquimedes, 673,593,133,28);
		//
		// Filosofia
		//
		this.setTitle("Mini Enciclopédia");
		this.setLocation(new Point(0, 0));
		this.setSize(new Dimension(884, 674));
		this.setResizable(false);
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}


	//
	// TODO: Add any method code to meet your needs in the following area
	//


//============================= Testing ================================//
//=                                                                    =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it.                            =//
//======================================================================//
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception ex)
		{
			System.out.println("Failed loading L&F: ");
			System.out.println(ex);
		}
		new Filosofia();
	}
//= End of Testing =


}
Agora eu queria anexar uma janela ao botão aristiteles

vc odem me ajudar

eliangela

Vc não anexa uma Janela a um botão, vc adiciona um listener (ouvinte) ao botão que vai pedir pra abrir uma janela quando vc clicar nesse botão.

dentro do método aristoteles_actionPerformed(e); vc coloca um código pra chamar a janela desejada, como eu coloquei na resposta acima.

MinhaJanela m = new MinhaJanela(); m.setVisible(true);

Espero ter ajudado.
Até mais.

ViniGodoy

Se você tiver uma janela chamada “Aristoteles”, faça o seguinte:
Na linha 73, troque o código:

Por:

O mesmo pode ser feito para as outras janelas.

Rafael_N

Cara vc é um genio :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:
deu certo
valeu

Criado 20 de setembro de 2010
Ultima resposta 20 de set. de 2010
Respostas 8
Participantes 5