如何从其他类访问jFrame的组件? [英] How to access components of jFrame from other class?

查看:82
本文介绍了如何从其他类访问jFrame的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,第一个是NewJFrame.java.它的代码是-

I have two classes, first one is NewJFrame.java. Its code is-

package javaapplication10;
import java.awt.Label;
public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }



    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        f = new javax.swing.JTextField();
        l = new java.awt.Label();

        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);
            }
        });

        l.setText("label1");

        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(139, 139, 139)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(f, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(l, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(185, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(f, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(22, 22, 22)
                .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(186, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("Calling");
        NewClass1 n = new NewClass1();
        n.st();

    }                                        



    public static void main(String args[]) {

        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);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    protected javax.swing.JTextField f;
    protected javax.swing.JButton jButton1;
    protected java.awt.Label l;

}

我正在使用netbeans. 代码的主要行是-

I am using netbeans. Main lines of codes are -

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.out.println("Calling");
            NewClass1 n = new NewClass1();
            n.st();

        } 

和变量声明

protected javax.swing.JTextField f;
protected javax.swing.JButton jButton1;
protected java.awt.Label l;

我的第二个类是NewClass.java,其代码是- 包javaapplication10;

My second class is NewClass.java and its code is - package javaapplication10;

import java.awt.Label;
public class NewClass1 extends NewJFrame{

    public void st()

    {
        l.setText(f.getText());
    }

}

我要做的是单击按钮(jbutton1),并且必须在Label(l)中设置在textfield(f)中编写的文本. 当我单击按钮时,控制台上会打印呼叫",但标签"上的文本不会更改.问题是什么?请帮忙.

what I want to do is to click on the button(jbutton1) and the text written in the textfield(f) must be set in the Label(l). When I am clicking on the button "Calling" is printed on the console but the text on Label is not changing. What is the problem? Please Help.

推荐答案

为什么不直接更改JLabel l的文本?通过以下方式:

Why not you directly change the text of JLabel l? in following way:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
  System.out.println("Calling");
  //NewClass1 n = new NewClass1();
  //n.st();
   l.setText(f.getText());   
}    

更新
好的,如果您想通过子类完成此操作,请转到这里:

UPDATE
Ok here u go , if you wish to do it via child class:

jButton1ActionPerformed更改为这种方式:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
  System.out.println("Calling");
  NewClass1 n = new NewClass1(this);
  n.st();
}

并按如下所示更改NewClass的构造函数:

And Change the constructor of NewClass as follows:

NewJFrame njf;
public NewClass(NewJFrame jf)
{
  this.nfj = jf;
}
public void st()
{
  nfj.l.setText(nfj.f.getText());
}

这篇关于如何从其他类访问jFrame的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆