Movendo os formulários pela tela

3 respostas
S

Bem, criei o seguinte código :

package Interface;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class teste_Interface extends JFrame implements ActionListener
{
	
	private static teste_Interface janela;
	private JPanel pane;
	private JButton botao;
	
	public teste_Interface(String title)
	{
		setTitle(title);
	}
	
	public void MontaJanela(int width, int height, int x, int y)
	{
		pane = new JPanel();
		botao = new JButton("Click me... if you can!");
		botao.setLocation(new Point(80, 300));
		botao.addActionListener
		(
				new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent e)
					{
						MsgBox("Bem feito feito");
					}
				}
		);
		
		pane.add(botao);
		add(pane);
		setSize(width, height);
		setLocation(x, y);
		setVisible(true);
	}
	
	public void MsgBox(String text)
	{
		JOptionPane.showMessageDialog(this, text, "Alerta", 
				JOptionPane.INFORMATION_MESSAGE);
	}
	
	public static void main(String arg[])
	{	
		janela = new teste_Interface("My window");
		janela.MontaJanela(480, 640, 40, 40);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
	}
}

o que eu quero fazer é colocar o botão em um outro local da tela, mas eu não consigo fazer isso utilizando o método setLocation, consegui fazer isso apenas quando coloquei ele dentro do ActionListener, mas ae só quando o suário clicar nele… queria saber como faço isso antes da janela aparecer?

se alguém pudesse me ajudar eu agradeceria…

3 Respostas

javax.swing.Daniel

Tem o método setBounds( posicaoX, posicaoY, width, height ).

JButton botao = new JButton(" Me aperte forte ");

botao.setBounds(0, 0, 150, 20);
S

Não, não funcionou

ViniGodoy

O setBounds só funciona se você usar setLayout(null).

Entretanto, o ideal mesmo é aprender como usar os gerenciadores de layout:
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Criado 13 de julho de 2012
Ultima resposta 14 de jul. de 2012
Respostas 3
Participantes 3