簡體   English   中英

如何在Swing中的國際化消息框中更改按鈕文本

[英]How do I change the button text in an internationalized message box in Swing

我正在嘗試讓Java Swing消息框使用資源包來獲取翻譯。 除了“確定”按鈕,“取消”按鈕和“是/否”按鈕之外,我可以執行其他所有操作。 有人知道Swing消息框是否可以翻譯?

抱歉-這是我可以用來說明問題的最短代碼

package s17xlat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ShowMbox extends JFrame {
    // Keep compiler happy
    static final long serialVersionUID = 0;
    ResourceBundle m_rb = null;
    JPanel m_panel;
    Map<AbstractButton, String> m_buttons;

    public void create()
    {   
        rbRedo(Locale.getDefault());
        JButton btnEnglish = new JButton();
        rbTag(btnEnglish, "english");
        btnEnglish.addActionListener(
            new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent event)
                {
                    rbRedo(Locale.ENGLISH);
                }
            });
        JButton btnKlingon = new JButton();
        rbTag(btnKlingon, "klingon");
        btnKlingon.addActionListener(
            new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent event)
                {
                    // Create a new Klingon locale
                    rbRedo(new Locale("kl", "KL"));
                }
            });
        JButton btnInformation = new JButton();
        rbTag(btnInformation, "information");
        btnInformation.addActionListener(
            new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent event)
                {
                    JOptionPane.showMessageDialog(
                        m_panel,
                        rbTag("tldr"),
                        rbTag("information"),
                        JOptionPane.INFORMATION_MESSAGE);
                }
            });

        m_panel = new JPanel();
        m_panel.add(btnEnglish);
        m_panel.add(btnKlingon);
        m_panel.add(btnInformation);

        add(m_panel);
        setTitle("ShowMbox Test");
        setSize(300,200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    void rbRedo(Locale in_locale)
    {
        if (m_buttons == null)
            m_buttons = new HashMap<AbstractButton, String>();

        try
        {
            if (m_rb != null)
                ResourceBundle.clearCache();
            final String dir = System.getProperty("user.dir");
            File anyfile = new File(dir);
            URL[] urls = { anyfile.toURI().toURL() };
            ClassLoader loader = new URLClassLoader(urls);
            m_rb = ResourceBundle.getBundle("rosetta", in_locale, loader);

            if (!m_buttons.isEmpty())
            {
                // Refresh the buttons - should do the title as well
                for (AbstractButton button: m_buttons.keySet())
                {
                    String key = m_buttons.get(button);
                    button.setText(m_rb.getString(key));
                }
            }
        }
        catch (MalformedURLException e)
        {
            System.out.println(e.toString());
            System.exit(ABORT);
        }
        catch (MissingResourceException e)
        {
            System.out.println(e.toString() + "(" + e.getClassName() +".properties)");
            System.exit(ABORT);
        }
    }

    String rbTag(String in_key)
    {
        return m_rb.getString(in_key);
    }

    void rbTag(AbstractButton var_button, String in_key)
    {
        m_buttons.put(var_button,  in_key);

        String xlat = m_rb.getString(in_key);
        var_button.setText(xlat);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
            new Runnable()
            {
                @Override
                public void run()
                {
                    ShowMbox self = new ShowMbox();
                    self.create();
                    self.setVisible(true);
                }
            }
        );
    }
}

我在英國,所以rosetta_en_GB.properties是

english=English
klingon=Klingon
information=Information
tldr=Too Long Don't Read
OK=Oh Kay

Klingon版本是rosetta_kl_KL.properties

english=English Hol
klingon=Tlhlngan
information=De'
tldr=je qaStaHvIS Qo' laD
OK=Lu'

我想知道的是

  1. 如果我要用英語撿起“ Oh Kay”,用克林貢撿起“ Lu'”,該標簽應該叫什么?
  2. 如何或在哪里找到在消息框中調用這些標簽的內容。

如果無法更改標簽,則可能必須編寫自己的消息框。

http://www.jguru.com/faq/view.jsp?EID=86180中找到了答案。 基本上,我不是在搜索正確的關鍵字:應該是JOptionPane而不是MessageBox。

UIManager.put("OptionPane.okButtonText", m_rb.getString("ok"));

其他OptionPane按鈕是

  • OptionPane.noButtonText
  • OptionPane.yesButtonText
  • OptionPane.cancelButtonText

可以使用JOptionPane.showOptionDialog方法來自定義更多選項,而不是使用JOptionPane.showMessageDialog

對於您的代碼:

JOptionPane.showOptionDialog(
    m_panel,
    rbTag("tldr"),
    rbTag("information"),
    JOptionPane.OK_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    null,
    new Object[]{ rbTag("OK") },
    null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM