按下按钮后,如何使用自己的ActionListener类将文本追加到JTextArea

时间:2019-05-28 16:11:57

标签: java jframe actionlistener jtextarea

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

我试图为ActionPerformed提供JTextArea,并意识到我必须自己创建Setter。然后我意识到MyActionListener类还需要一个像JTextArea这样的对象,它与JFrame类中的对象相同。然后我发现我还必须更新JFrame类中的JTextArea,在这里我现在有点卡住了。我试图将Setters放入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写入按下了哪个Button,但是按下后什么也没有发生。没有错误弹出。

1 个答案:

答案 0 :(得分:0)

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

首先:

MAL = new MyActionListener(south);

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

您正在将Null作为参数传递给侦听器。您必须先将“ south”初始化,然后再将其在构造函​​数中传递给MAL。而且Button没有任何方法作为getString。它具有用于JButton的getLabel或getText。也正如@vince所说,在“ LeftButton”中添加空间。我已经对您的代码进行了一些调整。以下是工作代码。为简单起见,我在同一文件中添加了自定义侦听器,并用JButton替换了Button(您已经在使用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);
    }
}
}