亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按下按鈕后如何使用自己的 ActionListener 類將文本附加到 JTextArea

按下按鈕后如何使用自己的 ActionListener 類將文本附加到 JTextArea

飲歌長嘯 2022-12-21 15:09:29
嘿,我想要一個 JFrame,它必須包含按鈕“LeftButton”和“RightButton”以及一個 JTextArea。在我按下兩者之一之后,我希望 JTextArea 在新的一行中寫入哪些按鈕已被按下。為了做到這一點,我想使用一個 MyActionListener 類,并引用 JTextArea,它實現了 Actionlistener。我試圖為 ActionPerformed 提供 JTextArea 并意識到我必須自己創建 Setters。然后我意識到 MyActionListener 類還需要一個像 JTextArea 這樣的對象,它與 JFrame 類中的相同。然后我發現我還必須更新 JFrame 類中的 JTextArea,現在我有點卡住了。我試圖將 Setters 放入 JFrame 類并從 MyActionListener 調用它們但沒有成功,我嘗試做類似的事情A_18_c.south = southpackage 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);    }}我的動作監聽器:package Aufgabe_18;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;我希望 JTextArea 寫下哪個 Button 已被按下,但按下后沒有任何反應。沒有錯誤彈出。
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

我試著在 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);

    }

}

}



查看完整回答
反對 回復 2022-12-21
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號