简体   繁体   中英

JButtons when pressed saved in array

I'm working on my school project. My asigment is to make a piano game. The program works fine but my work stopped when i came to a problem. I would like to put the integers, which i get from pushing the specific JButton, into an array. For example if i would press JButton 3, JButton 4, and JButton 1, i would get in my array [3,4,1]. Please help me. Best regards. Here's my code:

import javax.swing.*;
import javax.sound.sampled.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;

class vse_v_enem extends JFrame implements ActionListener
{

public static void main(String [] arg){
    new vse_v_enem();              
}    

public vse_v_enem(){
    initSounds();
}

protected void initSounds(){
    sounds();
    zacetek();        
}

public void sounds(){

    for(int i=1;i<=13;i++)
         try{
            audioIn[i] = AudioSystem.getAudioInputStream(vse_v_enem.class.getResource("zvoki/"+i+".wav"));
            //System.out.println("zvoki/"+i+".wav.wav");
        } catch(javax.sound.sampled.UnsupportedAudioFileException e){

        } catch(Exception e){
            System.out.println("Hm....  something went wrong");
        }

}

public void zacetek (){ 
    JFrame okvir = new JFrame("Klaviaturska zabava");
    okvir.setBounds(500,400,416,214);
    JLayeredPane panel = new JLayeredPane(); 
    okvir.add(panel); 

    //int f[] = igra();        
    /**naredi crne tipke*/
    int crnih = 5; 
    int sirina = 30; 
    int visina = 113;        
    for(int i = 0; i<crnih;i++){
        JButton b = new JButton(""+(i+1));
        b.setFont(b.getFont().deriveFont((float)11));
        b.setForeground(Color.WHITE);
        b.setBackground(Color.BLACK);
        b.setSize(sirina,visina);             
        b.setLocation(i*50 + (sirina*3)-5,0);            
        b.addActionListener(this);

        panel.add(b,1,-1);

    }
    /** naredi bele tipke*/
    int belih = 8; 
    int sirina1 = 50; 
    int visina1 = 176;

    for (int i = 6; i<belih+6; i++){            

        JButton b = new JButton(""+i);
        b.setBackground(Color.WHITE);
        b.setLocation((i-6)*sirina1,0);   
        b.setSize(sirina1,visina1);
        b.addActionListener(this);                    
        panel.add(b,0,-1);

    }        

    okvir.setVisible(true);   
}    

public void predvajajZvBesedilo(AudioInputStream audioIn){  
    sounds();

    try {
        clip = AudioSystem.getClip();
        clip.open(audioIn);            
    } catch(Exception e) {};
    clip.setFramePosition(0); 
    clip.start();
}

public void tabela(int tab){              
}

public void actionPerformed(ActionEvent e){

    JButton tempButton = (JButton)e.getSource();
    System.out.print(  tempButton.getText() + " " );           

    int clipNr = Integer.valueOf(tempButton.getText());
    //tabela(tab[clipNr]);
    predvajajZvBesedilo( audioIn[clipNr]);

}

public int[] igra(){        
    int t [] = {4,5,8,6};
    System.out.print("[ ");
    for(int i = 0; i < t.length; i++){
        //t[i] = (int)(Math.random()*13+1);            
        System.out.print(t[i]+" "); 

    }      
    System.out.print("] ");
    System.out.println();
    System.out.println();

    return t;
}

public void delay (int i){
    try {
        Thread.sleep(i*1000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}

private JFrame okvir = null;
private Clip clip = null;
int [] tab = new int [igra().length];

AudioInputStream audioIn[] = new AudioInputStream[14];

}

You need to add JButton to your JFrame . Then add ActionListener to your buttons with help of addActionListener method. In method actionPerformed of ActionListener you need to determine button which is pressed like next way if(e.getSource() == button1) array[i] = 1; where e is ActionEvent parametr. Use one listener for all buttons, here it's your frame . I think it help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM