UI troubles... 2006-12-08 - By Ne'Bahn
Back Hi list, I'm wondering if there is a way to "justify" components on a JFrame / JDialog or wherever they are...The problem is: I've a UI and I want to show it in many languages (I18n), but when language change take place the UI comes "crazy", for example: text on buttons doesn't fit, text labels are truncated and so on. Any one has dealing with this prior ???
Thanks in advance...
-- ---- ---- ---- -- filename: I18n.java -- ---- ---- ---- -- import java.awt.Dimension; import java.awt.Font; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Locale; import java.util.ResourceBundle;
import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;
public class I18n extends JFrame { private JPanel shoppingCartPanel = new JPanel(); private JPanel customerPanel = new JPanel(); private JLabel applesLabel = new JLabel(); private JLabel peachesLabel = new JLabel(); private JLabel pearsLabel = new JLabel(); private JLabel totalItemsLabel = new JLabel(); private JLabel itemsLabel = new JLabel(); private JLabel totalCostLabel = new JLabel(); private JLabel costLabel = new JLabel(); private JLabel customerIDLabel = new JLabel(); private JLabel creditCardLabel = new JLabel(); private JTextField applesTextField = new JTextField(); private JTextField peachesTextField = new JTextField(); private JTextField pearsTextField = new JTextField(); private JTextField customerIDTextField = new JTextField(); private JTextField creditCardTextField = new JTextField(); private JButton purchaseButton = new JButton(); private JButton clearButton = new JButton(); private Locale locale = null; private ResourceBundle resourceBundle = null; private NumberFormat numberFormat = null;
public I18n(String language, String country) { locale = new Locale(language, country); resourceBundle = ResourceBundle.getBundle("Text", locale); numberFormat = NumberFormat.getNumberInstance(locale); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } }
private void jbInit() throws Exception { this.setTitle(resourceBundle.getString("title")); this.getContentPane().setLayout(null); this.setSize(new Dimension(316, 309)); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getRootPane().setDefaultButton(purchaseButton);
// SHOPPING CART panel shoppingCartPanel.setBounds(new Rectangle(10, 5, 290, 145)); shoppingCartPanel.setLayout(null); shoppingCartPanel.setBorder(BorderFactory.createTitledBorder(resourceBundle .getString("shopping_cart"))); shoppingCartPanel.add(peachesTextField, null); shoppingCartPanel.add(applesTextField, null); shoppingCartPanel.add(costLabel, null); shoppingCartPanel.add(totalCostLabel, null); shoppingCartPanel.add(itemsLabel, null); shoppingCartPanel.add(totalItemsLabel, null); shoppingCartPanel.add(pearsLabel, null); shoppingCartPanel.add(peachesLabel, null); shoppingCartPanel.add(applesLabel, null); shoppingCartPanel.add(pearsTextField, null); applesLabel.setText(resourceBundle.getString("apples")); applesLabel.setBounds(new Rectangle(25, 23, 40, 15)); applesLabel.setDisplayedMnemonic('A'); peachesLabel.setText(resourceBundle.getString("peaches")); peachesLabel.setBounds(new Rectangle(25, 47, 50, 15)); peachesLabel.setDisplayedMnemonic('E'); pearsLabel.setText(resourceBundle.getString("pears")); pearsLabel.setBounds(new Rectangle(25, 73, 40, 15)); pearsLabel.setDisplayedMnemonic('A'); totalItemsLabel.setText(resourceBundle.getString("total_tems")); totalItemsLabel.setBounds(new Rectangle(25, 100, 75, 15)); totalItemsLabel.setFont(new Font("Tahoma", 1, 11)); itemsLabel.setText("0"); itemsLabel.setBounds(new Rectangle(100, 100, 175, 15)); totalCostLabel.setText(resourceBundle.getString("total_cost")); totalCostLabel.setBounds(new Rectangle(25, 120, 65, 15)); totalCostLabel.setFont(new Font("Tahoma", 1, 11)); costLabel.setText("0"); costLabel.setBounds(new Rectangle(100, 120, 175, 15)); applesTextField.setBounds(new Rectangle(75, 20, 200, 20)); peachesTextField.setBounds(new Rectangle(75, 45, 200, 20)); pearsTextField.setBounds(new Rectangle(75, 70, 200, 20));
// CUSTOMER panel customerPanel.setBounds(new Rectangle(10, 150, 290, 80)); customerPanel.setLayout(null); customerPanel.setBorder(BorderFactory.createTitledBorder(resourceBundle .getString("customer"))); customerPanel.add(creditCardTextField, null); customerPanel.add(customerIDTextField, null); customerPanel.add(creditCardLabel, null); customerPanel.add(customerIDLabel, null); customerIDLabel.setText(resourceBundle.getString("customer_ID")); customerIDLabel.setBounds(new Rectangle(25, 22, 70, 15)); customerIDLabel.setDisplayedMnemonic('U'); creditCardLabel.setText(resourceBundle.getString("credit_card")); creditCardLabel.setBounds(new Rectangle(25, 47, 60, 15)); creditCardLabel.setDisplayedMnemonic('D'); customerIDTextField.setBounds(new Rectangle(95, 20, 180, 19)); creditCardTextField.setBounds(new Rectangle(95, 45, 180, 19));
// UI purchaseButton.setText(resourceBundle.getString("purchase")); purchaseButton.setBounds(new Rectangle(130, 240, 80, 25)); purchaseButton.setMnemonic('P'); purchaseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { purchaseButton_actionPerformed(e); } }); clearButton.setText(resourceBundle.getString("clear")); clearButton.setBounds(new Rectangle(220, 240, 80, 25)); clearButton.setMnemonic('C'); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clearButton_actionPerformed(e); } });
this.getContentPane().add(clearButton, null); this.getContentPane().add(purchaseButton, null); this.getContentPane().add(customerPanel, null); this.getContentPane().add(shoppingCartPanel, null); }
private void purchaseButton_actionPerformed(ActionEvent e) { if (e != null) { } }
private void clearButton_actionPerformed(ActionEvent e) { if (e != null) { } }
public static void main(String[] args) { I18n i18n = new I18n("de", "DE"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = i18n.getSize();
if (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width; i18n.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); i18n.setVisible(true); } }
-- ---- ---- ---- ---- ---- ------ filename: Text_de_DE.properties -- ---- ---- ---- ---- ---- ------ # Application title title = Fr?chte 1.25 jede
# Labels apples = ?pfel: peaches = Birnen: pears = Pfirsiche: total_tems = Anzahl Fr?chte: total_cost = Gesamtkosten: credit_card = Kreditkarte: customer_ID = Kundenidentifizierung:
# Panels shopping_cart = " Kredit " customer = " Kunden "
# Buttons purchase = Kaufen clear = Zur?cksetzen
# Messages invalid_value = Ung?ltiger Wert cannot_send = Daten?bertragung zum Server nicht m?glich no_lookup = Das Server l??t sich nicht zu finden no_data = Keine Daten verf?gbar no_ID = Keine Kundenidentifizierungen verf?gbar no_server = Kein Zugang zu den Daten beim Server
-- ---- ---- ---- ---- ---- ------ filename: Text_en_US.properties -- ---- ---- ---- ---- ---- ------ # Application title title = Fruit 1.25 Each
# Labels apples = Apples: apples_mnemonic = A peaches = Peaches: pears = Pears: total_tems = Total items: total_cost = Total cost: credit_card = Credit card: customer_ID = Customer ID:
# Panels shopping_cart = " Shopping cart " customer = " Customer "
# Buttons purchase = Purchase clear = Clear
# Messages invalid_value = Invalid value cannot_send = Cannot send data to server no_lookup = Cannot look up remote server object no_data = No data available no_ID = No customer IDs available no_server = Cannot access data in server
-- ---- ---- ---- ---- ---- ------ filename: Text_fr_FR.properties -- ---- ---- ---- ---- ---- ------ # Application title title = Fruit 1.25 pi?ce
# Labels apples = Pommes: peaches = P?ches: pears = Poires: total_tems = Partial total: total_cost = Prix total: credit_card = Carte de Cr?dit: customer_ID = Num?ro de client:
# Panels shopping_cart = " Carte de pi?ce " customer = " Client "
# Buttons purchase = Achetez clear = R?initialisez
# Messages invalid_value = Valeur incorrecte cannot_send = Les donn?es n'ont pu ?tre envoy?es au serveur no_lookup = Acc?s impossible ? l'objet du serveur distant [remote server object] no_data = Aucune donn?e disponible no_ID = Identifiant du client indisponible no_server = Acc?s aux donn?es du serveur impossible
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|