【发布时间】:2016-02-21 20:54:34
【问题描述】:
我想在 JavaFx 中构建一个聊天应用程序,其中将有一个按钮让用户在英语和印地语(印度地区语言)之间切换。问题是我无法弄清楚我应该如何更改用户单击按钮时 JavaFx 文本框的文本语言。由于这个问题,我还没有开始开发我的项目。请给我一个演示程序,它可以在单击按钮时更改文本框中的文本。
谢谢!!!
编辑: 我试过这样做,对不起,我从来没有使用资源包或语言环境来动态更改文本,所以我不知道如何实现它....
import java.util.Locale;
public class demoLang extends javax.swing.JFrame {
public demoLang() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jLabel1.setText("language");
jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton1.setText("Change");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setText("jTextField1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(116, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(123, 123, 123))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Locale local = new Locale("hi","IN");
Locale.setDefault(local);
jTextField1.setLocale(local);
jTextField1.setText("" + local.getDisplayLanguage());
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(demoLang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(demoLang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(demoLang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(demoLang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new demoLang().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
【问题讨论】:
-
你的问题很不清楚。首先,您使用的是 JavaFX 还是 Swing?这是两个不同的 UI 工具包。您说您使用的是 JavaFX,但您的代码使用的是 Swing。其次,如果没有某种翻译服务可以将任意英文文本翻译成印地语文本,反之亦然,你不可能做到你所说的。实施这样的服务是一项非常重要的任务,(远远)超出了本论坛的问题范围。如果你有这样的服务,那么你只需要调用那个 API,但是不知道那个 API 没有人可以回答这个问题。
-
这似乎也是stackoverflow.com/questions/33817646/…的完全相同的副本
-
他们中的任何一个都会满足...这意味着您是说如果不使用翻译服务就无法更改输入语言???? stackoverflow.com/questions/25959166/… 即使上述问题的解决方案也不能满足我的要求....我基本上只需要当用户选择按钮上标记的特定语言(印地语/英语)时,文本字段的输入语言就会切换到那个语言....我尝试了焦点监听器,但我没有得到输出请帮助解决这些问题
-
这意味着如果用户选择了印地语,那么当他在文本字段中输入时,显示文本应该是印地语
-
好的,我会再试一次...我正在使用 javafx / swing 实现一个聊天应用程序,我卡住的地方是为用户提供多语言支持的一部分。在 gui 中,将有一个按钮可以将语言更改为印地语或英语。这是我不了解如何实现它的部分。如果用户单击印地语,则用户键入时文本字段中显示的语言必须是印地语。随意告诉是否需要更多解释:)