我有一个名为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并设置动作侦听器,如上所示,在名为SecondClass
的类中,我仅添加了一个名为FeelIn
的JTextField。我试图按类ok
中的MainFrem
按钮,并将SecondClass
中的JTextField更改为Operation Successful.
。 SecondClass
中的JTextField不会更改为Operation Successful
是否有解决此问题的方法。 最佳答案
您两次都拨打new SecondClass()
。我假设您要执行的操作是:
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.");
}