2012-02-23 2 views
1

나는이 키보드 시뮬레이터에서 작업하고 있지만 GUI가 비교적 새로운 편이며 버튼의 기능을 수행하기 위해 ActionListener를 추가하려고하는 지점에 갇혀있다. 버튼을 누를 때마다 입력 영역. 미리 감사드립니다.GUI 키보드 시뮬레이터

import java.awt.BorderLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 

public class StockTicker extends JFrame implements ActionListener 
{ 
String firstRow[] = {"1","2","3","4","5","6","7","8","9","0"}; 
String secondRow[] = {"Q","W","E","R","T","Y","U","I","O","P","Del"}; 
String thirdRow[] = {"A","S","D","F","G","H","J","K","L","Return"}; 
String fourthRow[] = {"Z","X","C","V","B","N","M","."}; 


JButton first[]; 

JButton second[]; 

JButton third[]; 

JButton fourth[]; 





public StockTicker() 
{ 
    super ("A Simple Stock Market GUI"); 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setResizable(false); 

    initWidgets(); 
} 


private void initWidgets() 
{ 


    JLabel jlOutput = new JLabel("Output: "); 
    JLabel jlInput = new JLabel("Intput: "); 
    final JLabel jtfOutput = new JLabel(""); 
    final JLabel jtfInput = new JLabel(); 

    JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.weightx = 1.0; 
     gbc.weighty = 0.1; 
     gbc.anchor = GridBagConstraints.PAGE_START; 
     JLabel bottomLabel = new JLabel(" Input : ", JLabel.LEFT); 


    setLayout(new BorderLayout()); 
    JPanel jpNorth = new JPanel(); 
    JPanel jpCenter = new JPanel(); 
    JPanel jpKeyboard = new JPanel(); 

    add(jpNorth, BorderLayout.NORTH); 
    add(jpCenter, BorderLayout.CENTER); 
    add(jpKeyboard, BorderLayout.SOUTH); 


    jpNorth.setLayout(new BorderLayout()); 
    jpNorth.add(jlOutput, BorderLayout.WEST); 
    jpNorth.add(jtfOutput, BorderLayout.SOUTH); 

    jpCenter.setLayout(new BorderLayout()); 
    jpCenter.add(jlInput, BorderLayout.WEST); 
    jpCenter.add(jtfInput, BorderLayout.CENTER); 

    jpKeyboard.setLayout(new GridLayout(4,1)); 


    pack(); 


    first = new JButton[firstRow.length]; 

    JPanel p = new JPanel(new GridLayout(1, firstRow.length)); 

    for(int i = 0; i < firstRow.length; ++i) 
    { 

     first[i] = new JButton(firstRow[i]); 
     p.add(first[i]); 


    } 

    jpKeyboard.add(p); 



    second = new JButton[secondRow.length]; 

    p = new JPanel(new GridLayout(1, secondRow.length)); 

    for(int i = 0; i < secondRow.length; ++i) 
    { 

     second[i] = new JButton(secondRow[i]); 
     p.add(second[i]); 



    } 

    jpKeyboard.add(p); 

    third = new JButton[thirdRow.length]; 

    p = new JPanel(new GridLayout(1, thirdRow.length)); 
    for(int i = 0; i < thirdRow.length; ++i) 
    { 

     third[i] = new JButton(thirdRow[i]); 
     p.add(third[i]); 


    } 
    jpKeyboard.add(p); 

    fourth = new JButton[fourthRow.length]; 

    p = new JPanel(new GridLayout(1, fourthRow.length)); 
    for(int i = 0; i < fourthRow.length; ++i) 
    { 

     fourth[i] = new JButton(fourthRow[i]); 
     p.add(fourth[i]); 

    } 
    jpKeyboard.add(p); 

    }  
+2

그리고 당신의 문제가 무엇과 같은 일을 할 것인가? – mre

+0

버튼에 actionlistener를 추가하고 실제로 작동하게하는 방법을 모르겠습니다. – lch

답변

0

JButton.addActionListener(listener);

그래서 당신은

MyActionListener listener = new MyActionListener(); 

. 
. 
. 

first[i] = new JButton(firstRow[i]); 
first[i].setActionCommand(firstRow[i]); 
first[i].addActionListener(listener); 

. 
. 
. 

public class MyActionListener implements ActionListener { 

    public void actionPerformed(ActionEvent evt) { 

     String cmd = evt.getActionCommand(); 

     // append the cmd value to your out put... 

    } 

}