【问题标题】:How to access components of jFrame from other class?如何从其他类访问 jFrame 的组件?
【发布时间】:2013-03-21 07:39:25
【问题描述】:

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

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。 主要代码行是 -

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;

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

    public void st()

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

}

我要做的是单击按钮(jbutton1),并且必须在Label(l)中设置写在textfield(f)中的文本。 当我单击按钮时,控制台上打印了“呼叫”,但标签上的文本没有改变。问题是什么?请帮忙。

【问题讨论】:

  • 很抱歉,'tf' 是错误的。它只有'f'。我已经编辑了问题。
  • 1) 请参阅The Use of Multiple JFrames, Good/Bad Practice? 2) import java.awt.Label; public class NewJFrame extends javax.swing.JFrame { 除非必要,否则不要混合使用 Swing (JFrame) 和 AWT (Label) 组件。在这种情况下,使用JLabel 而不是Label。 3) 除非必要,否则不要扩展框架(或对话框)。在这种情况下,只需使用组件的实例即可。

标签: java swing inheritance


【解决方案1】:

为什么不直接改JLabell的文字呢?通过以下方式:

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

更新
好的,你去吧,如果你想通过子类来做:

把jButton1ActionPerformed改成这样:

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

并将NewClass的构造函数更改如下:

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

【讨论】:

  • 我想通过使用另一个类来做到这一点。 (继承)
  • 谢谢...它的工作。您能否解释一下或提供一些链接来阅读它?
  • @AjaySainy:首先,我希望你通过这个官方的继承教程docs.oracle.com/javase/tutorial/java/IandI/subclasses.html ..其次,我建议你以使用对象的方式创建你的类使用适当的 setter 和 getter 方法更改超类组件的子类而不是超类。在上面的例子中,继承什么也没做。只有成员访问才起作用。。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 2014-10-06
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
相关资源
最近更新 更多