使用java中的MVC设置KeyListeners

时间:2014-01-29 15:04:27

标签: java swing model-view-controller jframe keylistener

我正在尝试用Java编写自己的计算器。我认为最好为我的代码实现MVC(模型视图控制器)设计。我有一些基础工作,计算器确实工作,问题是我无法弄清楚我在哪里听错键的实现。目前,我可以使用动作侦听器单击按钮并使用数值更新字段,并使用按钮进行加,减,乘,除以及清除。所以我现在真正想到的唯一一件事是试图允许用户(我自己)选择使用键盘上的数字键盘将值附加到字段中,无论如何这里是我的代码。

此视图

    package Calculator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

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

public class CalcFrame extends JFrame{


    private Dimension d = new Dimension(300,300);
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JButton equals = new JButton("=");
    JButton addBtn = new JButton("+");
    JButton subtractBtn = new JButton("-");
    JButton multiplyBtn = new JButton("*");
    JButton divideBtn = new JButton("/");
    JTextArea field = new JTextArea(1,20);
    JButton numBtn[] = new JButton[11];
    String numTxt[] = {"0","1","2","3","4","5","6","7","8","9","C"}; 
    Color newColor = new Color(10,70,40);
    int x = 50, y = 0;
    public CalcFrame(){
        this.setSize(d);
        this.setResizable(false);
    //  frame.setVisible(true);
        this.setTitle("Marks Calculator");
        this.setIconImage(new ImageIcon(this.getClass().getResource("emblem.png")).getImage());

        for(int i = 0; i < numBtn.length; i++){
            numBtn[i] = new JButton(numTxt[i]);
            numBtn[i].setSize(50, 30);

        }
        for(int i = 0; i <numBtn.length; i++){
            numBtn[0].setLocation(10,180);
            numBtn[1].setLocation(10,140);
            numBtn[2].setLocation(65,140);
            numBtn[3].setLocation(120,140);
            numBtn[4].setLocation(175,140);
            numBtn[5].setLocation(10,100);
            numBtn[6].setLocation(65,100);
            numBtn[7].setLocation(120,100);
            numBtn[8].setLocation(175,100);
            numBtn[9].setLocation(10,60);
            numBtn[10].setLocation(175,20);
            panel2.add(numBtn[i]);

        }
        field.setLocation(10, 10);
        field.setSize(280,30);
        field.setEditable(false);
        field.setFocusable(true);
        panel1.setSize(300, 50);
        panel1.setLayout(null);
        this.add(panel1);

        panel2.setSize(300, 250);
        panel2.setBackground(newColor);
        panel2.setLocation(0, 51);
        panel2.setLayout(null);
        this.add(panel2);

        equals.setLocation(230,180);
        equals.setSize(50, 30);
        panel2.add(equals);


        addBtn.setLocation(230, 140);
        addBtn.setSize(50,30);      
        panel2.add(addBtn);

        subtractBtn.setLocation(230, 100);
        subtractBtn.setSize(50,30);     
        panel2.add(subtractBtn);

        multiplyBtn.setLocation(230, 60);
        multiplyBtn.setSize(50,30);     
        panel2.add(multiplyBtn);

        divideBtn.setLocation(230, 20);
        divideBtn.setSize(50,30);       
        panel2.add(divideBtn);


        panel1.add(field);
        this.setLocationRelativeTo(rootPane);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    public void addKeyL(KeyListener keyp){
        addKeyListener(keyp);
    }

    public void addListener(ActionListener listener){



    addBtn.addActionListener(listener);
    subtractBtn.addActionListener(listener);
    equals.addActionListener(listener);
    multiplyBtn.addActionListener(listener);
    divideBtn.addActionListener(listener);

    for(int i = 0; i < numBtn.length; i++){
        numBtn[i].addActionListener(listener);
    }



    }

    public int getFieldText(){

        return Integer.parseInt(field.getText());
    }
public void setFieldText(){

        field.setText("");
    }
    public void setAnswer(int solution){

        field.setText(Integer.toString(solution));
    }

}

这是模型

package Calculator;

public class Calculations {

    private int total;

    public void addNumbers(int a, int b){

        total = a + b;
    }

    public void subtractNumbers(int a, int b){

        total = a - b;
    }

    public void multiplyNumbers(int a, int b){

        total = a * b;
    }

    public void divideNumbers(int a, int b){

        total = a / b;
    }
    public int getTotal(){
        return total;
    }

}

这是控制器

package Calculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class KeyEvents{

    private boolean isAdd = false;
    private boolean isSubtract = false;
    private boolean isDivide = false;
    private boolean isMultiply = false;
    private CalcFrame view = new CalcFrame();
    private Calculations model = new Calculations();
    int a = 0, b = 0, answer;
    int counter = 0;
    public KeyEvents(CalcFrame view, Calculations model){
        this.view = view;
        this.model = model;

        this.view.addListener(new CalcListener());
        this.view.addKeyL(new CalcListener());
    }



    class CalcListener implements ActionListener, KeyListener{

        @Override
        public void actionPerformed(ActionEvent e) {

                if(e.getSource().equals(view.addBtn)){
                    a = view.getFieldText();
                    view.field.setText("");
                    isAdd = true;
                    isSubtract = false;
                    isDivide = false;
                    isMultiply = false;

                }   


                if(e.getSource().equals(view.numBtn[0])){
                     view.field.append("0");                     
                }

                if(e.getSource().equals(view.numBtn[1])){
                    view.field.append("1");                 
                }

                if(e.getSource().equals(view.numBtn[2])){
                    view.field.append("2");
                }

                if(e.getSource().equals(view.numBtn[3])){
                    view.field.append("3");
                }

                if(e.getSource().equals(view.numBtn[4])){
                    view.field.append("4");
                }

                if(e.getSource().equals(view.numBtn[5])){
                    view.field.append("5");
                }

                if(e.getSource().equals(view.numBtn[6])){
                    view.field.append("6");;
                }

                if(e.getSource().equals(view.numBtn[7])){
                    view.field.append("7");
                }

                if(e.getSource().equals(view.numBtn[8])){
                    view.field.append("8");
                }

                if(e.getSource().equals(view.numBtn[9])){
                    view.field.append("9");
                }
                if(e.getSource().equals(view.numBtn[10])){
                    view.field.setText("");
                }

                if(e.getSource().equals(view.subtractBtn)){
                    a = view.getFieldText();
                    view.field.setText("");
                    isAdd = false;
                    isSubtract = true;
                    isDivide = false;
                    isMultiply = false;
                }   

                if(e.getSource().equals(view.multiplyBtn)){
                    a = view.getFieldText();
                    view.field.setText("");
                    isAdd = false;
                    isSubtract = false;
                    isDivide = false;
                    isMultiply = true;
                }   

                if(e.getSource().equals(view.divideBtn)){
                    a = view.getFieldText();
                    view.field.setText("");
                    isAdd = false;
                    isSubtract = false;
                    isDivide = true;
                    isMultiply = false;
                }   

                if(e.getSource().equals(view.equals)){
                    b = view.getFieldText();

                    if(isAdd == true){
                        view.setFieldText();
                        model.addNumbers(a, b);             
                        view.setAnswer(model.getTotal());
                    }

                    if(isSubtract == true){
                        view.setFieldText();
                        model.subtractNumbers(a, b);
                        view.setAnswer(model.getTotal());
                    }

                    if(isMultiply == true){
                        view.setFieldText();
                        model.multiplyNumbers(a, b);
                        view.setAnswer(model.getTotal());
                    }

                    if(isDivide == true){
                        view.setFieldText();
                        model.divideNumbers(a, b);
                        view.setAnswer(model.getTotal());
                    }

            }
        }

        @Override
        public void keyTyped(KeyEvent e) {
            if(e.getSource().equals(KeyEvent.VK_0)){
                System.out.println("sjkdhlkj");
            }
            if(e.getSource().equals(KeyEvent.VK_1)){
                view.field.append("1");
            }
            if(e.getSource().equals(KeyEvent.VK_2)){
                view.field.append("2");
            }
            if(e.getSource().equals(KeyEvent.VK_3)){
                view.field.append("3");
            }
            if(e.getSource().equals(KeyEvent.VK_4)){
                view.field.append("4");
            }
            if(e.getSource().equals(KeyEvent.VK_5)){
                view.field.append("5");
            }
            if(e.getSource().equals(KeyEvent.VK_6)){
                view.field.append("6");
            }
            if(e.getSource().equals(KeyEvent.VK_7)){
                view.field.append("7");
            }
            if(e.getSource().equals(KeyEvent.VK_8)){
                view.field.append("8");
            }
            if(e.getSource().equals(KeyEvent.VK_9)){
                view.field.append("9");
            }

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getSource().equals(KeyEvent.VK_0)){
                System.out.println("sjkdhlkj");
            }
            if(e.getSource().equals(KeyEvent.VK_1)){
                view.field.append("1");
            }
            if(e.getSource().equals(KeyEvent.VK_2)){
                view.field.append("2");
            }
            if(e.getSource().equals(KeyEvent.VK_3)){
                view.field.append("3");
            }
            if(e.getSource().equals(KeyEvent.VK_4)){
                view.field.append("4");
            }
            if(e.getSource().equals(KeyEvent.VK_5)){
                view.field.append("5");
            }
            if(e.getSource().equals(KeyEvent.VK_6)){
                view.field.append("6");
            }
            if(e.getSource().equals(KeyEvent.VK_7)){
                view.field.append("7");
            }
            if(e.getSource().equals(KeyEvent.VK_8)){
                view.field.append("8");
            }
            if(e.getSource().equals(KeyEvent.VK_9)){
                view.field.append("9");
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getSource().equals(KeyEvent.VK_0)){
                System.out.println("sjkdhlkj");
            }
            if(e.getSource().equals(KeyEvent.VK_1)){
                view.field.append("1");
            }
            if(e.getSource().equals(KeyEvent.VK_2)){
                view.field.append("2");
            }
            if(e.getSource().equals(KeyEvent.VK_3)){
                view.field.append("3");
            }
            if(e.getSource().equals(KeyEvent.VK_4)){
                view.field.append("4");
            }
            if(e.getSource().equals(KeyEvent.VK_5)){
                view.field.append("5");
            }
            if(e.getSource().equals(KeyEvent.VK_6)){
                view.field.append("6");
            }
            if(e.getSource().equals(KeyEvent.VK_7)){
                view.field.append("7");
            }
            if(e.getSource().equals(KeyEvent.VK_8)){
                view.field.append("8");
            }
            if(e.getSource().equals(KeyEvent.VK_9)){
                view.field.append("9");
            }           
        }
    }   
}

这是tl; dr,我无法让keyListener正常工作,我已经尝试将keyListener分配给字段,panel1,panel2和this。 seperately。一如既往地感谢您的帮助。

〜UPDATE〜 我决定试试GitHub,然后把我的代码放到它上面。我希望它能让我的代码更容易理解,甚至可以分叉,这样一个人就可以搞砸了。 https://github.com/niroshido/TestCalculator/tree/master/Calculator

2 个答案:

答案 0 :(得分:1)

使用Key Bindings。一个简单的例子:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(50, 50) );
            buttonPanel.add( button );

            KeyStroke pressed = KeyStroke.getKeyStroke(text);
            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(pressed, text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
//      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

答案 1 :(得分:0)

您需要确保要添加KeyListener的任何组件都具有焦点,或者您应该使用键绑定。