1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test005locale;
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* @author fabriceb
*/
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private static final Locale[] LOCALES = {Locale.US, Locale.FRANCE, new Locale("ar")};
private static final Locale DEFAULT_LOCALE = Locale.US;
public Main() throws Exception {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildGUI();
setSize(500, 400);
setLocationRelativeTo(null);
setVisible(true);
}
private void buildGUI() throws Exception {
Locale currentLocale = Locale.getDefault();
ResourceBundle bundle = ResourceBundle.getBundle("test005locale/labels");
JMenu languageMenu = new JMenu(bundle.getString("MENU_LANGUAGE"));
ButtonGroup group = new ButtonGroup();
for (Locale locale : LOCALES) {
String label = locale.getDisplayName();
if (!locale.equals(currentLocale)) {
String pattern = "{0} - {1}";
String name0 = locale.getDisplayName();
String name1 = locale.getDisplayName(locale);
label = MessageFormat.format(pattern, name0, name1);
}
JRadioButtonMenuItem item = new JRadioButtonMenuItem(label);
final Locale loc = locale;
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setVisible(false);
Preferences prefs = Preferences.userNodeForPackage(Main.class);
prefs.put("language", loc.getLanguage());
prefs.put("country", loc.getCountry());
Locale.setDefault(loc);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
setJMenuBar(null);
getContentPane().removeAll();
buildGUI();
setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
if (locale.equals(currentLocale)) {
item.setSelected(true);
}
group.add(item);
languageMenu.add(item);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(languageMenu);
setJMenuBar(menuBar);
JLabel textLabel = new JLabel(bundle.getString("LABEL_TEXT"));
JTextPane textPane = new JTextPane();
textPane.setText(bundle.getString("MESSAGE_TEXT"));
JScrollPane textScroll = new JScrollPane(textPane);
textScroll.setPreferredSize(new Dimension(320, 200));
JPanel sidePanel = new JPanel();
sidePanel.setLayout(new BoxLayout(sidePanel,BoxLayout.Y_AXIS));
sidePanel.add(textLabel);
JLabel progressLabel = new JLabel(bundle.getString("LABEL_PROGRESS"));
JProgressBar progressBar = new JProgressBar();
progressBar.setValue(50);
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
southPanel.add(progressLabel);
southPanel.add(progressBar);
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
for (int i = 0 ; i < 10;i++) {
String pattern = bundle.getString("ACTION_PATTERN");
final String label = MessageFormat.format(pattern, i);
toolBar.add(new AbstractAction(label) {
public void actionPerformed(ActionEvent event) {
System.out.println(getValue(Action.NAME));
}
});
}
ComponentOrientation orientation = ComponentOrientation.getOrientation(currentLocale);
setLayout(new BorderLayout());
add(sidePanel, orientation.isLeftToRight() ? BorderLayout.WEST : BorderLayout.EAST);
add(Box.createHorizontalStrut(3), orientation.isLeftToRight() ? BorderLayout.EAST : BorderLayout.WEST);
add(textScroll, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
add(toolBar, BorderLayout.NORTH);
applyComponentOrientation(orientation);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Preferences prefs = Preferences.userNodeForPackage(Main.class);
String language = prefs.get("language", DEFAULT_LOCALE.getLanguage());
String country = prefs.get("country", DEFAULT_LOCALE.getCountry());
Locale.setDefault(new Locale(language, country));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Main frame = new Main();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
} |
Partager