从字符串文本的JOption调用方法?

马吉查洛

我不太确定如何问这个问题,但是就在这里。请查看我的代码(为方便起见,我将其缩写,不用担心临时的“ 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                   


}

我在“问题区域”中输入了问题所在的位置。optionDialog框需要一个字符串作为文本,但是我想将其替换为“ public void info()”中的字符串,该字符串将与Math.Random一起随机化。

因此,如果我要正确运行的话,optionDialog应该显示为“ Sam”(在中间文本区域)。

抱歉,如果我没有清楚地说明任何问题,我将等待答复,谢谢!

勒韦纳尔

这是一个基本示例,说明您可以更有效地构建程序的另一种方式:

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;

}

我将其缩小了很多,以便您可以了解它的工作方式。建议您使用的“全局变量”位于底部。希望这可以让您得到JOPtionPane所需的问题和答案,并防止继续重新初始化数组。

再说一次,如果不确定,请回复我,我会看看我能做些什么。祝你好运!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从字符串文本的JOption调用方法?

来自分类Dev

从字符串创建方法调用

来自分类Dev

从字符串调用动态方法

来自分类Dev

以字符串格式调用类方法

来自分类Dev

使用字符串调用方法

来自分类Dev

发送两个字符串到JOption列表菜单

来自分类Dev

通过创建新的字符串对象来调用字符串的方法

来自分类Dev

从字符串创建类的实例和调用方法

来自分类Dev

设置对返回字符串的方法的Moq Mock <Interface>调用

来自分类Dev

使用字符串参数调用AndroidJni静态方法

来自分类Dev

用字符串和input()调用方法

来自分类Dev

使用字符串调用Java中的方法

来自分类Dev

调用静态JNI方法从C ++返回字符串

来自分类Dev

C#使用字符串调用方法

来自分类Dev

通过变量(字符串)调用对象和方法

来自分类Dev

对字符串文字调用方法(JAVA)

来自分类Dev

如何从格式字符串中调用对象的方法?(蟒蛇)

来自分类Dev

Javascript反射,使用字符串调用方法

来自分类Dev

从字符串动态调用类的静态方法

来自分类Dev

使用字符串调用Java中的方法

来自分类Dev

通过变量(字符串)调用对象和方法

来自分类Dev

用C ++中的字符串调用C#方法

来自分类Dev

使用特定字符串调用的测试方法

来自分类Dev

从字符串中调用方法作为继承类

来自分类Dev

用java替换字符串中文本的最佳方法

来自分类Dev

转字符串文本

来自分类Dev

匹配列中文本字符串中的字符然后调用函数

来自分类Dev

在发送recv调用时使用C ++字符串填充垃圾文本

来自分类Dev

Dapper:调用postgres函数时,字符串作为文本传递