Sono riuscito a creare il save e il load che funzionano perfettamente però c'è ancora un problema. Io voglio tenere conto di solo 3 valori (ovvero le prime tre righe). Per fare ciò ho bisogno di qualcosa per pulire il mio file (Altrimenti ogni volta che si clicca salva i contenuti verranno aggiunti e quindi non sarà possibile leggerli). La cancellazione ovviamente dovrà avvenire quando clicco il bottone SALVA.
L'evento del tasto salva sarà quindi:
1) Prendo i primi 3 valori dal file
2) Cancello il contenuto del file
3) Aggiorno i miei valori
4) Li rimetto nel file
Il problema sorge appunto, come detto prima, sul punto2
Qualche suggerimento?
Vi ringrazio
(Vi lascio qui sotto il codice)
L'evento del tasto salva sarà quindi:
1) Prendo i primi 3 valori dal file
2) Cancello il contenuto del file
3) Aggiorno i miei valori
4) Li rimetto nel file
Il problema sorge appunto, come detto prima, sul punto2
Qualche suggerimento?
Vi ringrazio
(Vi lascio qui sotto il codice)
Java:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class GUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
int Win;
int Kill;
int Death;
double K_D;
int contWin=0;
int contKill=0;
int contDeath=0;
String S_Nome = "✉PostmanPat✉";
String S_Rateo;
String S_Kill;
String S_Death;
String S_Win;
//Main
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
GUI frame = new GUI();
frame.setVisible(true);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void SaveToFile() {
try {
JOptionPane.showMessageDialog(null,"I dati sono stati inseriti correttamente.");
FileWriter writer = new FileWriter("DarwinProject.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
String temp_Kill;
String temp_Death;
String temp_Win;
FileReader reader = new FileReader("DarwinProject.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
temp_Kill = bufferedReader.readLine();
temp_Death = bufferedReader.readLine();
temp_Win = bufferedReader.readLine();
int x = Integer.parseInt(temp_Kill);
int y = Integer.parseInt(temp_Death);
int z = Integer.parseInt(temp_Win);
x = x+Kill;
y = y+Death;
z = z+Win;
S_Kill = Integer.toString(x);
S_Death = Integer.toString(y);
S_Win = Integer.toString(z);
bufferedWriter.write(S_Kill);
bufferedWriter.newLine(); // scrive una nuova linea
bufferedWriter.write(S_Death);
bufferedWriter.newLine(); // scrive una nuova linea
bufferedWriter.write(S_Win);
bufferedWriter.newLine();
bufferedWriter.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void K_D_Rateo(String a, String b)
{
Double t1 = Double.parseDouble(a);
Double t2 = Double.parseDouble(b);
K_D = (t1+t2)/2; // Kill Death rateo
S_Rateo = Double.toString(K_D);
}
public void LoadFromFile()
{
try (FileReader reader = new FileReader("DarwinProject.txt"))
{
BufferedReader bufferedReader = new BufferedReader(reader);
String a = bufferedReader.readLine();
String b = bufferedReader.readLine();
String c = bufferedReader.readLine();
K_D_Rateo(a,b);
textField.setText(S_Nome);
textField_1.setText(S_Rateo);
textField_2.setText(c);
} catch (IOException e)
{
e.printStackTrace();
}
}
//GUI
public GUI() {
super("Darwin Project Stats Tracker");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1920, 1080);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
Image Background = new ImageIcon(this.getClass().getResource("/Background.png")).getImage();
Image Layout = new ImageIcon(this.getClass().getResource("/Layout12.png")).getImage();
textField = new JTextField();
textField.setEditable(false);
textField.setBounds(463, 480, 381, 38);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setColumns(10);
textField_1.setBounds(463, 560, 381, 38);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setEditable(false);
textField_2.setColumns(10);
textField_2.setBounds(463, 642, 381, 38);
contentPane.add(textField_2);
JLabel lblNewLabel_0 = new JLabel("");
lblNewLabel_0.setForeground(Color.WHITE);
lblNewLabel_0.setFont(new Font("Microsoft YaHei UI Light", Font.BOLD, 21));
lblNewLabel_0.setBounds(673, 364, 45, 30);
contentPane.add(lblNewLabel_0);
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setForeground(Color.WHITE);
lblNewLabel_1.setFont(new Font("Microsoft YaHei UI Light", Font.BOLD, 21));
lblNewLabel_1.setBounds(343, 416, 38, 30);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("");
lblNewLabel_2.setForeground(Color.WHITE);
lblNewLabel_2.setFont(new Font("Microsoft YaHei UI Light", Font.BOLD, 21));
lblNewLabel_2.setBounds(343, 364, 45, 30);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_9 = new JLabel("");
lblNewLabel_9.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
SaveToFile();
lblNewLabel_1.setText("");
lblNewLabel_2.setText("");
lblNewLabel_0.setText("");
}
});
lblNewLabel_9.setBounds(614, 691, 293, 71);
contentPane.add(lblNewLabel_9);
JLabel lblNewLabel_8 = new JLabel("");
lblNewLabel_8.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
LoadFromFile();
}
});
lblNewLabel_8.setBounds(284, 691, 293, 71);
contentPane.add(lblNewLabel_8);
JLabel lblNewLabel_7 = new JLabel("");
lblNewLabel_7.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
Win+=1;
contWin+=1;
lblNewLabel_0.setText("x"+contWin);
}
});
lblNewLabel_7.setBounds(614, 344, 293, 71);
contentPane.add(lblNewLabel_7);
JLabel lblNewLabel_6 = new JLabel("");
lblNewLabel_6.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Death+=1;
contDeath+=1;
lblNewLabel_1.setText("x"+contDeath);
}
});
lblNewLabel_6.setBounds(284, 395, 293, 71);
contentPane.add(lblNewLabel_6);
JLabel lblNewLabel_5 = new JLabel("");
lblNewLabel_5.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Kill+=1;
contKill+=1;
lblNewLabel_2.setText("x"+contKill);
}
});
lblNewLabel_5.setBounds(284, 344, 293, 71);
contentPane.add(lblNewLabel_5);
JLabel lblNewLabel_3 = new JLabel("");
lblNewLabel_3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
}
});
lblNewLabel_3.setBounds(451, 745, 293, 71);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("");
lblNewLabel_4.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
int scelta;
scelta = JOptionPane.showConfirmDialog(null, "Sei sicuro/a di voler continuare?", "Conferma", JOptionPane.INFORMATION_MESSAGE);
if(scelta == JOptionPane.YES_OPTION)
{
contKill=0;
contDeath=0;
contWin=0;
}
else if(scelta == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null,"Reinserire i dati.");
lblNewLabel_1.setText("");
lblNewLabel_2.setText("");
lblNewLabel_0.setText("");
contKill=0;
contDeath=0;
contWin=0;
}
}
});
lblNewLabel_4.setBounds(614, 395, 293, 71);
contentPane.add(lblNewLabel_4);
JLabel label = new JLabel("");
label.setBounds(194, 304, 795, 524);
contentPane.add(label);
label.setIcon(new ImageIcon(Layout));
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBackground(Color.WHITE);
lblNewLabel.setBounds(-8, -19, 1920, 1080);
contentPane.add(lblNewLabel);
lblNewLabel.setIcon(new ImageIcon(Background));
Image Kill = new ImageIcon(this.getClass().getResource("/Kill1234.png")).getImage();
lblNewLabel_5.setIcon(new ImageIcon(Kill));
Image Inserisci = new ImageIcon(this.getClass().getResource("/Inserisci123456.png")).getImage();
lblNewLabel_4.setIcon(new ImageIcon(Inserisci));
Image Visualizza = new ImageIcon(this.getClass().getResource("/Visualizza.png")).getImage();
lblNewLabel_3.setIcon(new ImageIcon(Visualizza));
Image Death = new ImageIcon(this.getClass().getResource("/Death123.png")).getImage();
lblNewLabel_6.setIcon(new ImageIcon(Death));
Image Win = new ImageIcon(this.getClass().getResource("/Win.png")).getImage();
lblNewLabel_7.setIcon(new ImageIcon(Win));
Image Load = new ImageIcon(this.getClass().getResource("/Load.png")).getImage();
lblNewLabel_8.setIcon(new ImageIcon(Load));
Image Save = new ImageIcon(this.getClass().getResource("/Save.png")).getImage();
lblNewLabel_9.setIcon(new ImageIcon(Save));
}
}