【发布时间】: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