我的Key Listener无法正常工作

时间:2015-03-10 13:41:50

标签: java swing keylistener

我正在使用Swing制作一个小型计算器。注意:代码不完整,无法进行计算。 ActionListener一切顺利,但KeyListener没有。   我为此目的使用了KeyAdapter。 我尝试将setfocusable()方法添加到Text区域。但是,除了删除按钮之外,我仍然按下每个按键的消息对话框。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class Calculator{
JFrame frame;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
JPanel pane,pane1,pane2;
JSplitPane sp;
Font font1 = new Font("ALGERIAN", Font.PLAIN, 40);
Font font2 = new Font("Comic Sans MS", Font.PLAIN,20);
JTextArea txt,txt2;
double number1,number2,anumber,result;
double add, sub,mul,div;
int count=0,delete=0;
double[] allnos= new double[50];
public void run(){
    frame=new JFrame("Split Pane");
    frame.setSize(450,500);
    frame.setLocation(600,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(true);
//Panel designing
    pane=new JPanel();
    pane.setBackground(Color.darkGray);
    pane.setLayout(new FlowLayout(FlowLayout.LEFT));
    pane1=new JPanel();
    pane1.setBackground(Color.darkGray);
    sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,pane,pane1);
    sp.setOneTouchExpandable(true);


//Console Writing to txtArea
    txt=new JTextArea(2,29);
    txt.setEditable(false);
    txt.setFont(font2);
    txt.setLineWrap(true);
    txt.addKeyListener(new KeyHandler());

    txt2=new JTextArea(1,29);
    txt2.setEditable(false);
    txt2.setFont(font2);
    txt2.setLineWrap(true);
    txt2.setBackground(Color.lightGray);
    txt2.addKeyListener(new KeyHandler());
    PrintStream out=new PrintStream(new Console(txt));
    System.setOut(out);

//Adding Buttons And Listeners      
    b1=new JButton("1");
    b1.addActionListener(new ActionHandler());
    b1.setFont(font1);
    b2=new JButton("2");
    b2.addActionListener(new ActionHandler());
    b2.setFont(font1);
    b3=new JButton("3");
    b3.addActionListener(new ActionHandler());
    b3.setFont(font1);
    b4=new JButton("4");
    b4.addActionListener(new ActionHandler());
    b4.setFont(font1);
    b5=new JButton("5");
    b5.addActionListener(new ActionHandler());
    b5.setFont(font1);
    b6=new JButton("6");
    b6.addActionListener(new ActionHandler());
    b6.setFont(font1);
    b7=new JButton("7");
    b7.addActionListener(new ActionHandler());
    b7.setFont(font1);
    b8=new JButton("8");
    b8.addActionListener(new ActionHandler());
    b8.setFont(font1);
    b9=new JButton("9");
    b9.addActionListener(new ActionHandler());
    b9.setFont(font1);
    b10=new JButton("*");
    b10.addActionListener(new ActionHandler());
    b10.setFont(font1);
    b12=new JButton("0");
    b12.addActionListener(new ActionHandler());
    b12.setFont(font1);
    b11=new JButton("/");
    b11.addActionListener(new ActionHandler());
    b11.setFont(font1);
    b13=new JButton("+");
    b13.addActionListener(new ActionHandler());
    b13.setFont(font1);
    b14=new JButton("-");
    b14.addActionListener(new ActionHandler());
    b14.setFont(font1);
    b15=new JButton("=");
    b15.addActionListener(new ActionHandler());
    b15.setFont(font1);
    b16=new JButton("DEL");
    b16.addActionListener(new ActionHandler());
    b16.setFont(font1);

 //Adding To Panel
    pane1.add(b1);
    pane1.add(b2);
    pane1.add(b3);
    pane1.add(b4);
    pane1.add(b5);
    pane1.add(b6);
    pane1.add(b7);
    pane1.add(b8);
    pane1.add(b9);
    pane1.add(b10);
    pane1.add(b11);
    pane1.add(b12);
    pane1.add(b13);
    pane1.add(b14);
    pane1.add(b15);
    pane1.add(b16);
    pane.add(txt);
    pane.addKeyListener(new KeyHandler());
    pane.add(txt2,BorderLayout.NORTH);
    frame.add(sp);
//Set Layout to pane1
    GridLayout grd=new GridLayout(4,3);
    grd.setHgap(5);
    grd.setVgap(5);
    pane1.setLayout(grd);

}
//Adding Listeners to Buttons   
class ActionHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        try{
        if(e.getActionCommand().equals("1")){
            txt.append(b1.getText());
            txt2.append(b1.getText());
        }else if(e.getActionCommand().equals("2")){
            txt.append(b2.getText());
            txt2.append(b2.getText());
        }else if(e.getActionCommand().equals("3")){
            txt.append(b3.getText());
            txt2.append(b3.getText());
        }else if(e.getActionCommand().equals("4")){
            txt.append(b4.getText());
            txt2.append(b4.getText());
        }else if(e.getActionCommand().equals("5")){
            txt.append(b5.getText());
            txt2.append(b5.getText());
        }else if(e.getActionCommand().equals("6")){
            txt.append(b6.getText());
            txt2.append(b6.getText());
        }else if(e.getActionCommand().equals("7")){
            txt.append(b7.getText());
            txt2.append(b7.getText());
        }else if(e.getActionCommand().equals("8")){
            txt.append(b8.getText());
            txt2.append(b8.getText());
        }else if(e.getActionCommand().equals("9")){
            txt.append(b9.getText());
            txt2.append(b9.getText());
        }else if(e.getActionCommand().equals("*")){
            txt2.append(b10.getText());
            number1=numberread();
            add=0;
            sub=0;
            mul=1;
            div=0;
            txt.setText("");
        }else if(e.getActionCommand().equals("/")){
            txt2.append(b11.getText());
            number1=numberread();
            add=0;
            sub=0;
            mul=0;
            div=1;
            txt.setText("");
        }else if(e.getActionCommand().equals("0")){
            txt.append(b12.getText());
            txt2.append(b12.getText());
        }else if(e.getActionCommand().equals("+")){
            number1=numberread();
            count++;
            allnos[count]=number1;
            anumber=allnos[count]+anumber;
            txt2.append(b13.getText());
            add=1;
            sub=0;
            mul=0;
            div=0;
            txt.setText("");
        }else if(e.getActionCommand().equals("-")){
            txt2.append(b14.getText());
            number1=numberread();
            add=0;
            sub=1;
            txt.setText("");
        }
        if(e.getActionCommand().equals("=")){
            number2=numberread();
            if(add>0){
                result=anumber+number2;
                txt.setText(Double.toString(result));
            }
            if(sub>0){
                result=number1-number2;
                txt.setText(number1+"-"+number2+"="+Double.toString(result));
            }
            if(mul>0){
                result=number1*number2;
                txt.setText(number1+"*"+number2+"="+Double.toString(result));
            }
            if(div>0){
                if(number2!=0){
                result=number1/number2;
                txt.setText(number1+"/"+number2+"="+Double.toString(result));
            }else if(number2==0){
                System.out.println("Can't divide from 0");
            }
            }

        }else if(e.getActionCommand().equals("DEL")){
            txt.setText("");
            txt2.setText("");
            count=0;
            anumber=0;
        }
    }catch(NumberFormatException Ee){
        txt.setText("Error!");
        int o=JOptionPane.showConfirmDialog(frame,"Note: The Calculator is not      multitasking\n"+"Click Help Button if you are getting an error.","May I Help you",    JOptionPane.OK_OPTION);
        if(o==JOptionPane.OK_OPTION){
            txt.setText("");
            txt2.setText("");
        }else{
            txt.setText("");
            txt2.setText("");
        }
}
    }
}
class KeyHandler extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        if(e.getKeyChar()==KeyEvent.VK_NUMPAD1){
            txt.append(b1.getText());
            txt2.append(b1.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD2){
            txt.append(b2.getText());
            txt2.append(b2.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD3){
            txt.append(b3.getText());
            txt2.append(b3.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD4){
            txt.append(b4.getText());
            txt2.append(b4.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD5){
            txt.append(b5.getText());
            txt2.append(b5.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD6){
            txt.append(b6.getText());
            txt2.append(b6.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD7){
            txt.append(b7.getText());
            txt2.append(b7.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD8){
            txt.append(b8.getText());
            txt2.append(b8.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD9){
            txt.append(b9.getText());
            txt2.append(b9.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_NUMPAD0){
            txt.append(b12.getText());
            txt2.append(b12.getText());
        }else if(e.getKeyChar()==KeyEvent.VK_ASTERISK){
            txt2.append(b10.getText());
            number1=numberread();
            add=0;
            sub=0;
            mul=1;
            div=0;
            txt.setText("");
        }else if(e.getKeyChar()==KeyEvent.VK_DIVIDE){
            txt2.append(b11.getText());
            number1=numberread();
            add=0;
            sub=0;
            mul=0;
            div=1;
            txt.setText("");
        }else if(e.getKeyChar()==KeyEvent.VK_ADD){
            number1=numberread();
            count++;
            allnos[count]=number1;
            anumber=allnos[count]+anumber;
            txt2.append(b13.getText());
            add=1;
            sub=0;
            mul=0;
            div=0;
            txt.setText("");
        }else if(e.getKeyChar()==KeyEvent.VK_SUBTRACT){
            txt2.append(b14.getText());
            number1=numberread();
            add=0;
            sub=1;
            txt.setText("");
        }
        if(e.getKeyChar()==KeyEvent.VK_ENTER){
            number2=numberread();
            if(add>0){
                result=anumber+number2;
                txt.setText(Double.toString(result));
            }
            if(sub>0){
                result=number1-number2;
                txt.setText(number1+"-"+number2+"="+Double.toString(result));
            }
            if(mul>0){
                result=number1*number2;
                txt.setText(number1+"*"+number2+"="+Double.toString(result));
            }
            if(div>0){
                if(number2!=0){
                result=number1/number2;
                txt.setText(number1+"/"+number2+"="+Double.toString(result));
            }else if(number2==0){
                System.out.println("Can't divide from 0");
            }
            }

        }else if(e.getKeyChar()==KeyEvent.VK_DELETE){
            txt.setText("");
            txt2.setText("");
            count=0;
            anumber=0;
        }else{
            JOptionPane.showMessageDialog(frame, "Enter a Valid Key");
        }
    }
}
public double numberread(){
    double num1;
    String s=txt.getText();
    num1=Integer.valueOf(s);
    return num1;
}

public static void main(String[] args) {
    Calculator pan=new Calculator();
    pan.run();
}

}

0 个答案:

没有答案