将功能设置为单个按钮

时间:2014-09-17 16:09:58

标签: java swing jbutton

我正在为学校设计一个音板程序。我实际上被允许编写我想要的任何程序。我有一个为它编写的代码,如下所示:

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


}

在这个例子中,每个按钮都完全相同。如何,使用这个基本代码,我设置它,以便按钮做各自的事情?我计划设计它,然后点击“加载”按钮,然后一个数字按钮将声音加载到该按钮。在没有敲击负载的情况下按数字按钮首先播放先前指定的声音。点击“清除”会卸载所有按钮。

2 个答案:

答案 0 :(得分:2)

而不是

ButtonX.addActionListener(this);

ButtonX.addActionListener(e -> {
        //do stuff here
    });

->表示这是一个lambda表达式,它基本上从功能接口创建一个匿名类,并将其作为参数传递。有关lambda表达式的更多信息,您可以阅读我的指南here或官方(但很长)的教程here

创建所有lambda表达式后,可以删除

@Override
public void actionPerformed(ActionEvent event){
    load += 1;
    System.out.println(load);        
}

 implements ActionListener

来自你的班级。

答案 1 :(得分:2)

你需要附加不同的动作来分开按钮,例如。如何做到这一点是下面的加载和清除按钮

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


}