I am trying to make cleaner code in my programs. So I was trying to compress my code to create buttons:
Before, I needed to copy this every single time:
Dimension JButton_Cryption_Size = JButton_Cryption.getPreferredSize();
JButton_Cryption.setBounds(5, 5, JButton_Cryption_Size.width + 50, JButton_Cryption_Size.height);
JButton_Cryption.setFocusPainted(false);
JButton_Cryption.addActionListener(this);
add(JButton_Cryption);
but now I made this method: (Don't pay attention to the button names, they are for testing)
public JButton JButton_Testing1,
JButton_Testing2,
JButton_3;
private void addJButton(JButton ButtonName, String Name, int x, int y, int width, int height, String ToolTip, boolean FocusedPainted, boolean Opaque, boolean ContentAreaFilled, boolean BorderPainted){
ButtonName = new JButton(Name);
Dimension Button_Size = ButtonName.getPreferredSize();
if(width == 0){
ButtonName.setBounds(x, y, Button_Size.width, height);
}if(height == 0){
ButtonName.setBounds(x, y, width, Button_Size.height);
}if(width == 0 && height == 0){
ButtonName.setBounds(x, y, Button_Size.width, Button_Size.height);
}if(width != 0 && height != 0){
ButtonName.setBounds(x, y, width, height);
}
ButtonName.addActionListener(this); // class: implements ActionListener
ButtonName.setToolTipText(ToolTip);
ButtonName.setFocusPainted(FocusedPainted);
ButtonName.setOpaque(Opaque);
ButtonName.setContentAreaFilled(ContentAreaFilled);
ButtonName.setBorderPainted(BorderPainted);
add(ButtonName);
}
private void addButtonToFrame(){
addJButton(JButton_Testing1, "Testing 1", 150, 100, 172, 0, null, false, true, true, true);
addJButton(JButton_Testing2, "Testing 2", 0, 0, 0, 0, null, false, true, true, true);
addJButton(JButton_Testing3, "Testing 3", 200, 150, 250, 100, "YO", false, true, true, true);
}
But when I want to add an action to the button, it wont work
@Override
public void actionPerformed(ActionEvent e){
Object src = e.getSource();
if(src == JButton_Testing1){
System.out.println("yo");
}
}
How can I make so I can keep my thing (or modify it a bit) so I can use the ActionListener correctly