Issue
I can't use eval() and window[], because they don't work for me for some reason.
Here I'm trying to make a method to make the creation of a new JButton to be easier and more space efficient since I will be making a LOT of the same buttons but with a slight difference. I just don't know how to change the Class (type) and class (variable name).
public void makeButton(JButton specificButton, JFrame frame, String text, int[] border) {
specificButton = new JButton();
specificButton.setText(text);
frame.add(specificButton);
specificButton.setBounds(border[0], border[1], border[2], border[3]);
specificButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
myClass myclass = new myClass();// Example
myclass...
}
});
}
I found no other potential option.
Solution
One way to do this would be to pass the action as a Runnable to the method, something like:
public JButton makeButton(JFrame frame, String text, Runnable runnable) {
JButton button = new JButton();
button.setText(text);
frame.add(button);
button.addActionListener(event -> {
frame.setVisible(false);
runnable.run();
});
return button;
}
Which you would use like this:
JButton button1 = makeButton(frame, "button 1", () -> {
// TODO action for button 1
});
JButton button2 = makeButton(frame, "button 2", () -> {
// TODO action for button 2
});
Answered By - greg-449
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.