在Java中为JButton分配键盘键

时间:2018-06-27 20:08:06

标签: java eclipse swing jbutton

im试图构建一个带有GUI的计算器程序。我如何才能做到这一点,JButton(可以说在计算器显示屏上添加数字7的按钮“ 7”)不仅可以在您单击它时起作用,而且还可以在您按下键盘上的按钮时起作用(因此您可以按下按钮“ 7”还是按键盘上的7)?请只菜鸟友好的答案:)

这是我的代码:

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Kalkulator extends JFrame {
    private JTextField txtDisplay;
    public Kalkulator() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        txtDisplay = new JTextField();
        txtDisplay.setFont(new Font("Tahoma", Font.PLAIN, 22));
        txtDisplay.setHorizontalAlignment(SwingConstants.RIGHT);
        txtDisplay.setBounds(12, 13, 341, 77);
        getContentPane().add(txtDisplay);
        txtDisplay.setColumns(10);

        JButton btnOne = new JButton("1");
        btnOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnOne.getText();
                          txtDisplay.setText(EnterNumber);
            }
        });
        btnOne.setBounds(12, 380, 70, 61);
        getContentPane().add(btnOne);

        JButton btnTwo = new JButton("2");
        btnTwo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnTwo.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnTwo.setBounds(113, 380, 70, 61);
        getContentPane().add(btnTwo);

        JButton btnThree = new JButton("3");
        btnThree.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnThree.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnThree.setBounds(216, 380, 70, 61);
        getContentPane().add(btnThree);

        JButton btnFour = new JButton("4");
        btnFour.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
            }
        });
        btnFour.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnFour.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnFour.setBounds(12, 291, 70, 61);
        getContentPane().add(btnFour);

        JButton btnFive = new JButton("5");
        btnFive.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnFive.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnFive.setBounds(113, 291, 70, 61);
        getContentPane().add(btnFive);

        JButton btnSix = new JButton("6");
        btnSix.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnSix.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnSix.setBounds(216, 291, 70, 61);
        getContentPane().add(btnSix);

        JButton btnSeven = new JButton("7");
        btnSeven.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnSeven.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnSeven.setBounds(12, 195, 70, 61);
        getContentPane().add(btnSeven);

        JButton btnEight = new JButton("8");
        btnEight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnEight.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnEight.setBounds(113, 195, 70, 61);
        getContentPane().add(btnEight);

        JButton btnNine = new JButton("9");
        btnNine.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnNine.getText();
                  txtDisplay.setText(EnterNumber);
            }
        });
        btnNine.setBounds(216, 195, 70, 61);
        getContentPane().add(btnNine);

        JButton btnZero = new JButton("0");
        btnZero.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String EnterNumber = txtDisplay.getText() + btnZero.getText();
                  txtDisplay.setText(EnterNumber);
                }
        });
        btnZero.setBounds(113, 455, 70, 61);
        getContentPane().add(btnZero);
    }

    public static void main(String[] args) {
        Kalkulator theApp = new Kalkulator();
        theApp.setSize(new java.awt.Dimension(384,5));
        theApp.setVisible(true);


    }

}

*我正在使用Eclipse和microsoft builder作为GUI

0 个答案:

没有答案