嘿,我想要一个具有按钮“LeftButton”和“RightButton”的 JFrame 以及一个 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);
}
}
我的ActionListener:
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 能够写入哪个按钮被按下,但是按下后什么也没有发生。没有弹出错误。
最佳答案
我尝试在 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);
}
}
}
关于java - 按下按钮后如何使用自己的 ActionListener 类将文本附加到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56346290/