让JButton工作

时间:2016-02-10 00:46:56

标签: java swing actionlistener

现在我在计算器上有一些按钮,它们没有设置。我很困惑如何让他们在点击时在JTextField中打印一些东西。我知道你需要使用ActionListener,但我似乎无法使它工作。谢谢你的帮助!

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

public class Calculator extends JPanel implements ActionListener {
private JTextField tf = null;
private JButton[] arrBtn = null;
private String[] btnNames = { "1", "2", "3", "4", "5", "6", "7", "CE", "-", "+", "/", "%", "*", "=" };
private JPanel jp = new JPanel();
private char op = ' ';
private int num1 = 0;
private int num2 = 0;
private int result = 0;
private boolean isOpPressed = false;
private JPanel btnPl;

public Calculator() {
    super();

    jp = new JPanel();
    jp.setLayout(new GridLayout(3, 3));
    btnPl = new JPanel();
    btnPl.setLayout(new GridLayout(4, 4));

    jp.add(new JTextField());
    jp.add(new JTextField());
    jp.add(new JTextField());
    jp.add(new JTextField());
    jp.add(new JTextField());
    jp.add(new JTextField());

    arrBtn = new JButton[btnNames.length];

    for (int i = 0; i < arrBtn.length; i++) {
        arrBtn[i] = new JButton(btnNames[i]);
        arrBtn[i].addActionListener(this);
        btnPl.add(arrBtn[i]);
    }

    this.setLayout(new BorderLayout());
    this.add(jp, BorderLayout.NORTH);
    this.add(btnPl, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e) {

}

public static void main(String args[]) {
    new Calculator();
    JFrame frame = new JFrame("Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    RomanCalculator cal = new RomanCalculator();
    frame.add(cal);
    frame.pack();
    frame.setSize(500, 500);
    frame.setVisible(true);

}

}

2 个答案:

答案 0 :(得分:0)

我通常在java中使用的方法是拥有一个处理按钮点击的内部类

public x extends JFrame(){
//I like to store my buttons in an array if possible. 
JButton [] buttonArray =  new JButton [2];

//instantialize each of the arrays buttons
buttonArray[0] = new JButton("hello");
buttonArray[1] = new JButton("world");

//create a listener of type buttonpress (currently undefined)
buttonPress Listener = new buttonPress();

//attach the button action listeners to the listener I created above.
buttonArray[0].addActionListener(Listener);
buttonArray[1].addActionListener(Listener);

//Create a private inner class called "buttonPress" which will handle the clicks for its listeners
private class buttonPress implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == buttonArray[0]){
            try
            {
                ..some logic                
            }
            catch (Exception e1)
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else if( e.getSource() == buttonArray[1])
        { 
            ...some other logic     
        }
    }

}//close inner class
}//close outer class

答案 1 :(得分:0)

我建议先查看thisthisthis

以下是创建新TextField的方法

JTextField textField = new JTextField();

然后,您应该创建一个类似于此

的按钮和动作侦听器
JButton someBtn = new JButton("Some Text");
someBtn.addActionListener(this);

你的ActionPerformed

@Override
public void actionPerformed(ActionEvent e) {
    textField.setText("New Text");
}

如果您希望继续使用JButtons数组的方法,我建议您做类似的事情。

String[] btnNames = {"1", "2", "3", etc.};
JButton[] allBtns = new JButton[10];
for(int i = 0; i < 10; i++){
    allBtns[i] = new JButton(btnNames[i]);
    allBtns[i].addActionListener(this);
    //Using the previous actionPerformed
}

如果您想自定义每个按钮的功能,可以执行此操作

anyBtn.addActionListener(e -> textField.setText("Anything"));

请查看lambda's了解详情。