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));
}
}
}
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));
}
}
}