import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
public class Provincia2 {
int punti=0;
JFrame provincia1=new JFrame("GiocoProvince");
String[] regioni=new String[100];
int[] numeroProvince=new int[100];
Scanner in;
JLabel l1=new JLabel("Regione");
JLabel l2=new JLabel("Province");
JLabel l3=new JLabel("Risultato");
JLabel l4=new JLabel("Risposta_Corretta");
JLabel l5=new JLabel("Punteggio_Finale");
JTextField t1=new JTextField();
JPanel bottoni=new JPanel();
Pannello p=new Pannello();
JTextField t2=new JTextField();
JButton b1=new JButton("Avanti");
JTextField t3=new JTextField();
JButton b2=new JButton("Invio");
JTextField t4=new JTextField();
JTextField t5=new JTextField();
int risposta=0;
int i=0;
public void caricaRegioniNumeroProvince() throws IOException{
String letta;
in=new Scanner(new FileReader("province.txt"));
int letti=1;
int i=0;
int j=0;
while ( in.hasNext()){
letta=in.next();
if (letti%2!=0) {
regioni[i]=letta;
i++;
}else {
numeroProvince[j]=Integer.parseInt(letta);
j++;
}
letti++;
}
}
public void azzeraArray(){
for (int i=0; i
}
for (int i=0; i
}
}
public void creaForm1(){
provincia1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
provincia1.setSize(800,200);
p.setLayout(new GridLayout(2,5));
bottoni.setLayout(new GridLayout(1,2));
bottoni.add(b1);
bottoni.add(b2);
p.add(l1);
p.add(l2);
p.add(l3);
p.add(l4);
p.add(l5);
p.add(t1);
p.add(t2);
p.add(t3);
p.add(t4);
p.add(t5);
provincia1.add(p,BorderLayout.NORTH);
provincia1.add(bottoni,BorderLayout.SOUTH);
b1.addActionListener(new Avanti());
provincia1.setVisible(true);
b2.addActionListener(new Invio());
}
public class Pannello extends JPanel{
public void paintComponent(Graphics g){
}
}
public class Avanti implements ActionListener{
public void actionPerformed(ActionEvent event){
t1.setText(regioni[i]);
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
i++;
if ((i>=regioni.length)(regioni[i]==null)){
i=0;
String pf=Integer.toString(punti);
t5.setText(pf);
}
}
}
public static void main(String[] args) {
Provincia2 g=new Provincia2();
g.creaForm1();
g.azzeraArray();
try {
g.caricaRegioniNumeroProvince();
}
catch (IOException e){
System.out.println(e);
System.out.printf("Correggi il problema e riprova\n");
}
catch (NoSuchElementException e){
System.out.println(e);
System.out.printf("end of file raggiunto\n");
}
}
public class Invio implements ActionListener{
public void actionPerformed(ActionEvent event){
risposta=Integer.parseInt(t2.getText());
if (risposta==numeroProvince[i-1]){
t3.setText("bravo hai preso 2 punti");
punti+=2;
} else {
t3.setText("risposta errata");
String np=Integer.toString(numeroProvince[i-1]);
t4.setText(np);
}
}
}
}
Nessun commento:
Posta un commento