0

The buttons are visible and displayed. when the buttons are press though nothings happens. i have tested it a bit using print statements and the buttons seem to not have an actionlistener. is there an issue with making buttons using a method like i have?

Frame:

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 Method:

    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;
                }
            }
        }
    }
};

i feel that there is an issue with the method used to make the buttons. is there an alternative way that is just as efficient?

Mirahles
  • 5
  • 5
  • 1
    Over abundance of static is a pretty good sign of a bad design – MadProgrammer Jun 02 '18 at 08:46
  • true but the question isn't roast me – Mirahles Jun 02 '18 at 08:53
  • 1
    No, but it’s a pretty good place to start trying to solve your issue – MadProgrammer Jun 02 '18 at 08:53
  • yeah, you have a good point – Mirahles Jun 02 '18 at 08:55
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. (Note: it is an MCVE / SSCCE.) – Andrew Thompson Jun 02 '18 at 14:44
  • .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 02 '18 at 14:45

2 Answers2

2

Your buttons static JButton add, sub, div, mul, clear, equals, zero, dec, per, pms; are always null. You create new Buttons in your method but its only "in the method" your static variables are never set.

You should have a look here: Is Java "pass-by-reference" or "pass-by-value"?

pL4Gu33
  • 2,045
  • 16
  • 38
0

You can change your makeButton() method to return the created JButton instance instead of trying to change the static field reference, which won't work. And why you are on it, you might want to remove the JPanel argument as well, so this method only creates the button and not add it to a panel. The method will look like this.

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;
}

Then you can use this method as follow:

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

Or write it together as:

dis.add(add = makeButton("+", 300, 400, 100, 100, action));
Progman
  • 16,827
  • 6
  • 33
  • 48