2010-03-19 6 views
2

아래의 계산기 인터페이스 코드를 참조하십시오. 초보자 시점에서 "1"이 눌려 졌을 때 표시되어야하지만 분명히 잘못된 것을하고 있습니다. 어떤 제안이라도?자바 리스너에서 액션 리스너가 응답하지 않습니다

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.event.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


/** 
*A Class that operates as the framework for a calculator. 
*No calculations are performed in this section 
*/ 
public class CalcFrame extends JPanel 
{ 
    private CalcEngine calc; 

    private JFrame frame; 
    private JTextField display; 
    private JLabel status; 

    /** 
    * Constructor for objects of class GridLayoutExample 
    */ 
    //public CalcFrame(CalcEngine engine) 
    //{ 

     //frame.setVisible(true); 
     // calc = engine; 
     // makeFrame(); 

    //} 
    public CalcFrame() { 
    makeFrame(); 
    calc = new CalcEngine(); 
} 
class ButtonListener implements ActionListener { 
    ButtonListener() { 
    } 

    public void actionPerformed(ActionEvent e) { 
    if (e.getActionCommand().equals("1")) { 
     System.out.println("1"); 
    } 
    } 
} 




    /** 
    * This allows you to quit the calculator. 
    */ 

    // Alows the class to quit. 
    private void quit() 
    { 
     System.exit(0); 


    } 

    // Calls the dialog frame with the information about the project. 
    private void showAbout() 
    { 
     JOptionPane.showMessageDialog(frame, 
        "Declan Hodge and Tony O'Keefe Group Project", 
        "About Calculator Group Project", 
        JOptionPane.INFORMATION_MESSAGE); 
    } 


     // ---- swing stuff to build the frame and all its components ---- 

    /** 
    * The following creates a layout of the calculator frame. 
    */ 
    private void makeFrame() 
    { 
     frame = new JFrame("Group Project Calculator"); 
     makeMenuBar(frame); 

     JPanel contentPane = (JPanel)frame.getContentPane(); 
     contentPane.setLayout(new BorderLayout(8, 8)); 
     contentPane.setBorder(new EmptyBorder(10, 10, 10, 10)); 





     /** 
    * Insert a text field 
    */ 
     display = new JTextField(8); 
     contentPane.add(display, BorderLayout.NORTH); 

     //Container contentPane = frame.getContentPane(); 
     contentPane.setLayout(new GridLayout(4, 5)); 
     JPanel buttonPanel = new JPanel(new GridLayout(4, 4)); 
     contentPane.add(new JButton("9")); 
     contentPane.add(new JButton("8")); 
     contentPane.add(new JButton("7")); 
     contentPane.add(new JButton("6")); 
     contentPane.add(new JButton("5")); 
     contentPane.add(new JButton("4")); 
     contentPane.add(new JButton("3")); 
     contentPane.add(new JButton("2")); 
     contentPane.add(new JButton("1")); 
     contentPane.add(new JButton("0")); 
     contentPane.add(new JButton("+")); 
     contentPane.add(new JButton("-")); 
     contentPane.add(new JButton("/")); 
     contentPane.add(new JButton("*")); 
     contentPane.add(new JButton("=")); 
     contentPane.add(new JButton("C")); 
     contentPane.add(new JButton("CE")); 
     contentPane.add(new JButton("%")); 
     contentPane.add(new JButton("#")); 
     //contentPane.add(buttonPanel, BorderLayout.CENTER); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    /** 
    * Create the main frame's menu bar. 
    * The frame that the menu bar should be added to. 
    */ 










    private void makeMenuBar(JFrame frame) 
    { 
     final int SHORTCUT_MASK = 
      Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); 


     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     JMenu menu; 
     JMenuItem item; 

     // create the File menu 
     menu = new JMenu("File"); 
     menubar.add(menu); 






     // create the Quit menu with a shortcut "Q" key. 
     item = new JMenuItem("Quit"); 
      item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK)); 
      item.addActionListener(new ActionListener() { 
           public void actionPerformed(ActionEvent e) { quit(); } 
          }); 
     menu.add(item); 

     // Adds an about menu. 
     menu = new JMenu("About"); 
     menubar.add(menu); 

     // Displays 
     item = new JMenuItem("Calculator Project"); 
      item.addActionListener(new ActionListener() { 
           public void actionPerformed(ActionEvent e) { showAbout(); } 
          }); 
     menu.add(item); 

    } 

} 
+0

같은 질문에서 분명히 같은 질문이 질문의 중복처럼 보입니다. http://stackoverflow.com/questions/2470793/simple-2-class-java-calculator-doesnt-accept-inputs-or-do-calculations. 문제가 이미 해결되었습니다. 그건 그렇고 숙제 같아 보이지? – jfpoilpret

답변

3

많은 코드가 있습니다.

실제로 버튼 하나를 추가하지 않고 ButtonListener을 실제로 만들지는 않습니다.

+0

와우 톰! 정말 도움이됩니다! – tokee

2

단추로 동작 수신기를 등록해야합니다.

//Step 1. 
JButton b1 = new JButton("1"); 

//Step2 register 
b1.addActionListener(new ButtonListener()); 

상기 단계 1에서 선언하여 버튼 EDIT

시작. 그런 다음 컨텐츠 분할 창에서 단추를 지금 추가하는 f}과 유사한 단추를 추가해야합니다.

contentPane.add(b1); 

이제 버튼이 표시됩니다.

+0

감사합니다 vincent, 유일한 문제는 정확히 위의 코드를 추가 할 때 문제가 컴파일되지만 단추가 표시되지 않습니다? tony – tokee

+0

@tokee 내가 제공 한 코드가 완전하지 않습니다. 대답을 조금 편집하고 좀 더 완전한 예를 들어 보겠습니다. –

+0

다시 한번 감사 드리고 싶습니다.하지만 "1"을 누르면 터미널 창에서 열리고 계산기 화면에 표시되지 않습니다. 그 이유는 무엇입니까? 귀하의 도움을 주셔서 감사합니다, 이미 명확하지 않은 경우에 대비하여이 물건을 처음 접했습니다. tony – tokee

관련 문제