Java 我如何让我的代码找出哪个按钮是在合理的时间内按下的?

Java 我如何让我的代码找出哪个按钮是在合理的时间内按下的?,java,swing,awt,Java,Swing,Awt,好的,这一条有点糟糕,大约240行长,但我要说的是。关键是它不起作用。一般来说,我对编码相当陌生,如果能提供一些帮助,我将不胜感激。我知道问题在哪里,它在for循环的最后一位,我错误地假设,如果你点击一个JButton,它就会被选中。在不重写整个代码的情况下,如何在程序中实现.setSelected方法?顺便说一句,这是一种权宜之计 import javax.swing.*; import java.awt.*; import java.awt.event.*; import sun.audio

好的,这一条有点糟糕,大约240行长,但我要说的是。关键是它不起作用。一般来说,我对编码相当陌生,如果能提供一些帮助,我将不胜感激。我知道问题在哪里,它在for循环的最后一位,我错误地假设,如果你点击一个JButton,它就会被选中。在不重写整个代码的情况下,如何在程序中实现.setSelected方法?顺便说一句,这是一种权宜之计

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.*;
public class Jeopardy implements ActionListener
{
    public static JFrame frame = new JFrame("Jeopardy!");
    public static JButton[] row1Buttons = new JButton[6];
    public static JButton[] row2Buttons = new JButton[6];
    public static JButton[] row3Buttons = new JButton[6];
    public static JButton[] row4Buttons = new JButton[6];
    public static JButton[] row5Buttons = new JButton[6];

    //Creates 5 arrays, each with 6 JButtons in each (otherwise mass confusion
    //is going to ensue because I would have to individually add 30 JButtons to the pane,
    //all of which I would have to remember what they contain.)
    public static JPanel pane = new JPanel();
    public static JLabel[] labels = new JLabel[6];
    //Creates an array of 6 JLabels
    public static void main(String[] args)
    {
        frame.setSize(800, 600);
        frame.setVisible(true);
        frame.toFront();
        frame.setContentPane(pane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font font = new Font("ComicSans", Font.PLAIN, 20);
        Color colour = new Color(0, 0, 240);
        for(int a = 0; a < row1Buttons.length; a++)
        {
            row1Buttons[a] = new JButton("$200");
            row2Buttons[a] = new JButton("$400");
            row3Buttons[a] = new JButton("$600");
            row4Buttons[a] = new JButton("$800");
            row5Buttons[a] = new JButton("$1000");
            //Gives text to all 30 JButtons
            row1Buttons[a].addActionListener(new Jeopardy());
            row2Buttons[a].addActionListener(new Jeopardy());
            row3Buttons[a].addActionListener(new Jeopardy());
            row4Buttons[a].addActionListener(new Jeopardy());
            row5Buttons[a].addActionListener(new Jeopardy());
            //adds action listener to all 30
            row1Buttons[a].setBackground(colour);
            row2Buttons[a].setBackground(colour);
            row3Buttons[a].setBackground(colour);
            row4Buttons[a].setBackground(colour);
            row5Buttons[a].setBackground(colour);
            //sets background colour
            row1Buttons[a].setForeground(Color.yellow);
            row2Buttons[a].setForeground(Color.yellow);
            row3Buttons[a].setForeground(Color.yellow);
            row4Buttons[a].setForeground(Color.yellow);
            row5Buttons[a].setForeground(Color.yellow);
        }
        labels[0] = new JLabel("Mathematics");
        labels[1] = new JLabel("Computer Science");
        labels[2] = new JLabel("Historical Events");
        labels[3] = new JLabel("Chemistry");
        labels[4] = new JLabel("TBD");
        labels[5] = new JLabel("TBD");
        for(int k = 0; k < labels.length; k++)
        {
            labels[k].setForeground(Color.yellow);
        }
        pane.setLayout(new GridLayout(6, 6, 6, 6));
        for(int b = 0; b < labels.length; b++)
        {
            pane.add(labels[b]);
            pane.add(row1Buttons[b]);
            pane.add(row2Buttons[b]);
            pane.add(row3Buttons[b]);
            pane.add(row4Buttons[b]);
            pane.add(row5Buttons[b]);
        }
        pane.setBackground(Color.blue);
    }
    public void actionPerformed(ActionEvent event)
    {
        String[] answerRow1 = new String[6];
        String[] answerRow2 = new String[6];
        String[] answerRow3 = new String[6];
        String[] answerRow4 = new String[6];
        String[] answerRow5 = new String[6];
        String[] questionsRow1 = new String[6];
        //Write $200 questions
        questionsRow1[0] = "Question";
        questionsRow1[1] = "Question";
        questionsRow1[2] = "Question";
        questionsRow1[3] = "Question";
        questionsRow1[4] = "Question";
        questionsRow1[5] = "Question";
        String[] questionsRow2 = new String[6];
        //Write $400 questions
        questionsRow2[0] = "Question";
        questionsRow2[1] = "Question";
        questionsRow2[2] = "Question";
        questionsRow2[3] = "Question";
        questionsRow2[4] = "Question";
        questionsRow2[5] = "Question";
        String[] questionsRow3 = new String[6];
        //Write $600 questions
        questionsRow3[0] = ("The function y=3(2)^x will have this for a y value when x = 3.");
        questionsRow3[1] = "Question";
        questionsRow3[2] = "Question";
        questionsRow3[3] = "Question";
        questionsRow3[4] = "Question";
        questionsRow3[5] = "Question";
        String[] questionsRow4 = new String[6];
        //Write $800 questions
        questionsRow4[0] = "Question";
        questionsRow4[1] = "Question";
        questionsRow4[2] = "Question";
        questionsRow4[3] = "Question";
        questionsRow4[4] = "Question";
        questionsRow4[5] = "Question";
        String[] questionsRow5 = new String[6];
        //Write $1000 questions
        questionsRow5[0] = "Question";
        questionsRow5[1] = "Question";
        questionsRow5[2] = "Question";
        questionsRow5[3] = "Question";
        questionsRow5[4] = "Question";
        questionsRow5[5] = "Question";
        for(int j = 0; j < questionsRow1.length; j++)
        {
            if(row1Buttons[j].getModel().isPressed())
            {
                try
                {
                    InputStream in = new FileInputStream("sounds/x.wav");
                    AudioStream openSound = new AudioStream(in);
                    AudioPlayer.player.start(openSound);

                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
                }
                answerRow1[j] = JOptionPane.showInputDialog(null, questionsRow1[j]);
            }
        }
        for(int a = 0; a < questionsRow2.length; a++)
        {
            if(row2Buttons[a].isSelected())
            {
                try
                {
                    InputStream in = new FileInputStream("sounds/x.wav");
                    AudioStream openSound = new AudioStream(in);
                    AudioPlayer.player.start(openSound);
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
                }    
                JOptionPane.showInputDialog(null, questionsRow2[a]);
            }
        }
        for(int r = 0; r < questionsRow3.length; r++)
        {
            if(row3Buttons[r].isSelected())
            {
                try
                {
                    InputStream in = new FileInputStream("sounds/x.wav");
                    AudioStream openSound = new AudioStream(in);
                    AudioPlayer.player.start(openSound);
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
                }
                JOptionPane.showInputDialog(null, questionsRow3[r]);
            }
        }
        for(int e = 0; e < questionsRow4.length; e++)
        {
            if(row4Buttons[e].isSelected())
            {
                try
                {
                    InputStream in = new FileInputStream("sounds/x.wav");
                    AudioStream openSound = new AudioStream(in);
                    AudioPlayer.player.start(openSound);
                }
                catch(Exception exception)
                {
                    JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
                }    
                JOptionPane.showInputDialog(null, questionsRow4[e]);
            }
        }
        for(int d = 0; d < questionsRow5.length; d++)
        {
            if(row5Buttons[d].isSelected())
            {
                try
                {
                    InputStream in = new FileInputStream("sounds/x.wav");
                    AudioStream openSound = new AudioStream(in);
                    AudioPlayer.player.start(openSound);
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
                }
                JOptionPane.showInputDialog(null, questionsRow5[d]);
            }
        }        
    }
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入sun.audio.*;
导入java.io.*;
公共类Jeopardy实现ActionListener
{
publicstaticjframe=newjframe(“Jeopardy!”);
公共静态JButton[]row1Buttons=新JButton[6];
公共静态JButton[]row2Buttons=新JButton[6];
公共静态JButton[]row3Buttons=新JButton[6];
公共静态JButton[]row4Buttons=新JButton[6];
公共静态JButton[]行5按钮=新JButton[6];
//创建5个数组,每个数组中有6个JButton(否则会导致大量混乱)
//因为我必须在窗格中单独添加30个按钮,
//所有这些我都必须记住它们包含了什么。)
公共静态JPanel窗格=新建JPanel();
公共静态JLabel[]标签=新JLabel[6];
//创建一个包含6个jlabel的数组
公共静态void main(字符串[]args)
{
框架设置尺寸(800600);
frame.setVisible(true);
frame.toFront();
frame.setContentPane(窗格);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font Font=新字体(“ComicSans”,Font.PLAIN,20);
颜色=新颜色(0,0,240);
对于(int a=0;apublic class Response_Button extends JButton {

public Response_Button(final JPanel container, final String name) {
    this.setText(name);
    this.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JOptionPane.showMessageDialog(container, "clicked " + name);
        }
    });
}
public static void main(String[] args) {
    ...
    pane.setBackground(Color.blue);
    frame.pack();
    frame.setVisible(true);
}
if(event.getSource()== row1Buttons[j])