簡體   English   中英

Java中的動作監聽器

[英]Action Listeners in Java

我有一個使用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

建議:

  • 首先,請努力格式化您的代碼。 如果格式不正確(例如您當前正在顯示的隨機百搭縮進),我們將無法很好地理解您的代碼,並且您經常會出錯。 每個代碼塊應縮進相同的數量,我通常使用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字段。

JTextField getTextJTextArea結合使用(str) ;

我不知道我是否在某種程度上誤解了你的問題,但是在ButtonListener類的actionPerformed方法中 - 你應該對傳入的ActionEvents做出反應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM