gpt4 book ai didi

java - 尝试从文本字段获取文本以移动到文本区域

转载 作者:行者123 更新时间:2023-12-02 04:10:07 25 4
gpt4 key购买 nike

package edu.bsu.cs121.mamurphy;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.event.*;

public class InClass1Main extends JFrame {

JButton button1;
JTextField textField1;
JTextArea textArea1;
int buttonClicked;

public static void main(String[] args) {
new InClass1Main();
}

public InClass1Main() {

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension dim = tk.getScreenSize();
this.setSize((dim.width / 2), (dim.height / 2));
int xPos = (dim.width / 2) - (this.getWidth() / 2);
int yPos = (dim.height / 2) - (this.getHeight() / 2);

JPanel panel = new JPanel();
JLabel label1 = new JLabel("Translation Panel");
JButton button1 = new JButton("Transfer");

ListenForButton button = new ListenForButton();

button1.addActionListener(button);

JTextField textField1 = new JTextField();
JTextArea textArea1 = new JTextArea(10, 50);
JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

textField1.setColumns(50);

textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);

panel.add(label1);
panel.add(button1);
panel.add(textField1);
panel.add(scrollBar1);

this.add(panel);
this.setLocation(xPos, yPos);
this.setResizable(false);
this.setTitle("Translation Frame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

textField1.requestFocus();

}

private class ListenForButton implements ActionListener {

@Override
public void actionPerformed(ActionEvent ae) {
textArea1.setText(textField1.getText());
}
}
}

现在,每当我尝试在文本字段中实际输入文本,然后将其放入文本区域时,都会收到错误。我无法理解这个错误到底有什么意义。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at edu.bsu.cs121.mamurphy.InClass1Main$ListenForButton.actionPerformed(InClass1Main.java:66)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

我真的不知道是什么导致了这个错误。它给我的很多原因(正如你在上面看到的)是(来源不明)。因此,任何关于为什么会发生这种情况的帮助将不胜感激。

最佳答案

ListenForButton 主体中删除 addActionListener 语句

private class ListenForButton implements ActionListener {

@Override
public void actionPerformed(ActionEvent ae) {
textArea1.setText(textField1.getText());
}
}

关于java - 尝试从文本字段获取文本以移动到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33874191/

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