Frage

The goal of my program is to have the user enter some text into a text field. After pressing a button, the button ActionEvent should capitalize the text and print it in the text area below the button. I have posted my code below but I'm not sure as to why it doesn't do what I described. It gives me the error: "

 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)

And the code:

 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
}

 }
War es hilfreich?

Lösung

The variables field1 and area1 are only defined within the scope of the SecondLab constructor. Declare these as class instance variables

private JTextField field1;
private JTextArea area1;

to make them visible from the actionPerformed method. Also make sure to remove the initial keywords in the assignments

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

Read: Understanding Class Members

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top