【问题标题】:How can I append Text to JTextArea with an own ActionListener Class after pressing Buttons按下按钮后如何使用自己的 ActionListener 类将文本附加到 JTextArea
【发布时间】:2019-05-28 16:11:57
【问题描述】:

嘿,我想要一个 JFrame,它必须有按钮“L​​eftButton”和“RightButton”以及一个 JTextArea。在我按下两个之一后,我希望 JTextArea 在新行中写入已按下的按钮。为了做到这一点,我想使用一个 MyActionListener 类,它引用了实现 Actionlistener 的 JTextArea。

我尝试为 ActionPerformed 提供 JTextArea 并意识到我必须自己创建 Setter。然后我意识到 MyActionListener 类还需要一个像 JTextArea 这样的对象,它与 JFrame 类中的相同。然后我发现我还必须更新 JFrame 类中的 JTextArea,我现在有点卡住了。我试图将 Setter 放入 JFrame 类并从 MyActionListener 调用它们但没有成功,我尝试做类似A_18_c.south = south

package Aufgabe_18;

import javax.swing.*;
import java.awt.*;

public class A_18_c extends JFrame {
    private Button LeftButton;
    private Button RightButton;
    private JScrollPane scroll;
    private JTextArea south;
    private MyActionListener MAL;

    public static void main(String[] args) {
        A_18_c l = new A_18_c("Aufgabe18c");
    }


    public A_18_c(String title) {
        super(title);
        setSize(300, 150);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        MAL = new MyActionListener(south);

        south = new JTextArea(5, 20);
        south.setEditable(false);
        JScrollPane sroll = new JScrollPane(south);
        this.add(sroll, BorderLayout.SOUTH);

        LeftButton = new Button("Left Button");
        LeftButton.setOpaque(true);
        LeftButton.addActionListener(MAL);
        this.add(LeftButton, BorderLayout.WEST);

        RightButton = new Button("Right Button");
        RightButton.setOpaque(true);
        RightButton.addActionListener(MAL);
        this.add(RightButton, BorderLayout.EAST);

        setVisible(true);
    }
}

MyActionListener:

package Aufgabe_18;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyActionListener implements ActionListener{

    private final JTextArea south;

    public MyActionListener(JTextArea south)
    {
        this.south = south;
    }

    private void setTextLeftButton(JTextArea south){
        south.append("Left Button \n");
    }

    private void setTextRightButton(JTextArea south){
        south.append("Right Button \n");
    }

@Override
        public void actionPerformed(ActionEvent e) {
        String a;
        Object src = e.getSource();
        Button b = null;
        b = (Button) src;
        a = b.getString();
        if (a == "LeftButton")
            setTextRightButton(south);
        if (a == "RightButton")
            setTextRightButton(south);
    }
}

我希望 JTextArea 写出按下了哪个按钮,但按下后什么也没有发生。没有错误弹出。

【问题讨论】:

  • 您的按钮名称有空格。您的听众的支票不占空间。

标签: java swing jframe actionlistener jtextarea


【解决方案1】:

我尝试在 JDK8 上编译您的代码,它给出了错误,我可以看到它几乎没有问题。

首先:

MAL = new MyActionListener(south);

south = new JTextArea(5, 20);
south.setEditable(false);

您将 Null 作为参数传递给您的侦听器。您必须先初始化“south”,然后才能在构造函数中将其传递给 MAL。 Button 也没有任何方法作为 getString。它有 JButton 的 getLabel 或 getText。同样正如@vince 所说,在“LeftButton”中添加空格。我对您的代码进行了一些调整。下面是工作代码。为简单起见,我在同一个文件中添加了自定义侦听器,并将 Button 替换为 JButton(您已经在使用 swing 的 JFrame,因此最好尝试使用所有 swing 组件)。你会得到这个要点:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

public class Test extends JFrame {
    private JButton LeftButton;
    private JButton RightButton;
    private JScrollPane scroll;
    private JTextArea south;
    private MyActionListener MAL;

    public static void main(String[] args) {
        Test l = new Test("Aufgabe18c");
    }

    public Test(String title) {
        super(title);
        setSize(300, 150);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //initialize south
        south = new JTextArea(5, 20);
        south.setEditable(true);

        //pass it to your Listener
        MAL = new MyActionListener(south);
        JScrollPane scroll = new JScrollPane(south);
        this.add(scroll, BorderLayout.SOUTH);

        LeftButton = new JButton("Left Button");
        LeftButton.setOpaque(true);
        LeftButton.addActionListener(MAL);
        this.add(LeftButton, BorderLayout.WEST);

        RightButton = new JButton("Right Button");
        RightButton.setOpaque(true);
        RightButton.addActionListener(MAL);
        this.add(RightButton, BorderLayout.EAST);

        setVisible(true);
    }


public class MyActionListener implements ActionListener{

    private final JTextArea south;

    public MyActionListener(JTextArea south)
    {
        this.south = south;
    }

    private void setTextLeftButton(JTextArea south){
        south.append("Left Button \n");
    }

    private void setTextRightButton(JTextArea south){
        south.append("Right Button \n");
    }

@Override
        public void actionPerformed(ActionEvent e) {
        String a;
        Object src = e.getSource();
        JButton b = null;
        b = (JButton) src;
        a = b.getText();
        if (a == "Left Button")
            setTextLeftButton(south);
        else
            setTextRightButton(south);
    }
}
}

【讨论】:

  • 非常感谢!所以我主要是搞砸了一些细节,并且一直在夸大它们。
  • @FrancisDrake 请投票并选择已接受的答案。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2016-09-02
  • 2017-04-07
  • 1970-01-01
  • 2023-03-30
  • 2021-02-28
  • 1970-01-01
相关资源
最近更新 更多