我有一个使用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
答案 0 :(得分:2)
建议:
所以改变这个:
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字段。
答案 1 :(得分:0)
将JTextField
的{{3}}与JTextArea
getText合并;
答案 2 :(得分:0)
我不知道我是否在某种程度上误解了你的问题,但是在ButtonListener类的actionPerformed方法中 - 你应该对传入的ActionEvents做出反应。