Porque nao aparecem os botoes no 1º painel?

1 resposta
P

Fiz umas experiencias mas continua a dar erro:...
se tenho de estudar mais, digam no problem ora

i
mport java.awt.*; 
  import javax.swing.*; 
  import javax.swing.border.*; 
 
  public class manGui extends JFrame implements ActionListener
  { 
  	// variaveis
  	private JPanel contentPane; 
  	private JPanel jPanel1; 
    	private JPanel jPanel2; 
  	private JPanel jPanel3; 
 
         JButton[][] _botoes = null;
  
    
  	public manGui() 
  	{ 
  		super(); 
  		initializeComponent(); 
  		  
  		this.setVisible(true); 
  	} 
 
  	private void initializeComponent() 
  	{ 
  		contentPane = (JPanel)this.getContentPane(); 
  		//----- 
  		jPanel1 = new JPanel(); 
  		//----- 
  		jPanel2 = new JPanel(); 
  		//----- 
  		jPanel3 = new JPanel(); 
  		//----- 
   
  	
  		// contentPane 
  		contentPane.setLayout(null); 
  		addComponent(contentPane, jPanel1, 28,75,319,356); 
  		addComponent(contentPane, jPanel2, 368,75,297,356); 
  		addComponent(contentPane, jPanel3, 30,465,636,66); 
 
  		// jPanel1 
   		jPanel1.setLayout(null); 
  		jPanel1.setBorder(new TitledBorder("Jogador")); 
 
 //---------------
  		_botoes = new JButton[10][10];
  		for (int i = 0; i< 10; i++){
  			for (int j = 0; j< 10; j++){
  				JButton b = new JButton();
  				b.addActionListener(this);
  				_botoes[i][j] = b;
  				JPanel1.add(b,i,j);
  				
  			}
                    return JPanel1;
  		}
  		
 //---------------
 
 
  		 
  		// jPanel2 
   		jPanel2.setLayout(null); 
  		jPanel2.setBorder(new TitledBorder("Computador")); 
 
  		// jPanel3 
   		jPanel3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 
 
  		// manGui 
  		this.setTitle("IGui"); 
  		this.setLocation(new Point(0, 0)); 
  		this.setSize(new Dimension(722, 577)); 
  	} 
   
  	// Add Componentes 
  	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
  	{ 
  		c.setBounds(x,y,width,height); 
  		container.add(c); 
  	} 
   
 //Main
  	public static void main(String[] args) 
  	{ 
  		JFrame.setDefaultLookAndFeelDecorated(true); 
  		JDialog.setDefaultLookAndFeelDecorated(true); 
  		try 
  		{ 
  			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
  		} 
  		catch (Exception ex) 
  		{ 
  			System.out.println("Nao abre L&F: "); 
  			System.out.println(ex); 
  		} 
  		new manGui(); 
  	} 
 public void actionPerformed(ActionEvent e) {
  		// OBS: o botão 0,0 (Ref) é o do cando inferior esquerdo
  		for (int i = 0; i< 10; i++){
  			for (int j = 0; j< 10; j++){
  				if (e.getSource() == _botoes[i][j]){
  					JOptionPane.showMessageDialog(this, "botão: " + i + "," + j);
  				}
  			}
  		}
  	}
 
  }

1 Resposta

hvidal

Olá Paulo,

Esse seu código está uma bagunça.

1 - Estão faltando imports;

2 - Você está tentando retornar um JPanel no meio de um método void;

3 - Você está adicionando o contentPane do Frame dentro de paineis que não são adicionados a lugar nenhum. Você deve fazer o contrário: adicionar os paineis no contentPane.

Pegue mais exemplos com telas e botões para você ganhar mais intimidade com a API do Swing.

Um bom link é:
http://java.sun.com/docs/books/tutorial/uiswing/components/components.html

Grande abraço,
Hugo.

Criado 9 de novembro de 2006
Ultima resposta 9 de nov. de 2006
Respostas 1
Participantes 2