PDA

View Full Version : Layout JAVA


Pro7on
04-06-2007, 16:03
Sto cercando di fare battaglia navale in java(sapete xcaso se c'è in giro un source) cmq chi mi spiega xche il button incrementa me lo mette a sinistra del titotlo??

p.s. vi conviene compilarlo:D

Thx ;)




package battaglianavale;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author edpsam
*/
public class Main extends JFrame{

private JTextField txt;
private JButton incrementa;
private int valore = 0;


/** Creates a new instance of Main */
public Main() {

setTitle("Pattaglia Navale");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().setLayout(new BorderLayout());

GridLayout lay = new GridLayout(2,1);

JPanel Psinistra = new JPanel();
GridLayout l_sin = new GridLayout(1,1,5,5);
Psinistra.setLayout(l_sin);

JPanel Pdestra = new JPanel();
GridLayout l_des = new GridLayout(1,1,5,5);
Pdestra.setLayout(l_des);


//--------------------------DESTRA
JPanel pan = new JPanel();
GridLayout l_pan = new GridLayout(1,3,5,5);
pan.setLayout(l_pan);

JPanel pannello = new JPanel();


JPanel pannello2 = new JPanel();



JLabel titolo = new JLabel("Titolo");
titolo.setText("<html><font size=3><b><u>TITOLO</u></b></font></html>");
pannello.add(titolo);

incrementa = new JButton("incrementa");
incrementa.addActionListener(new IncrementaEvento());
txt = new JTextField(String.valueOf(valore));
pannello2.add(incrementa);
pannello2.add(txt);

pan.add(pannello);
pan.add(pannello2);

Pdestra.add(pan);
//--------------------------Sinistra
//Creo Matrice
JPanel matrice = new JPanel();
GridLayout mat = new GridLayout(10,10);
matrice.setLayout(mat);

JButton[][] btn = new JButton[10][10];

for(int y=0;y<10;y++){
for(int x=0;x<10;x++){

btn[x][y] = new JButton(x+"_"+y);

}
}

//Aggiorno i componenti al JFRAME
for(int y=0;y<10;y++){
for(int x=0;x<10;x++){

matrice.add(btn[x][y]);
}
}
//......
//----------------------------------Componenti

//add ai pannelli
Pdestra.add(pannello);
Psinistra.add(matrice);

getContentPane().add(Psinistra, BorderLayout.WEST);
getContentPane().add(Pdestra, BorderLayout.EAST);


pack();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Main().setVisible(true);
}

private class IncrementaEvento implements ActionListener{

public void actionPerformed(ActionEvent e){
valore++;
txt.setText(String.valueOf(valore));
}
}



}

^TiGeRShArK^
04-06-2007, 16:04
ehmmm...
usa il tag "code" anzikè "quote" perchè senza indentazione non si capisce una cippa :p

Pro7on
05-06-2007, 07:55
ehmmm...
usa il tag "code" anzikè "quote" perchè senza indentazione non si capisce una cippa :p


a scusa nn sapevo c fosse :D

Pro7on
05-06-2007, 12:29
a scusa nn sapevo c fosse :D

:help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: Plase

Pro7on
05-06-2007, 13:32
package battaglianavale;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author edpsam
*/
public class Main extends JFrame{

private JTextField txt;
private JButton incrementa;
private int valore = 0;


/** Creates a new instance of Main */
public Main() {

setTitle("Battaglia Navale");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().setLayout(new BorderLayout());

JPanel scheletro = new JPanel();
GridLayout l_scheletro = new GridLayout(1,2,5,5);
scheletro.setLayout(l_scheletro);

JPanel Psinistra = new JPanel();
GridLayout l_sin = new GridLayout(1,1,5,5);
Psinistra.setLayout(l_sin);

JPanel Pdestra = new JPanel();
GridLayout l_des = new GridLayout(3,1,5,5);
Pdestra.setLayout(l_des);


//--------------------------DESTRA


JLabel titolo = new JLabel("Titolo");
titolo.setText("<html><font size=3><b><u>TITOLO</u></b></font></html>");


incrementa = new JButton("incrementa");
incrementa.addActionListener(new IncrementaEvento());
txt = new JTextField(String.valueOf(valore));

Pdestra.add(titolo, BorderLayout.NORTH);
Pdestra.add(incrementa, BorderLayout.CENTER);
Pdestra.add(txt, BorderLayout.SOUTH);


//--------------------------Sinistra
//Creo Matrice
JPanel matrice = new JPanel();
GridLayout mat = new GridLayout(10,10);
matrice.setLayout(mat);

JButton[][] btn = new JButton[10][10];

for(int y=0;y<10;y++){
for(int x=0;x<10;x++){

btn[x][y] = new JButton(x+"_"+y);

}
}

//Aggiorno i componenti al JFRAME
for(int y=0;y<10;y++){
for(int x=0;x<10;x++){

matrice.add(btn[x][y]);
}
}
//......
//----------------------------------Componenti

//add ai pannelli

scheletro.add(Psinistra);
scheletro.add(Pdestra);
Psinistra.add(matrice);

getContentPane().add(Psinistra, BorderLayout.WEST);
getContentPane().add(Pdestra, BorderLayout.EAST);


pack();
}
}



ho fatto confusione con i pannelli.....:D