首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java中JFrame类中 公共方法调用,该如何解决

2012-04-21 
java中JFrame类中 公共方法调用在NetBeans IDE 7.1.1中创建一个JFrame窗口程序。其中包含jTextField控件。现

java中JFrame类中 公共方法调用
在NetBeans IDE 7.1.1中创建一个JFrame窗口程序。其中包含jTextField控件。现在需要在其他类中获取jTextField控件中的数据。
我是通过在JFrame中添加一个public方法,获取jTextField中的值。再到其他类中引用JFrame中的公共方法。结果在其他类中能获取jTextField中的初始值。更改后就不能即时的获取到了。
经测试:
JFrame中的public方法可以实时的获取jTextField中的值,但是经其他类一转,就变味了,这是咋解决啊、
代码如下:

Java code
public class NewJFrame extends javax.swing.JFrame {    /**     * Creates new form NewJFrame     */    public String getback()//这个就是公共方法    {        String c;        c = jTextField1.getText();       // System.out.println(c);        return  c;    }    public NewJFrame() {        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);        jTextField1.setText("erg");        jTextField1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jTextField1ActionPerformed(evt);            }        });        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()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createSequentialGroup()                        .addGap(156, 156, 156)                        .addComponent(jButton1))                    .addGroup(layout.createSequentialGroup()                        .addGap(108, 108, 108)                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))                .addContainerGap(132, Short.MAX_VALUE))        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addGap(48, 48, 48)                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addGap(63, 63, 63)                .addComponent(jButton1)                .addContainerGap(145, Short.MAX_VALUE))        );        pack();    }// </editor-fold>    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                    // TODO add your handling code here:    }                                               private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {        NewClass  s = new NewClass();//newclass是新建其他的类,ee是其中的一个方法,只是打印。        s.ee();      // String m =  getback();    //   System.out.println(m);        // 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);        } catch (InstantiationException ex) {            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);        } catch (IllegalAccessException ex) {            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);        } catch (javax.swing.UnsupportedLookAndFeelException ex) {            java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);            }        });    }    // Variables declaration - do not modify    private javax.swing.JButton jButton1;    private javax.swing.JTextField jTextField1;    // End of variables declaration} 


Java code
 /*****************************其他类****************************/public class NewClass {    public void ee(){    NewJFrame j = new NewJFrame();    String k = j.getback();    System.out.println(k);   // b = j.getback();    }    }





[解决办法]
String k = j.getback();
当k调用这句后,k就是固定的,但是j.getback()是实时的啊、
用这个试试。看能不能达到你要的实时。
public JTextField getback()
{
return this.jTextField1;
}

热点排行