0

我有一个使用 Japplet 的课程。该表单有 2 个输入字段和一个按钮。它还有一个 TextPanel 来显示用户输入的信息。我遇到的问题是使用动作侦听器显示在文本区域中输入的信息。我不知道我错过了什么。

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

public class CreatePanel extends JPanel
{
 private Vector accountList;
 private JButton button1;
 private TransferPanel transferPanel;
 final int FIELD_WIDTH = 10;
   final int ROWS = 50;
 final int COLUMNS = 50;



public CreatePanel(Vector accountList, TransferPanel tPanel)
 {
this.accountList = accountList;
this.transferPanel = tPanel;


JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField accountID = new JTextField();
JTextField amount = new JTextField();


button1 = new JButton("Create an Account");



JTextArea textArea = new JTextArea(ROWS, COLUMNS);
textArea.append("No account");
textArea.setEditable(true);

JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(3,2));
infoPanel.add(label1);
infoPanel.add(accountID);
infoPanel.add(label2);
infoPanel.add(amount);
infoPanel.add(button1);

add(infoPanel);

ActionListener listener = new ButtonListener();
button1.addActionListener(listener);

JPanel textPanel = new JPanel();
textPanel.add(textArea);

   add(textPanel);




  }



   private class ButtonListener implements ActionListener
    {


public void actionPerformed(ActionEvent event)
   {



   } //end of actionPerformed method
 } //end of ButtonListener class

} //end of CreatePanel class
4

3 回答 3

2

建议:

  • 首先,请努力格式化您的代码。如果格式不正确(例如您当前显示的随机百搭缩进),我们将无法很好地理解您的代码,并且您经常会出错。每个代码块应该缩进相同的数量,我通常使用 2-3 个空格(一个或另一个并保持一致)。此外,一行空白区域就足够了。
  • 至于您的问题,您的字段不应该是构造函数的本地字段,而应该是类字段,以便类的方法可以访问它们。特别是您的 JTextArea。否则,您的 ButtonListener 将无法识别 JTextArea 变量,因为该变量的范围将被限制在声明它的块中——这里是您的构造函数。

所以改变这个:

public class CreatePanel extends JPanel
{
 private Vector accountList;
 private JButton button1;
 private TransferPanel transferPanel;
 final int FIELD_WIDTH = 10;
   final int ROWS = 50;
 final int COLUMNS = 50;



public CreatePanel(Vector accountList, TransferPanel tPanel)
 {
this.accountList = accountList;
this.transferPanel = tPanel;


JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField accountID = new JTextField();
JTextField amount = new JTextField();


button1 = new JButton("Create an Account");



JTextArea textArea = new JTextArea(ROWS, COLUMNS);
textArea.append("No account");
textArea.setEditable(true);

// .... etc

对此(请注意格式更改):

public class CreatePanel extends JPanel {
  public static final int FIELD_WIDTH = 10;
  public static final int ROWS = 50;
  public static final int COLUMNS = 50;

  private Vector accountList;
  private JButton button1;
  private TransferPanel transferPanel;
  private JTextField accountID = new JTextField();
  private JTextField amount = new JTextField();
  private JTextArea textArea = new JTextArea(ROWS, COLUMNS);

  public CreatePanel(Vector accountList, TransferPanel tPanel) {
    accountList = accountList;
    transferPanel = tPanel;

    JLabel label1 =new JLabel("Account ID: ");
    JLabel label2 = new JLabel("Amount: ");

    button1 = new JButton("Create an Account");

    textArea.append("No account");
    textArea.setEditable(true);

    // .... etc

现在 ButtonListener 可以访问 textArea 字段。

于 2013-10-01T21:49:06.303 回答
0

将您的getTextJTextFieldJTextArea append(str)结合起来;

于 2013-10-01T21:48:23.587 回答
0

我不知道我是否以某种方式误解了您的问题,但在您的 ButtonListener 类的 actionPerformed 方法中 - 您应该对传入的 ActionEvents 做出反应。

于 2013-10-01T21:48:58.923 回答