使用方法初始化按钮

时间:2018-06-02 08:32:20

标签: java swing actionlistener

按钮可见并显示。按钮按下时虽然没有发生。我使用print语句测试了一下,按钮似乎没有actionlistener。使用像我这样的方法制作按钮有问题吗?

框:

public class mainClass {    
static JFrame win = new JFrame();
static JPanel dis = new makeJPanel();
static JButton[][] f = new JButton[4][4];
static JButton add, sub, div, mul, clear, equals, zero, dec, per, pms;
static JTextField out;
static int v = 0;
static int w = 1;
public static void main(String[] args) {
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setTitle("Calculator");
    win.setVisible(true);
    dis.setSize(400, 600);
    win.setSize(dis.getWidth()+15, dis.getHeight()+40);
    win.add(dis);
    dis.setLayout(null);
    for(int i = 0; i < 3; i++){
        for(int k = 0; k < 3; k++){
            dis.add(f[i][k] = new JButton((v+1)+"")).setBounds(100+(k-1)*100,300+(i-1)*100,100,100);
            f[i][k].setContentAreaFilled(false);
            f[i][k].setForeground(Color.WHITE);
            f[i][k].setFont(new Font("Italics", Font.BOLD, 70));
            f[i][k].addActionListener(action);
            f[i][k].setFocusable(false);
            v++;
        }
    }
    makeButton(dis ,zero, "0", 0, 500, 100, 100, action);
    makeButton(dis ,mul, "*", 300, 200, 100, 100, action);
    makeButton(dis ,sub, "-", 300, 300, 100, 100, action);
    makeButton(dis ,add, "+", 300, 400, 100, 100, action);
    makeButton(dis ,clear, "C", 0, 100, 100, 100, action);
    makeButton(dis ,equals, "=", 300, 500, 100, 100, action);
    makeButton(dis ,dec, ".", 200, 500, 100, 100, action);
    makeButton(dis ,per, "%", 100, 100, 100, 100, action);
    makeButton(dis ,pms, "±", 200, 100, 100, 100, action);
    dis.add(out = new JTextField("")).setBounds(0, 0, 600, 100);
    out.setFont(new Font("Italics", Font.BOLD, 70));
}

makeButton方法:

    static void makeButton(JPanel dis, JButton but, String str, int x1, int y1, int x2, int y2, ActionListener a){
    System.out.println(but);
    dis.add(but = new JButton(str));
    but.setBounds(x1, y1, x2, y2);
    but.setContentAreaFilled(false);
    but.setForeground(Color.WHITE);
    but.setFont(new Font("Italics", Font.BOLD, 70));
    but.addActionListener(a);
    but.setFocusable(false);
    System.out.println(but);
}

的ActionListener

static ActionListener action=new ActionListener(){ 
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == add){
            out.setText(out.getText() + add.getText());
            System.out.println(out.getText());
        }
        if(e.getSource() == sub){
            out.setText(out.getText() + "-");
        }
        if(e.getSource() == mul){
            out.setText(out.getText() + "*");
        }
        if(e.getSource() == div){
            out.setText(out.getText() + "/");
        }
        if(e.getSource() == clear){
            out.setText("");
        }
        if(e.getSource() == dec){
            out.setText(out.getText() + dec.getText());
        }
        if(e.getSource() == equals){

        }

        if(e.getSource() == per){

        }
        if(e.getSource() == pms){

        }
        for(int i=0;i<3;i++){
            for(int k=0;k<3;k++){
                if(e.getSource() == f[i][k]){
                    out.setText(out.getText() + f[i][k].getText());
                    break;
                }
            }
        }
    }
};

我觉得用于制作按钮的方法存在问题。是否有另一种同样有效的方式?

2 个答案:

答案 0 :(得分:2)

您的按钮static JButton add, sub, div, mul, clear, equals, zero, dec, per, pms;始终为空。您可以在方法中创建新的按钮,但在方法&#34;中仅使用#34;永远不会设置您的静态变量。

你应该看看这里:Is Java "pass-by-reference" or "pass-by-value"?

答案 1 :(得分:0)

您可以更改makeButton()方法以返回创建的JButton实例,而不是尝试更改静态字段引用,但这不会起作用。为什么你这样做,你可能也想删除JPanel参数,所以这个方法只创建按钮而不是将它添加到面板。该方法将如下所示。

static JButton makeButton(JPanel dis, String str, int x1, int y1, int x2, int y2, ActionListener a){
    JButton but = new JButton(str);
    but.setBounds(x1, y1, x2, y2);
    but.setContentAreaFilled(false);
    but.setForeground(Color.WHITE);
    but.setFont(new Font("Italics", Font.BOLD, 70));
    but.addActionListener(a);
    but.setFocusable(false);
    return but;
}

然后您可以使用以下方法:

add = makeButton("+", 300, 400, 100, 100, action);
dis.add(add);

或者将它们写在一起:

dis.add(add = makeButton("+", 300, 400, 100, 100, action));