简体   繁体   English

我想在运行时更改SWT应用程序的语言。 怎么做?

[英]I want to change the language of a SWT Application at Runtime. How to do that?

I'm developing an application to my software engineering class and one of the requisites is that it has 2 or more languages in the interface. 我正在为我的软件工程课开发一个应用程序,其中之一是接口中必须有2种或更多种语言。 I already implemented that. 我已经实现了。 On the other hand, it is required that the user can change the language on its own will by choosing from a combobox or something like that. 另一方面,要求用户可以通过从组合框等中进行选择来自行更改语言。

As i'm using AWT and Netbeans, I can't edit the initComponents method of the form, as it is automatically generated. 当我使用AWT和Netbeans时,我无法编辑表单的initComponents方法,因为它是自动生成的。 I have the option below in mind, but don't know if they would even work, so I'm asking for help. 我在心中有以下选择,但不知道它们是否还能工作,所以我正在寻求帮助。

-Edit the constructor of my class so that it looks like this: -编辑类的构造函数,使其看起来像这样:

public JFmyConstructor() {
    initComponents(); //auto-generated
    myInitMethod();
}

The problem I think i'm going to find is that I need to call the constructor one time after the interface is already running (and thus, all objects instantiated. Is there a workaround for this? 我想我会发现的问题是,我需要在接口已经运行之后调用构造函数一次(因此,所有对象都实例化了。是否有解决方法?

Probably my answer comes a little late (too bad you put wrong title)... 可能我的答案来得有点晚(可惜您输入的标题错误)...

Obviously you do not have to call any constructor in order to switch language at runtime. 显然,您无需调用任何构造函数即可在运行时切换语言。 In Java Desktop applications it is as ease as: 在Java Desktop应用程序中,它很简单:

Locale brazilian = new Locale("pt", "BR");
Locale.setDefault(brazilian);

Of course if you want to switch it via JComboBox, you would probably do it a bit different. 当然,如果要通过JComboBox进行切换,则可能会有所不同。 What I would do, I would actually create Model (Swing promotes MVC Design Pattern) that will hold data (different Locales) and I would set default Locale in correct ActionListener. 我会做的是,实际上将创建一个模型(Swing提倡MVC设计模式)来保存数据(不同的语言环境),并在正确的ActionListener中设置默认语言环境。 It is not easy though. 不过这并不容易。

Of course I would rather use native names for Locales. 当然,我宁愿使用本地名称作为语言环境。 If you have them constructed, you can easily get native name Locale name by simply calling getDisplayName() passing itself as a parameter: 如果已构建它们,则可以通过简单地调用getDisplayName()并将其自身作为参数传递来轻松获得本地名称Locale名称:

String brazilianNativeName = brazilian.getDisplayName(brazilian);

The result might be unsatisfactory for certain Locales (I believe Brazilian Portuguese is unfortunately one of them), but there is not much you can do about it (unless you use your own string). 对于某些语言环境,结果可能无法令人满意(我相信不幸的是巴西葡萄牙语是其中的一种),但是您对此无能为力(除非您使用自己的字符串)。

To be honest, for just two languages, I would probably use some links instead. 老实说,对于两种语言,我可能会改用一些链接。 You can use simple JLabels to do that, just set correct font properties and color and assign common ActionListener. 您可以使用简单的JLabel来做到这一点,只需设置正确的字体属性和颜色并分配通用的ActionListener即可。 You can then query which one was actually clicked and create and set Locale appropriately. 然后,您可以查询实际单击了哪一个,并适当地创建和设置Locale。 For homework that should be sufficient. 对于家庭作业来说应该足够了。

SWT and NetBeans is a weird combination. SWT和NetBeans是一个奇怪的组合。 If you can do things the "normal" way with SWT and use Eclipse, then there's a powerful set of tools for managing localization, all built right in. See, for example, this introduction. 如果您可以使用SWT以“正常”方式进行操作并使用Eclipse,那么将有一套强大的工具来管理本地化,所有这些工具都是内置的。例如,请参见本介绍。

The best way is to use resource injection for all components (rather than getting i18n strings from a Bundle at panel construction time). 最好的方法是对所有组件使用资源注入 (而不是在面板构建时从Bundle中获取i18n字符串)。

Resource Injection is available in a number of open source libraries or frameworks. 在许多开源库或框架中都可以使用资源注入。

Since you use NetBeans, you should be able to generate an application based on Swing Application Framework (SAF), which already supports Resource Injection. 由于使用NetBeans,因此您应该能够基于已经支持资源注入的Swing应用程序框架 (SAF)生成应用程序。

Otherwise, there are also libraries out there that do just that; 否则,那里也有图书馆可以做到这一点。 simply google for "swing" "resource injection" and you'll find, eg Fuse library. 只需在Google上搜索“ swing”“资源注入”即可,例如Fuse库。

Edit: You should also take a look at this SO question that deals exactly with the same problem, and which has more detailed answers (including my own). 编辑:您还应该看看这个SO问题该问题恰好处理相同的问题,并且有更详细的答案(包括我自己的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SWT应用程序中更改主题 - How to change theme in SWT application 如何在SWT应用程序中停止自定义Thead? - How do I stop a custom Thead in SWT application? 如何在独立的SWT应用程序中创建IMarker - How do I create an IMarker in a standalone SWT-application 给定一个字符串作为泛型类的类型名称,我如何在运行时创建实例。 如果不可能,还有其他方法吗? - Given a string as the type name of a generic class, how can I create the instance in runtime. If it is not possible, Is there another way? 在运行时更改数据库。 如何刷新 JdbcTemplate bean 的数据源? - Changing database during runtime. How can i refresh data source for JdbcTemplate bean? SWT:在运行时更改 Shell 的样式 - SWT: Change style of a Shell at runtime 如何使用Maven构建SWT应用程序 - How do you build an SWT application with Maven 如何将消息从Win32应用程序发送到Java SWT应用程序? - How do I send messages from Win32 application to Java SWT application? 如何在不重新启动 Spring Boot 应用程序的情况下在运行时更改日志级别 - how do I change log level in runtime without restarting spring boot application 如何在运行时更改日志级别而不重新启动Vert.x应用程序 - How do I change log level in runtime without restarting Vert.x application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM