【发布时间】:2013-12-06 22:45:14
【问题描述】:
我对应用程序开发很陌生,所以我希望你们能教一两件事。
我得到了 1 个JFrame 和 1 个JTextField。我使用的方法每次单击按钮时都会返回一个数字并将其写入JTextfield。
有效!但是,问题是当我希望该方法有自己的类时。
我在我的方法类中得到了这个:
打包新包;
package test;
public class class1 extends class2 {
class2 testingobj = null;
/** this is the constructor. It is given a parameter, so it knows
* about the class2
*/
public class1(class2 frame) {
this.testingobj = frame;
}
private int num = 0;
public int testing(){
if(num <=8){
num ++;
testingobj.jTextField1.setText("" +num);
}
return num;
}
}
这在我的课堂上使用Jbutton 和`JTextField':
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author Admin
*/
public class class2 extends javax.swing.JFrame {
class1 testing2ndobj = new class1(this);
/**
* Creates new form class2
*/
public class2() {
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() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(79, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(173, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
testing2ndobj.testing();
}
/**
* @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(class2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(class2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(class2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(class2.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 class2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}
问题 1) 我可以运行应用程序,但是当我按下按钮时没有任何反应。这是为什么?当我跨班级工作时,有什么我必须记住的黄金法则吗?
谢谢!
编辑:已解决。
【问题讨论】:
-
风格注释:Java 类使用 CamelCase 命名。不要使用小写的首字母。
-
真的很乱,我想试试,但不想花时间读完这本小说。
-
对于初学者,在示例中您通常应该说
Class2 extends Class1,而不是相反,并且Class2很少同时拥有-AClass1(testingobj) 和 Be -AClass1(extends)。很难准确说出您的意图。 -
看起来您的
main方法在您的actionPerformed方法中。我错过了右括号吗?如果是错字,请编辑您的问题 -
你把相关部分也拿出来了,这段代码连编译都编译不了。您必须以某种方式将事件处理程序(可能是
jButton1ActionPerformed)附加到按钮,而且很可能您没有这样做。