当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How do I make my code find out which button was pressed within a reasonable amount of time?

问题描述:

Alright, this one is a bit of a doozy, its about 240 lines long, but on to my point. The point is that it doesn't work. I'm pretty new to coding in general and some help would be really appreciated. I know where the problem is, it's in the last bits of it in the for loops, I went under the false presumption that if you click a JButton, it is then selected. How do I implement the .setSelected method to my program without rewriting the entire code? This code is a make-shift jeopardy of sorts btw.

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]);

}

}

}

}

网友答案:

The problem is that you make the frame visible and then add your components. Instead, call setVisible() at the end of your constructor, after you pack() the frame.

public static void main(String[] args) {
    ...
    pane.setBackground(Color.blue);
    frame.pack();
    frame.setVisible(true);
}
网友答案:

try to create a new class for buttons

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

like this each button will respond through it's own method

网友答案:

This is what fixes it:

if(event.getSource()== row1Buttons[j])

Yes, that makes it so that it will do all of the category in one fell swoop, but it doesn't matter. Thanks for all who tried and gave positive and helpful answers. EDIT: nope, it doesn't do it all in one go, I just labeled it wrong.

分享给朋友:
您可能感兴趣的文章:
随机阅读: