繁体   English   中英

JAVA中的大写ActionEvent函数

[英]Capitalization ActionEvent Function in JAVA

我的程序的目标是让用户在文本字段中输入一些文本。 按下按钮后,按钮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
}

 }

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

private JTextField field1;
private JTextArea area1;

使它们从actionPerformed方法可见。 另外,请务必删除分配中的初始关键字

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

阅读: 了解班级成员

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM