Salve a tutti ragazzi. Sto svolgendo questo esercizio per allenarmi un po con Java e WindowBuilder. Putroppo però ho problemi quando tento di salvare i miei 3 INT in un file di testo (Questo servirà perchè poi creero il metodo LOAD)
Apprezzerei davvero molto se qualcuno mi desse delle delucidazioni su quanto segue e dove sono i miei principali errori.
Vi ringrazio
Apprezzerei davvero molto se qualcuno mi desse delle delucidazioni su quanto segue e dove sono i miei principali errori.
Vi ringrazio
Java:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Formatter;
import javax.swing.JTextArea;
import javax.swing.DropMode;
import java.awt.Font;
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;
int contWin=0;
int contKill=0;
int contDeath=0;
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();
}
}
});
}
//Creazione File
/*private Formatter x;
public void openFile()
{
try
{
x = new Formatter("DarwinStats.txt");
}
catch(Exception e)
{
System.out.println("Errore!");
}
}
public void addRecords()
{
x.format("%s%s%s", S_Kill, S_Death , S_Win);
}
public void closeFile()
{
x.close();
} */
//Creazione File Metodo 2
/*public void SaveFile()
{
try
{
FileWriter writer = new FileWriter("Darwin.txt",true);
writer.write(S_Kill);
writer.write(S_Death);
writer.write(S_Win);
writer.close();
}
catch(Exception e)
{
System.out.println("Errore!");
}
}*/
//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_9 = new JLabel("");
lblNewLabel_9.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
GUI g = new GUI();
// g.SaveFile(); //Metodo 2
// g.openFile(); //Metodo 1
// g.addRecords(); //Metodo 1
// g.closeFile(); //Metodo 1
}
});
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);
lblNewLabel_9.setBounds(614, 691, 293, 71);
contentPane.add(lblNewLabel_9);
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_8 = new JLabel("");
lblNewLabel_8.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
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)
{
JOptionPane.showMessageDialog(null,"I dati sono stati inseriti correttamente.");
S_Kill = Integer.toString(Kill);
S_Death = Integer.toString(Death);
S_Win = Integer.toString(Win);
lblNewLabel_1.setText("");
lblNewLabel_2.setText("");
lblNewLabel_0.setText("");
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));
}
}