Ok ho modificato un po' il codice per farti capire cosa voglio fare e perchè mi sarebbe comoda una stringa modificabile
codice:
import java.awt.EventQueue;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
public class esempio extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
esempio frame = new esempio();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public esempio() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 251);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnItaliano = new JButton("Italiano");
btnItaliano.setBounds(12, 12, 117, 25);
contentPane.add(btnItaliano);
JButton btnInglese = new JButton("English");
btnInglese.setBounds(12, 43, 117, 25);
contentPane.add(btnInglese);
final JLabel label = new JLabel("");
label.setBounds(150, 105, 250, 15);
contentPane.add(label);
textField = new JTextField();
textField.setBounds(12, 105, 117, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(12, 136, 117, 19);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel label_1 = new JLabel("");
label_1.setBounds(150, 138, 250, 15);
contentPane.add(label_1);
JButton btnControlla = new JButton("Controlla");
btnControlla.setBounds(12, 167, 117, 25);
contentPane.add(btnControlla);
//stringhe
Locale italy = new Locale("it","IT"); // utilizzo l'italiano di default all' apertura del programma
ResourceBundle bundle = ResourceBundle.getBundle("lang/trad", italy);
final String Stringa = bundle.getString("stringa");
//listener
btnItaliano.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Locale italy = new Locale("it","IT");
ResourceBundle bundle = ResourceBundle.getBundle("lang.trad", italy);
Stringa = bundle.getString("stringa"); // Cosa che ovviamente non si fa ma è solo per far capire il concetto di ciò che vorrei fare
}
});
btnInglese.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Locale usa = new Locale("en","US");
ResourceBundle bundle = ResourceBundle.getBundle("lang.trad", usa);
Stringa = bundle.getString("stringa"); // idem
}
});
btnControlla.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s = textField.getText(); // controllo che il numero inerito sia intero
if(!isInt(s)) {
label.setText(Stringa);
}
else {
label.setText("");
}
String s1 = textField_1.getText();
if(!isInt(s1)) {
label_1.setText(Stringa);
}
else {
label_1.setText("");
}
}
});
}
// metodi
private boolean isInt (String s) { // ho fatto un metodo bruto per controllare se il numero è un int
try {
Integer.parseInt(s);
return true;
}
catch(Exception e) {
System.out.println("Il numero non è intero");
return false;
}
}
}