JTextField在Netbeans IDE中无法正常工作

时间:2016-02-21 01:21:02

标签: java netbeans

我有一个名为MainFrem

的班级
public class MainFrem extends javax.swing.JFrame {

    public MainFrem() {
        initComponents();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ok.setText("ok");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(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(74, 74, 74)
                .addComponent(ok)
                .addContainerGap(144, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(ok)
                .addContainerGap(70, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
        new SecondClass().setVisible(true);
        System.out.println("Ok button Pressed.");
        new SecondClass().FeelIn.setText("Operation Successful.");

    }                                  

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrem().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton ok;
    // End of variables declaration                   
}

第二类是

public class SecondClass extends javax.swing.JFrame {
    public SecondClass() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        FeelIn = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        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(48, 48, 48)
                .addComponent(FeelIn, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(FeelIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SecondClass().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JTextField FeelIn;
    // End of variables declaration                   
}

我使用的是NetBeans IDE,大部分代码都是由IDE生成的。在名为Mainfrem的类中,我编写了下面的代码,这是我在这两个类中编写的唯一代码。

private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
    new SecondClass().setVisible(true);
    System.out.println("Ok button Pressed.");
    new SecondClass().FeelIn.setText("Operation Successful.");

}

在名为MainFrem的类中,我使用JButton并将Action Listener设置为如上所示,并在名为SecondClass的类中添加名为FeelIn的JTextField。我正在尝试按下班级ok中的MainFrem按钮,并将SecondClass中的JTextField更改为Operation Successful.SecondClass中的JTextField未更改为Operation Successful有没有办法解决问题。

2 个答案:

答案 0 :(得分:2)

private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
    new SecondClass().setVisible(true); // An anonymous SecondClass instance
    System.out.println("Ok button Pressed.");
    new SecondClass().FeelIn.setText("Operation Successful."); // Another anonymous instance?

}

您只是在创建SecondClass的新匿名实例,这就是问题所在。基本上,你只是创建实例并抛弃它们。

答案 1 :(得分:1)

您两次都在呼叫private void okActionPerformed(java.awt.event.ActionEvent evt) { SecondClass frame = new SecondClass() frame.setVisible(true); System.out.println("Ok button Pressed."); frame.FeelIn.setText("Operation Successful."); } 。我假设你想要做的是:

{{1}}