请记住,我是一个完整的(2周)Java初学者,并且可能需要解释为三岁的孩子。
我创建了一个由主类调用的表单。它从另一个类调用六个对象的arrayList,并在按钮上显示前四个值。 第六项是字符串' qText'显示在文本窗格上,而第五个没有显示。到现在为止还挺好。
按a按钮应为变量' qans'分配值0,1,2或3。
我希望能够检查一下' qans'具有与变量相同的值' ans'并将int或bool返回给主类。
package lp2;
import java.util.ArrayList;
/**
*
* @author david
*/
public class form extends javax.swing.JFrame {
ArrayList set = methods.getquestion();
int a = (int) set.get(0);
int b = (int) set.get(1);
int c = (int) set.get(2);
int d = (int) set.get(3);
int ans = (int) set.get(4);
int qans;
int check = (int) set.get(qans);
String qText = (String) set.get(5);
String stringA = String.valueOf(a);
String stringB = String.valueOf(b);
String stringC = String.valueOf(c);
String stringD = String.valueOf(d);
String stringAns = String.valueOf(ans);
/**
* Creates new form form
*/
public form() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
btnA = new javax.swing.JButton();
btnB = new javax.swing.JButton();
btnC = new javax.swing.JButton();
btnD = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jTextArea1.setRows(5);
jTextArea1.setText(qText);
jScrollPane1.setViewportView(jTextArea1);
btnA.setText(stringA);
btnA.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAActionPerformed(evt);
}
});
btnB.setText(stringB);
btnB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBActionPerformed(evt);
}
});
btnC.setText(stringC);
btnC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCActionPerformed(evt);
}
});
btnD.setText(stringD);
btnD.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnA, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnB, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnC, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnD, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(28, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnA, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnB, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnC, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnD, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 54, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnAActionPerformed(java.awt.event.ActionEvent evt) {
int qans =0;
}
private void btnBActionPerformed(java.awt.event.ActionEvent evt) {
int qans = 1; // TODO add your handling code here:
}
private void btnCActionPerformed(java.awt.event.ActionEvent evt) {
int qans = 2; // TODO add your handling code here:
}
private void btnDActionPerformed(java.awt.event.ActionEvent evt) {
int qans = 3; // 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(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(form.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 form().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnA;
private javax.swing.JButton btnB;
private javax.swing.JButton btnC;
private javax.swing.JButton btnD;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
答案 0 :(得分:1)
建议:
int qans = 1;
更改为qans = 1;
,同样更改所有其他类似的代码。答案 1 :(得分:1)
同时查看JFileChooser
和JOptionPane
,它们都是'形式'(java术语中的'windows'),但以不同的方式返回它们的值。
JOptionPane
有一个静态方法,显示对话框,等待它完成,然后立即返回结果。
JFileChooser
通过创建一个实例(就像你一样),让它可见(就像你一样),等待它完成,并在完成后你可以通过调用'getter'方法来询问对象(询问)不同类型的结果)
你可以模仿其中任何一个,但第二个更强大。
如果您使用JDialog
代替JFrame
,则会自动处理等待完成的部分。
一些让您加快速度的术语:
public int getQans() { return qans; }
qans
,set
等)