首页 > 解决方案 > 我可以使用 JButton 作为构造函数的参数吗

问题描述

所以我有两个类,一个是带有我的 JButtons、JFrames 等的 GUI 类。我的另一个类有我的 main 方法和一个播放声音的静态方法。我正在尝试创建一个构造函数,它可以让我轻松创建一堆测验问题,它会播放声音,要求用户识别产生噪音的原因,然后显示答案是正确还是不正确。我正在尝试将 JButton 作为将要创建的问题的“正确响应”放在构造函数中,但是我收到了一个找不到符号错误

这是我的第一堂课

import javax.sound.sampled.Clip;
import java.io.File;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class NateApp {

    private static JLabel label = new JLabel("Select the correct answer");





    public static void main(String[] args)
    {
       File shred = new File("shredNoise.WAV");
       // PlaySound(noise);
        generateQuestion(" Shred", " Airplane ", "Car", "Pool Party", buttonA, shred);
    }

    public static void PlaySound(File Sound)
    {
        try{
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(Sound));
            clip.start();


            Thread.sleep(clip.getMicrosecondLength()/1000);
        }
        catch(Exception e){
        }
    }
    public static void generateQuestion(String option1, String option2, String option3, String option4, JButton correctAnswer, File audioQue)
    {
        new GUI(option1, option2, option3, option4, correctAnswer, audioQue);


        //System.out.println("Can you identify this sound?");
        File noise = new File(String.valueOf(audioQue));
        PlaySound(noise);
        //System.out.println(option1 + option2 + option3 + option4);

        if(correctAnswer.getModel().isPressed())
        {
            label.setText("Correct!");
        }




    }
}

这是我的第二个 GUI

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class GUI implements ActionListener {
    private int clicks = 0;
    private JLabel label = new JLabel("Can you identify this sound?");
    private JFrame frame = new JFrame();
    private Object ActionEvent;


    public GUI(String optionA, String optionB, String optionC, String optionD, JButton correct, File noise) {

        // the clickable button
        JButton buttonA = new JButton("A " + optionA);
        JButton buttonB = new JButton("B " + optionB);
        JButton buttonC = new JButton("C " + optionC);
        JButton buttonD = new JButton("D " + optionD);



        buttonA.addActionListener(this);
        buttonB.addActionListener(this);
        buttonC.addActionListener(this);
        buttonD.addActionListener(this);

        // the panel with the button and text
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(buttonA);
        panel.add(buttonB);
        panel.add(buttonC);
        panel.add(buttonD);
        panel.add(label);

        // set up the frame and display it
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("GUI");
        frame.pack();
        frame.setVisible(true);

    }

    // process the button clicks
    public void actionPerformed(ActionEvent e) {




    }

    // create one Frame
    public static void main(String[] args) {
    }

}

标签: javaswingconstructorjbutton

解决方案


推荐阅读