2013-10-01 80 views
0

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

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 
+0

你已经删除了所有你的问题无法解决的相关代码 - 为什么?我已经将它恢复到之前的状态,以便它对我们更有意义。 –

回答

0

我不知道如果我误解你的问题,但好歹你ButtonListener类的actionPerformed方法中 - 你应该反应,这是在通过是ActionEvent

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的领域。