好吧,这个有点像doozy,它大约有240行,但是就我而言。关键是它不起作用。我对编码很新,一些帮助会非常感激。我知道问题出在哪里,它在for循环中的最后几位,我假设如果你单击一个JButton,它会被选中。如何在不重写整个代码的情况下将.setSelected方法实现到我的程序中?这段代码是各种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]);
}
}
}
}
答案 0 :(得分:1)
问题是您可以看到框架,然后添加组件。相反,请在构造函数的 end 处调用setVisible()
,在pack()
框架之后调用。
public static void main(String[] args) {
...
pane.setBackground(Color.blue);
frame.pack();
frame.setVisible(true);
}
答案 1 :(得分:0)
尝试为按钮创建一个新类
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);
}
});
}
像这样每个按钮都会通过它自己的方法进行响应
答案 2 :(得分:0)
这就是修复它的原因:
if(event.getSource()== row1Buttons[j])
是的,这使它能够一举完成所有类别,但这并不重要。感谢所有尝试并给出积极和有益答案的人。 编辑:不,它不会一气呵成,我只是把它标错了。