gpt4 book ai didi

java - JAVA中大写ActionEvent函数

转载 作者:行者123 更新时间:2023-11-29 05:29:37 25 4
gpt4 key购买 nike

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

阅读:Understanding Class Members

关于java - JAVA中大写ActionEvent函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21565405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com