JAVA中的大写ActionEvent函数

时间:2014-02-04 23:03:22

标签: java swing jtextfield jtextarea actionevent

我的程序的目标是让用户在文本字段中输入一些文本。按下按钮后,按钮ActionEvent应该将文本大写并将其打印在按钮下方的文本区域中。我已经在下面发布了我的代码,但我不确定为什么它没有按照我的描述进行操作。它给了我错误:“

 Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation     problems: 
field1 cannot be resolved
area1 cannot be resolved

at SecondLab.actionPerformed(SecondLab.java:24)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

代码:

 import javax.swing.*;

 import java.awt.event.*;
 import java.awt.*;
 import java.io.*;

 public class SecondLab extends JFrame implements ActionListener{
public SecondLab() {
    setLayout(new GridLayout(3, 1, 5, 5));

    JTextField field1 = new JTextField();
    JTextArea area1 = new JTextArea();
    area1.setEditable(false);
    JButton jbtSubmit = new JButton("Submit");
    jbtSubmit.addActionListener(this);
    add(field1);
    add(jbtSubmit);
    add(area1);


}

public void actionPerformed(ActionEvent a) {
String text = field1.getText();
String upper = text.toUpperCase();
area1.setText(upper);


}

public static void main(String[] args) {
    SecondLab frame = new SecondLab();
    frame.setTitle("Lab #2"); //Sets the window title
    frame.setSize(500, 400); //Starts with a default size
    frame.setLocationRelativeTo(null); //Starts in center of screen
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to end after closing
    frame.setVisible(true); //Tells the frame to show up
}

 }

1 个答案:

答案 0 :(得分:3)

变量field1area1仅在SecondLab构造函数的范围内定义。将这些声明为类实例变量

private JTextField field1;
private JTextArea area1;

通过actionPerformed方法显示它们。另外,请务必删除作业中的初始关键字

field1 = new JTextField();
area1 = new JTextArea();

阅读:Understanding Class Members