【问题标题】:Getting all locales in java在java中获取所有语言环境
【发布时间】:2014-02-27 12:44:33
【问题描述】:

问题

在 Eclipse 中创建一个新项目
• 创建一个JFrame 应用程序,其中包含一个带有“列出所有区域设置”标签的JButton 和一个JTextArea。按下 JButton 时,将所有可用的语言环境输出到 JTextArea。

所以我的代码在它有 JButton 的地方工作,但是当按下按钮时,我似乎无法让它打印出 JTextArea 中的所有语言环境。有什么我想念的吗

我的代码:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
* 
*/

/**
* @author Michelle
*
*/
public class Exercise1 extends JFrame implements ActionListener{

    JButton button;
    JFrame frame;
    JTextArea ta;

    private static final long serialVersionUID = 1L;
    Locale[] available = Calendar.getAvailableLocales();


public Exercise1(){

    Container c = getContentPane();
    JPanel p = new JPanel();
    ta = new JTextArea(20,22);
    ta.setText("All Locales will display here");
    ta.setEditable(false);
    button = new JButton();
    button.setText("List all Locales");
    JScrollPane output = new JScrollPane(ta);
    button.addActionListener(this);
    p.add(button, BorderLayout.SOUTH);
    p.add(output);
    c.add(p);


    setSize(300,300);
    setVisible(true);
}
public void actionPerformed(ActionEvent a) {
    // TO DO Auto-generated method stub
    if (a.getActionCommand().equals("List All Locales")){

        for(int i=0; i<available.length;i++){

            ta.append(available[i].getDisplayName()+"\n");
        }
    }
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
        Exercise1 myLocaleTest = new Exercise1();
        myLocaleTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

【问题讨论】:

  • 您的行动检查对我来说似乎不正确。您似乎正在检查按钮的标签!?是否有 button.setCommand(String) 或您忘记的东西?
  • 快速谷歌发现了这个方法:setActionCommand(String);我建议使用 than。

标签: java eclipse swing jframe jbutton


【解决方案1】:

按钮上的文字是“列出所有区域设置”

button.setText("List all Locales");

actionPerformed 中,您正在检查操作命令是否为“列出所有区域设置”

if (a.getActionCommand().equals("List All Locales")){

你看出"List all Locales""List All Locales"之间的区别了吗?

为避免此类错误,您可以在类中创建一个常量并使用它。例如:

public class Exercise1 extends JFrame implements ActionListener{

    private static final String BUTTON_ACTION = "List all Locales";

    public Exercise1(){
        // ...
        button.setText(BUTTON_ACTION);
        // ...
    }

    public void actionPerformed(ActionEvent a) {
        if (a.getActionCommand().equals(BUTTON_ACTION)){
            // ...

【讨论】:

  • 标签是用的?如果有多个按钮具有相同的标签文本怎么办?
  • 为什么要在同一个窗口中拥有多个文本完全相同的按钮?
  • @munyul 那是开发者的错误。
  • @Jesper 原因应该不重要,支持它的能力应该有!我确定 SWT 支持它。
  • @AJ,或者库开发人员缺乏远见/想象力 - 这是另一种看待它的方式。
【解决方案2】:

改为equalsIgnoreCase...

if (a.getActionCommand().equalsIgnoreCase("List all locales")){

【讨论】:

    【解决方案3】:

    此处拼写错误if (a.getActionCommand().equals("List All Locales"))。按钮有下一个命令"List all Locales"。改变它,一切都会好起来的。

    【讨论】:

      【解决方案4】:

      其他人已经指出您的字符串检查中有大写/小写拼写错误。

      使用static final 的建议很好,但不适用于按钮标签!为什么?零语言支持!

      我在网上快速搜索了一下,找到了方法:setActionCommand(String);

      private static final String ACTION_COMMAND = "ac1";
      
      ...
      
      JButton jbutton = new JButton();
      jbutton.setText("My label text");
      jbutton.setActionCommand(ACTION_COMMAND);
      
      ...
      
      if ( action.getActionCommand().equals(ACTION_COMMAND) )
      

      static final 应该用于设置动作命令和测试动作,而不是标签!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        相关资源
        最近更新 更多