Я не уверен, как передавать параметры/аргументы в события события. Предполагается, что эта программа генерирует таблицу случайных времен «флеш-карту», сравнивает ее с правильным ответом и возвращает результат на консоль, позволяя пользователю узнать, правильно ли введен их вход. Редактор говорит мне, что мне нужно сделать мой класс абстрактным, но, очевидно, это не решение.java, передающий параметры в методы событий действия
общественного класса MultiplicationGui {
public static void main(String[] args)
{
//new JFrame
JFrame myJFrame = new JFrame();
//Attributes
myJFrame.setTitle("Multiplication Gui");
myJFrame.setSize(240, 200);
myJFrame.setLocation(200, 100);
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make the frame visible
myJFrame.setVisible(true);
}
}//end class
class timesTableFrame extends JFrame implements ActionListener
{
JLabel jlblNote = new JLabel("This GUI gets data from a text field");
JLabel prompt = new JLabel("Please enter your answer");
JTextField jtfAnswer = new JTextField(20);
// constructor
public timesTableFrame()
{
setLayout(new FlowLayout(FlowLayout.CENTER));
add(jlblNote);
add(prompt);
add(jtfAnswer);
// register TextFieldFrame (this method) as the listener for jtfName
jtfAnswer.addActionListener(this);
} // end TextFieldFrame() constructor
public void actionPerformed(ActionEvent e, int answer)
{
// capture the name from the text field and reset the field
double response=Double.parseDouble(jtfAnswer.getText());
jtfAnswer.setText("");
// output a message using the name to the console
if (response == answer)
System.out.println("Congratulations. You are correct!");
else if (response != answer)
System.out.println("Sorry, the correct answer is "+answer);
// dispose of the frame (this frame)
this.dispose();
} // end actionPerformed(ActionEvent e)
//********************************************************************
class generateArray
{
public int generateArray()
{
int i, j;//variables to iterate over array
int answer;//hold answer from indices into array
int MDArray[][]=new int [13][13];//new md array for table
//use nested for to populate array
for(i=0; i<13; i++)
for(j=0; j<13; j++)
MDArray[i][j]=i*j;
//generate two random numbers from 0-13 to be used as indices into array
int index1=(int)(Math.random()*13);
int index2=(int)(Math.random()*13);
//populate answer variable and return
answer = MDArray[index1][index2];
return answer;
}
} }
Вы можете сделать это 'JFrame myJFrame = новый JFrame(); 'this вместо этого' JFrame myJFrame = newtimeTableFrame(); '. Первый будет представлять пустой кадр. –
Откуда появляется 'int answer'? Вы можете просто иметь его как член класса и использовать его внутри 'actionPerformed' вместо того, чтобы принимать его как параметр. –
@peeskillet int ответ от класса generateArray, метод generateArray. Пытаясь сохранить модульный дизайн, но я не уверен, что это правильный подход. –