我有一个名为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
有没有办法解决问题。
答案 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}}