【发布时间】:2014-05-18 22:48:17
【问题描述】:
我不太确定如何问这个问题,但就是这样。请查看我的代码(为方便起见,我将其缩写,不要担心“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”(在中间的文本区域)。
对不起,如果我没有说清楚,请问有什么问题,我将等待回复,谢谢!
【问题讨论】:
-
在您的 info 方法结束时,您有 12 个随机字符串,您是说您希望其中一个字符串成为您的 JOPtionPane 中的消息吗?
-
是的,它们是随机发生器,我希望结果显示在 JOptionPane 的 ("") 中,使其成为“正文”。
-
是的,但是您希望这 12 个字符串中的 1 个随机出现,还是全部出现在 ("") 中?
-
是其中之一,因为我将使用其他随机字符串分配不同的按钮。基本上是随机化每个按钮的危险问题。 6 列不同的主题,每列有 5 个按钮选择,按钮将显示一个随机问题(我将输入代替“Sam”),然后不允许用户输入答案并将其与答案进行比较字符串。感谢您的宝贵时间!
-
这里的主要问题是变量作用域。无论您调用该方法多少次来初始化这些变量,它们都只会持续到方法结束。考虑将他们居住的地方搬到一块田地。
标签: java string swing user-interface joptionpane