我想从textfield contractNo获取文本。当前值从另一个类导入。但是,当我从textfield contractNo的内容设置String变量契约的值时,错误指出:"非静态变量契约不能从静态上下文引用,非静态变量contractNo不能从静态背景"
代码如下:
public static void main(final String user, final String cNo) {
/* 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(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditContract.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() {
EditContract editC = new EditContract();
editC.contractNo.setText(cNo);
editC.encoder.setText(user);
editC.setVisible(true);
fillData();
}
private void fillData() {
try {
contract = contractNo.getText();
String sql = "Select engager, contactInfo, eventDate, eventtime, address, menu, contract, referred "
+ "from kusinanikambal.contracts where contractno = 1234";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
ex.printStackTrace();
}
}
});
}
答案 0 :(得分:2)
您的代码已损坏,需要多次重要修复。
fillData()
方法闻起来就像它不属于你所说的那样。 修改强>
您在评论中说明:
Filldata实际上应该在程序初始化时执行。
修改2
澄清一下,如果我的GUI类有一个JTextField并且我需要从其他地方访问它,我会做类似的事情:
public class MyGui {
private JTextField myField = new JTextField(10);
public String getFieldText() {
return myField.getText();
}
// ....
}
然后,如果他们需要访问文本,其他类可以在MyGui实例上调用getFieldText()
方法。