Java - 如何使表单 return 成为一个值?
Java - How to make a form return a value?
请记住,我是一个完整的(2 周)java 初学者,可能需要向三岁孩子解释事情。
我创建了一个由主 class 调用的表单。它从另一个 class 调用一个包含六个 objects 的数组列表,并在按钮上显示前四个值。
第六项是显示在文本窗格中的字符串 'qText',而第五项不显示。到目前为止,一切都很好。
按下 a 按钮应将值 0、1、2 或 3 分配给变量 'qans'。
我希望能够检查 'qans' 是否与变量 'ans' 和 return 具有相同的值,或者是主变量 class 的 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
}
建议:
- 如果你想要一个 window,一个不代表主要应用程序(这里你的 "main class" 不管它是什么)到 return 一个值,那么不要使用永远不能以模式方式使用的 JFrame,而是使用模式对话框,例如通过模式 JDialog。
- 出于这个原因和许多其他原因,您应该避免让您的 GUI classes 扩展顶级 windows,例如 JFrame(或 JDialog)。相反,让您的 GUI classes 生成 JPanes,这些可以放在最适合您需要的顶层 window。
- 您正在 隐藏 您的 qans 变量,这样无论按下什么按钮,该字段都不会改变,这意味着您正在 重新声明 方法或构造函数中的变量,从而创建和设置状态(将值赋给)局部变量,并且这样做不会更改 class 实例字段的状态。解决方案不是 重新声明 变量。因此,在执行操作的方法中,将
int qans = 1;
更改为 qans = 1;
,对于所有其他类似的代码位也是如此。
看看这两个 JFileChooser
and JOptionPane
,它们都是 'forms'(java 术语中的 'windows'),但 return 它们的值在不同方面。
JOptionPane
有一个显示对话框的静态方法,等待它完成,return立即得到结果。
JFileChooser
通过创建一个实例(就像你做的那样),使其可见(像你做的那样),等待它完成,完成后你可以通过调用询问 object 'getter' 方法(询问不同类型的结果)
您可以模仿其中任何一种,但第二种更强大。
如果您使用 JDialog
而不是 JFrame
,waiting-until-done 部分会自动为您处理。
一些帮助您快速上手的术语:
- Window - 您可以创建每个单独的 GUI
- 框架 - window 带有标题栏、关闭按钮等
- 对话框 - 'modal'
的框架
- 模态 - 意味着它阻止执行任何使其可见的东西,直到它变得不可见
- getter - return 字段的方法:如
public int getQans() { return qans; }
- 字段 - 在 class 中定义的变量(您的
qans
、set
等)
请记住,我是一个完整的(2 周)java 初学者,可能需要向三岁孩子解释事情。
我创建了一个由主 class 调用的表单。它从另一个 class 调用一个包含六个 objects 的数组列表,并在按钮上显示前四个值。 第六项是显示在文本窗格中的字符串 'qText',而第五项不显示。到目前为止,一切都很好。
按下 a 按钮应将值 0、1、2 或 3 分配给变量 'qans'。
我希望能够检查 'qans' 是否与变量 'ans' 和 return 具有相同的值,或者是主变量 class 的 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
}
建议:
- 如果你想要一个 window,一个不代表主要应用程序(这里你的 "main class" 不管它是什么)到 return 一个值,那么不要使用永远不能以模式方式使用的 JFrame,而是使用模式对话框,例如通过模式 JDialog。
- 出于这个原因和许多其他原因,您应该避免让您的 GUI classes 扩展顶级 windows,例如 JFrame(或 JDialog)。相反,让您的 GUI classes 生成 JPanes,这些可以放在最适合您需要的顶层 window。
- 您正在 隐藏 您的 qans 变量,这样无论按下什么按钮,该字段都不会改变,这意味着您正在 重新声明 方法或构造函数中的变量,从而创建和设置状态(将值赋给)局部变量,并且这样做不会更改 class 实例字段的状态。解决方案不是 重新声明 变量。因此,在执行操作的方法中,将
int qans = 1;
更改为qans = 1;
,对于所有其他类似的代码位也是如此。
看看这两个 JFileChooser
and JOptionPane
,它们都是 'forms'(java 术语中的 'windows'),但 return 它们的值在不同方面。
JOptionPane
有一个显示对话框的静态方法,等待它完成,return立即得到结果。
JFileChooser
通过创建一个实例(就像你做的那样),使其可见(像你做的那样),等待它完成,完成后你可以通过调用询问 object 'getter' 方法(询问不同类型的结果)
您可以模仿其中任何一种,但第二种更强大。
如果您使用 JDialog
而不是 JFrame
,waiting-until-done 部分会自动为您处理。
一些帮助您快速上手的术语:
- Window - 您可以创建每个单独的 GUI
- 框架 - window 带有标题栏、关闭按钮等
- 对话框 - 'modal' 的框架
- 模态 - 意味着它阻止执行任何使其可见的东西,直到它变得不可见
- getter - return 字段的方法:如
public int getQans() { return qans; }
- 字段 - 在 class 中定义的变量(您的
qans
、set
等)