Uploaded by mandoiumadalina97

1.Tema Java nr3

advertisement
Tema Java nr.3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class TemaFinala{
static int N = 3;
static JTextField tf;
static JButton array[][];
public static void main(String args[]){
JFrame frame = new JFrame();
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
JSplitPane splitPane = new JSplitPane();//imparte frame
array = new JButton[N][N];
for(int i=0;i<N;i++){//adaugam butoane
for(int j=0;j<N;j++){
JButton b = new JButton();
b.setName(""+i+j);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JButton b = (JButton) e.getSource();
int x = Integer.parseInt(b.getName().charAt(0)+"");
int y = Integer.parseInt(b.getName().charAt(1)+"");
String txt = b.getText();
if(txt.equals("X")){
array[x][y] = null;
b.setText("");
}else{
array[x][y] = b;
b.setText("X");
}
}
});
topPanel.add(b);
}
}
topPanel.setLayout(new GridLayout(N,N));
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerLocation(350);
splitPane.setTopComponent(topPanel);
splitPane.setBottomComponent(bottomPanel);
tf = new JTextField();
tf.setPreferredSize(new Dimension(390, 25));
JButton calcul = new JButton("Calcul");
calcul.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int i,j,s;
for(i=0;i<N;i++){
s=0;
for(j=0;j<N;j++)
if(array[i][j]!=null) s++;
if(s==3){
tf.setText("Exista o linie orizontala.");
return;
}
}
for(i=0;i<N;i++){
s=0;
for(j=0;j<N;j++){
if(array[j][i]!=null) s++;
}
if(s==3){
tf.setText("Exista o linie verticala.");
return;
tf.setText("Nu exista o linie verticala.");
}
}
}
});
calcul.setPreferredSize(new Dimension(150, 25));
JButton sterge = new JButton("Sterge");
sterge.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(array[i][j]!=null){
array[i][j].setText("");
array[i][j]=null;
}
}
}
tf.setText("");
}
});
sterge.setPreferredSize(new Dimension(150, 25));
bottomPanel.add(tf);
bottomPanel.add(calcul);
bottomPanel.add(sterge);
frame.add(splitPane);
frame.setSize(400, 500);
frame.setVisible(true);
}
}
Download