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?