-4

主要问题:当我使用 . 创建一个挪威计算器时KeyListener,它不能正常工作。

我已经添加了对 JFrameaddKeyListener()方法的调用,并且我已经实现了KeyListener接口和所有内容。但它仍然不起作用。我为糟糕的代码道歉。

任何帮助表示赞赏。提前致谢。

代码:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;  
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;


public class miniräknare extends JFrame implements KeyListener {
public static int answer = 0;
public JLabel lblRandomnum;
public static int answer2 = 0;
public static boolean flip = true;
public static JLabel lblOutput;
Random myRandom = new Random();
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    new miniräknare();

}


public miniräknare() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 300, 600);
    setResizable(false);
    getContentPane().setLayout(null);
    lblOutput = new JLabel("" + answer);
    lblOutput.setBounds(35, 33, 222, 37);
    getContentPane().add(lblOutput);
    JButton button = new JButton("1");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        increment(1);
        lblRandomnum.setText("");
        }
    });
    button.setBounds(35, 111, 75, 75);
    getContentPane().add(button);

    JButton button_1 = new JButton("2");
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(2);
            lblRandomnum.setText("");
        }
    });
    button_1.setBounds(109, 111, 75, 75);
    getContentPane().add(button_1);

    JButton button_2 = new JButton("3");
    button_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(3);
            lblRandomnum.setText("");
        }
    });
    button_2.setBounds(182, 111, 75, 75);
    getContentPane().add(button_2);

    JButton button_3 = new JButton("4");
    button_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(4);
            lblRandomnum.setText("");
        }
    });
    button_3.setBounds(35, 184, 75, 75);
    getContentPane().add(button_3);

    JButton button_4 = new JButton("5");
    button_4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(5);
            lblRandomnum.setText("");
        }
    });
    button_4.setBounds(109, 184, 75, 75);
    getContentPane().add(button_4);

    JButton button_5 = new JButton("6");
    button_5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        increment(6);
        lblRandomnum.setText("");
        }
    });
    button_5.setBounds(182, 184, 75, 75);
    getContentPane().add(button_5);

    JButton button_6 = new JButton("7");
    button_6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(7);
            lblRandomnum.setText("");
        }
    });
    button_6.setBounds(35, 257, 75, 75);
    getContentPane().add(button_6);

    JButton button_7 = new JButton("8");
    button_7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(8);
            lblRandomnum.setText("");
        }
    });
    button_7.setBounds(109, 257, 75, 75);
    getContentPane().add(button_7);

    JButton button_8 = new JButton("0");
    button_8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(0);
            lblRandomnum.setText("");
        }
    });
    button_8.setBounds(109, 331, 75, 75);
    getContentPane().add(button_8);

    JButton button_9 = new JButton("9");
    button_9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            increment(9);
            lblRandomnum.setText("");
        }
    });
    button_9.setBounds(182, 257, 75, 75);
    getContentPane().add(button_9);

    JButton btnC = new JButton("C");
    btnC.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            answer = 0;
            answer2 = 0;
            flip = true;
            lblOutput.setText("" + answer);
            lblRandomnum.setText("");
        }
    });
    btnC.setBounds(35, 331, 75, 75);
    getContentPane().add(btnC);

    JButton button_10 = new JButton("=");
    button_10.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        lblOutput.setText("tänker.........");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lblOutput.setText("Detta tal är för advancerat :-(");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lblRandomnum.setText("Men, jag tror det blir " + myRandom.nextInt(100000));
        }
    });
    button_10.setBounds(182, 331, 75, 75);
    getContentPane().add(button_10);

    JButton button_11 = new JButton("+");
    button_11.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if((lblOutput.getText().endsWith("+")) || (lblOutput.getText().endsWith("-")) || (lblOutput.getText().endsWith("x")) || (lblOutput.getText().endsWith("%"))) {
                lblOutput.setText("" + answer + "+");
            }
            else lblOutput.setText(lblOutput.getText() + "+");
            if(flip) {
                lblOutput.setText("+");
                flip = false;
            }
            else if(!flip) {
                flip = true;
            }
        }
    });
    button_11.setBounds(35, 405, 75, 75);
    getContentPane().add(button_11);

    JButton button_12 = new JButton("-");
    button_12.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if((lblOutput.getText().endsWith("+")) || (lblOutput.getText().endsWith("-")) || (lblOutput.getText().endsWith("x")) || (lblOutput.getText().endsWith("%"))) {
                lblOutput.setText("" + answer + "-");
            }
            else lblOutput.setText(lblOutput.getText() + "-");
            if(flip) {
                lblOutput.setText("-");
                flip = false;
            }
            else if(!flip) {
                flip = true;
            }
        }
    });
    button_12.setBounds(109, 405, 75, 75);
    getContentPane().add(button_12);

    JButton btnX = new JButton("X");
    btnX.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if((lblOutput.getText().endsWith("+")) || (lblOutput.getText().endsWith("-")) || (lblOutput.getText().endsWith("x")) || (lblOutput.getText().endsWith("%"))) {
                lblOutput.setText("" + answer + "x");
            }
            else lblOutput.setText(lblOutput.getText() + "x");
            if(flip) {
                lblOutput.setText("x");
                flip = false;
            }
            else if(!flip) {
                flip = true;
            }
        }
    });
    btnX.setBounds(182, 405, 75, 75);
    getContentPane().add(btnX);

    JButton button_13 = new JButton("%");
    button_13.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if((lblOutput.getText().endsWith("+")) || (lblOutput.getText().endsWith("-")) || (lblOutput.getText().endsWith("x")) || (lblOutput.getText().endsWith("%"))) {
                lblOutput.setText("" + answer + "%");
            }
            else lblOutput.setText(lblOutput.getText() + "%");
            if(flip) {
                lblOutput.setText("%");
                flip = false;
            }
            else if(!flip) {
                flip = true;
            }
        }
    });
    button_13.setBounds(109, 479, 75, 75);
    getContentPane().add(button_13);

    lblRandomnum = new JLabel("randomnum");
    lblRandomnum.setBounds(45, 82, 212, 16);
    getContentPane().add(lblRandomnum);
    lblRandomnum.setText("");
    setVisible(true);
    setTitle("norsk miniräknare");
    addKeyListener(this);
}

public static void increment(int i) {
    if(flip) {
    String s = "" + answer;
    StringBuilder str = new StringBuilder(s);
    if(s.startsWith("0")) {
        answer = i;
    } else {
    str.insert(s.length(), "" + i);
    s = str.toString(); 

    if(s.length() != 10) answer = Integer.parseInt(s);
    }
    lblOutput.setText("" + answer);
    }
    if(!flip) {
        String s = "" + answer2;
        StringBuilder str = new StringBuilder(s);
        if(s.startsWith("0")) {
            answer2 = i;
        } else {
        str.insert(s.length(), "" + i);
        s = str.toString();
        answer2 = Integer.parseInt(s);
        }
        lblOutput.setText("" + answer2);
        }
}


@Override
public void keyPressed(KeyEvent e) {
    System.out.println("lol");
    int KeyCode = e.getKeyCode();
    if(KeyCode == KeyEvent.VK_0) increment(0);
    if(KeyCode == KeyEvent.VK_1) increment(1);
    if(KeyCode == KeyEvent.VK_2) increment(2);
    if(KeyCode == KeyEvent.VK_3) increment(3);
    if(KeyCode == KeyEvent.VK_4) increment(4);
    if(KeyCode == KeyEvent.VK_5) increment(5);
    if(KeyCode == KeyEvent.VK_6) increment(6);
    if(KeyCode == KeyEvent.VK_7) increment(7);
    if(KeyCode == KeyEvent.VK_8) increment(8);
    if(KeyCode == KeyEvent.VK_9) increment(9);
}


@Override
public void keyReleased(KeyEvent e) {
    System.out.println("trololol");

}


@Override
public void keyTyped(KeyEvent e) {
    System.out.println("ofta");
}
}
4

1 回答 1

2

不要将 KeyListener 添加到 JFrame。KeyListener 仅在它们被添加到具有焦点的组件时才起作用,而这几乎不是 JFrame。事实上,对于 Swing 应用程序,您应该完全避免使用 KeyListeners,而是使用 Key Bindings,因为它们可以解决焦点问题并允许您使用可重用的 Actions。

请查看键绑定教程:这里

另外顺便说一句:您不应该使用空布局并设置边界,因为它会使程序难以调试和扩展。相反,请考虑使用布局管理器将一组 JButtons 放置到您的 GUI 中。GridLayout 可以很好地用于保存 JButton 的 JPanel。

于 2013-01-12T23:01:41.763 回答