Reputation: 3
I have a soundboard program I am designing for school. I'm actually permitted to write whatever program that I want. I have a code written for it, as shown here:
package soundboard;
import javax.swing.*;
import java.awt.event.*;
public class Soundboard implements ActionListener{
JButton loadButton;
JButton clearButton;
JButton Button1;
JButton Button2;
JButton Button3;
JButton Button4;
JPanel mainsPanel;
int load;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
loadButton.addActionListener(this);
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
clearButton.addActionListener(this);
Button1 = new JButton("1");
Button1.setSize(80, 80);
Button1.setLocation(4, 45);
Button1.addActionListener(this);
Button2 = new JButton("2");
Button2.setSize(80, 80);
Button2.setLocation(92, 45);
Button2.addActionListener(this);
Button3 = new JButton("3");
Button3.setSize(80, 80);
Button3.setLocation(4, 133);
Button3.addActionListener(this);
Button4 = new JButton("4");
Button4.setSize(80, 80);
Button4.setLocation(92, 133);
Button4.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(Button1);
frame.add(Button2);
frame.add(Button3);
frame.add(Button4);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent event){
load += 1;
System.out.println(load);
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
In this example, every single button does the exact same thing. How, using this base code, ca I set it so the buttons do their own individual thing? I plan on designing it so hitting the "load" button and then a number-button loads a sound to that said button. Hitting a number-button without hitting load first plays the previously designated sound. Hitting "clear" unloads all buttons.
Upvotes: 0
Views: 103
Reputation: 11132
Instead of
ButtonX.addActionListener(this);
write
ButtonX.addActionListener(e -> {
//do stuff here
});
The ->
signifies that this is a lambda expression, which basically creates an anonymous class from a functional interface and passes it as an argument. For more on lambda expressions, you can read my guide here, or the official (but long) tutorial here.
After you make all the lambda expressions, you can remove the
@Override
public void actionPerformed(ActionEvent event){
load += 1;
System.out.println(load);
}
and
implements ActionListener
from your class.
Upvotes: 2
Reputation: 15708
You need to attach different action performed to seperate button an e.g. on how to do it is below for load and clear button
import javax.swing.*;
import java.awt.event.*;
public class Soundboard implements ActionListener{
JButton loadButton;
JButton clearButton;
JButton Button1;
JButton Button2;
JButton Button3;
JButton Button4;
JPanel mainsPanel;
int load;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
loadButton.addActionListener(e -> System.out.println("load action"));
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
clearButton.addActionListener(e -> System.out.println("Clear action"));
Button1 = new JButton("1");
Button1.setSize(80, 80);
Button1.setLocation(4, 45);
Button1.addActionListener(this);
Button2 = new JButton("2");
Button2.setSize(80, 80);
Button2.setLocation(92, 45);
Button2.addActionListener(this);
Button3 = new JButton("3");
Button3.setSize(80, 80);
Button3.setLocation(4, 133);
Button3.addActionListener(this);
Button4 = new JButton("4");
Button4.setSize(80, 80);
Button4.setLocation(92, 133);
Button4.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(Button1);
frame.add(Button2);
frame.add(Button3);
frame.add(Button4);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent event){
load += 1;
System.out.println(load);
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
Upvotes: 2