嘿,我想要一个有"LeftButton“和"RightButton”按钮的JFrame和一个JTextArea。在我按下两个按钮中的一个后,我想让JTextArea在新的一行中写下已按下的按钮。为了做到这一点,我想使用一个引用到JTextArea的MyActionListener类,它实现了动作侦听器。
我试着给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会写出按下了哪个按钮,但按下后什么也不会发生。未弹出错误。
发布于 2019-05-28 16:40:42
我试着在JDK8上编译你的代码,它给出了错误,我可以看到它几乎没有问题。
首先,这是:
MAL = new MyActionListener(south);
south = new JTextArea(5, 20);
south.setEditable(false);
您正在将Null作为参数传递给侦听器。在将"south“传递给MAL构造函数之前,必须先初始化”south“。另外,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);
}
}
}
https://stackoverflow.com/questions/56346290
复制