Ho capito tutto tranne questo:

Originariamente inviata da
andbin
1) Abituati a tenere i riferimenti ai componenti come variabili di istanza della tua classe. Es.:
codice:
public class esempio extends JFrame {
private JPanel contentPane;
private JLabel label; // <------
.....
Ti facilita di molto le cose, specialmente se i componenti devono essere usati anche da altri metodi della classe. Se non li vuoi tenere tutti, tieni almeno quelli con cui sai che devi interagire in seguito alla creazione della interfaccia.
Un' altra cosa, adesso non mi dà errori, ma comunque non mi traduce in inglese quando clicco sul bottone english, perchè?
Ti posto il codice così ci capisci megli anche tu:
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;
public class esempio extends JFrame {
private JPanel contentPane;
/**
* 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, 101);
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(164, 29, 250, 15);
contentPane.add(label);
//listener
btnItaliano.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Locale italy = new Locale("it","IT");
ResourceBundle bundle = ResourceBundle.getBundle("trad", italy);
label.setText(bundle.getString("stringa"));
}
});
btnInglese.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Locale usa = new Locale("en","US");
ResourceBundle bundle = ResourceBundle.getBundle("trad", usa);
label.setText(bundle.getString("stringa"));
}
});
}
}