Цель моей программы - ввести текст в текстовое поле. После нажатия кнопки кнопка ActionEvent должна загладить текст и печатать его в текстовой области под кнопкой. Я разместил свой код ниже, но я не уверен, почему он не делает то, что я описал. Это дает мне ошибку: "Функция «Действие» Actionization в JAVA
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
}
}
У меня возникли проблемы с пониманием, как именно мне нужно изменить мой код для того, чтобы делать то, что вы описали. Предполагается ли, что частные переменные помещаются вне конструктора SecondLab? – Ben
Да, в классе - прочитайте ссылку, которую я только что добавил :) – Reimeus
Это очень помогает. Большое спасибо, я понял! – Ben