2013-10-01 5 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는 텍스트 영역 필드에 액세스 할 수 있습니다

그래서이를 변경합니다.