我不太确定如何提出这个问题,但现在确实如此。请查看我的代码(我为了您的方便而缩写,并且不要担心" Sam"它的临时性)
public class JeopardyGUI_Main11 extends javax.swing.JFrame {
/**
* Creates new form JeopardyGUI_Main
*/
public JeopardyGUI_Main11() {
initComponents();
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void info(){
String[] am = new String[25];
String[] sp = new String[25];
String[] mu = new String[25];
String[] spe = new String[25];
String[] tv = new String[25];
String[] mo = new String[25];
String[] ama = new String[25];
String[] spa = new String[25];
String[] mua = new String[25];
String[] spea = new String[25];
String[] tva = new String[25];
String[] moa = new String[25];
am[0] = "Sam";ama[0] = "Sam";
am[1] = "Sam";ama[1] = "Sam";
...
am[23] = "Sam";ama[23] = "Sam";
am[24] = "Sam";ama[24] = "Sam";
mu[0] = "Sam";mua[0] = "Sam";
mu[1] = "Sam";mua[1] = "Sam";
...
mu[23] = "Sam";mua[23] = "Sam";
mu[24] = "Sam";mua[24] = "Sam";
spe[0] = "Sam";spea[0] = "Sam";
spe[1] = "Sam";spea[1] = "Sam";
...
spe[23] = "Sam";spea[23] = "Sam";
spe[24] = "Sam";spea[24] = "Sam";
tv[0] = "Sam";tva[0] = "Sam";
tv[1] = "Sam";tva[1] = "Sam";
...
tv[23] = "Sam";tva[23] = "Sam";
tv[24] = "Sam";tva[24] = "Sam";
sp[0] = "Sam";spa[0] = "Sam";
sp[1] = "Sam";spa[1] = "Sam";
...
sp[23] = "Sam";spa[23] = "Sam";
sp[24] = "Sam";spa[24] = "Sam";
mo[0] = "Sam";moa[0] = "Sam";
mo[1] = "Sam";moa[1] = "Sam";
...
mo[23] = "Sam";moa[23] = "Sam";
mo[24] = "Sam";moa[24] = "Sam";
int random_int = (int) (Math.random() * ( 0 - 24 ));
String am_qu = am[random_int];
String am_an = ama[random_int];
String sp_qu = sp[random_int];
String sp_an = spa[random_int];
String mu_qu = mu[random_int];
String mu_an = mua[random_int];
String spe_qu = sp[random_int];
String spe_an = spa[random_int];
String tv_qu = tv[random_int];
String tv_an = tva[random_int];
String mo_qu = mo[random_int];
String mo_an = moa[random_int];
}
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void sp1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void sp2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void am1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object[] options = {"Yes, please",
"No way!"};
int n = JOptionPane.showOptionDialog(null,
""+am_qu, <-------------------------------------AREA IN QUESTION
"",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
}
private void mo1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void am1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JeopardyGUI_Main11.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JeopardyGUI_Main11.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JeopardyGUI_Main11.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JeopardyGUI_Main11.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JeopardyGUI_Main11().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton am1;
private javax.swing.JButton am2;
private javax.swing.JButton am3;
...
private javax.swing.JButton am4;
private javax.swing.JButton am5;
// End of variables declaration
}
在#34;问题区域&#34;我说的是问题的位置。 optionDialog框需要一个字符串作为文本,但我想用&#34; public void info()&#34;中的字符串替换它。将使用Math.Random随机化。
因此,optionDialog应该读取&#34; Sam&#34; (在中间文本区域)如果我要正确运行它。
对不起,如果我没有说清楚,请问任何事情,我会等待回复,谢谢!
答案 0 :(得分:1)
以下是一个基本示例,您可以通过不同的方式构建程序,这可能更有效:
public class SO {
public static void main(String[] args) throws ClassNotFoundException{
getinfo();//Set the random question and answer
Object[] options = {"Yes, please", "No way!"};
int n = JOptionPane.showOptionDialog(null,
SO.question,//Reference the question set
"",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
JOptionPane.showMessageDialog(null, SO.answer);
}
public static void getinfo(){ //Will set question and answer
int category = new Random().nextInt(2 - 0);//Random number to get a category
int choice = new Random().nextInt(2 - 0);//Random to get a selection
SO.question = questions[category][choice];//Get question
SO.answer = answers[category][choice];//Answer is located in same position in answers array
}
/*Each category is represented by a seperate array within the questions
* 2 dimensional array. These are declared as static fields or 'Global fields'
* This way they are easily accessible and only initialised once
*/
private static String[][] questions = new String[][]{
{"Question 1, category 1", "Question 2 category 1"},//questions[0][*] for one category
{"Question 1 category 2", "Question 2 category 2"}//questions[1][*] on another category
};
/*The answers array mirrors the other array so each answer in this 2D array
* is in the same position as the relevant question in the questions array
*
*/
private static String[][] answers = new String[][]{
{"Answer 1 category 1", "Answer 2 category 1"}, //answers[0][*] for questions [0][*]
{"Answer 1 category 2", "Answer 2 category 2"}//answers[1][*] for questions [1][*]
};
//These will each hold a question and an answer
private static String question;
private static String answer;
}
我已经做得更小了,所以你可以了解它是如何工作的。全局变量&#39;有人建议你在底部。这应该可以让您获得JOPtionPane所需的问题和答案,并防止不断重新初始化阵列。
再一次,如果不确定评论我回来,我会看到我能做什么。 祝你好运!
答案 1 :(得分:0)
我不确定我是否了解您的程序,但我假设您尝试在info()方法中实例化一些String,然后在am1ActionPerformed()方法中使用这些String的值。现在,我也不能很好地理解你的问题,但我在你的代码中看到的是范围问题。
您尝试在am1ActionPerformed()方法中访问的字符串am_qu不存在,因为它是在info()方法中创建和销毁的。如果您想在另一种方法中使用它,请尝试将其改为全局变量。
更新
它无法找到符号的原因是因为范围不存在。我不知道你对&#34的意思;我添加了Global而不是void info()&#34;但是你创建一个变量global的方式是在类中声明它,但在其他所有内容之外;
考虑以下事项:
public class TempClass{
public String globVar; //global variable
public void method1(){
globVar ="hello"; // Same global variable
}
public void method2(){
globVar ="world!"; // same here too
}
}
这里同样的变量globVar可以被method1()和method2()访问,并且可以被两者修改。在method1之后调用method2()将替换globVar的值 现在考虑一个局部变量。
public class TempClass{
public void method1(){
String localVar; //local variable is declared
localVar = "Hello"; //value assigned to local Variable
} //localVar is destroyed
public void method2(){
String localVar; // different localVar to method1
localVar = "world"; // value assigned to localVar
} // localVar destroyed
}
这里使用两个不同的变量,即使它们被称为相同。不仅如此,当方法退出时它们会被销毁,并且不能在其他任何地方使用。 最后考虑到目前为止你在做什么
public class TempClass{
public void method1(){
String localVar; //local variable is declared
localVar = "Hello World"; //value assigned to local Variable
} //localVar is destroyed
public void method2(){
System.out.println(localVar); // Error. localVar does not exist
}
}
因此,您可以看到变量在被使用之前被销毁。您需要全局声明它。