I have tried this on my other jframe:
public class frmB extends javax.swing.JFrame {
public frmB() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
frmA a = new frmA();
a.jTextField1.setEditable(true);
}
jButton1ActionPerformed
method, you're doing just that, creating a new frmA object, one that is completely unrelated to the other one that is displayed. Note that your problem has nothing to do with Swing and all to do with basic Java -- any class can have multiple instances made from it, and each one will be unique and fully distinct from the other. Changing the state of one instance will have no effect on any of the others.
You ask if I can make code out of it, and so sure, here's an example.
Say you have a class A that has a JTextField called textField and a method called setText(String text)
that takes the String passed in and places it into the textField:
// this is in class A
public void setText(String text) {
textField.setText(text);
}
And your situation you have a class B1 with a JButton that tries to call a method of A, but does it by creating a new A object like so:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
A a = new A(); // creates a new A object
a.setText("Hello from B1!"); // and calls its method
}
});
But again, this won't work since the code above creates a new A object which is not the same as the displayed A. The key is to pass a reference of A into this class and then call the method off of it, like my B2 class so:
class B2 extends JPanel {
private JButton button = new JButton("Press ME");
private A a; // field to hold a reference to A object
public B2(final A a) { // constructor parameter to get A refrence
this.a = a; // assign reference
setBorder(BorderFactory.createTitledBorder("B2"));
add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
a.setText("Hello from B2!"); // now can use the reference in program
}
});
}
}
A runnable example program:
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Foo {
public static void main(String[] args) {
A a = new A();
B1 b1 = new B1();
B2 b2 = new B2(a);
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(a);
panel.add(b1);
panel.add(b2);
JOptionPane.showMessageDialog(null, panel);
}
}
class A extends JPanel {
private JTextField textField = new JTextField(10);
public A() {
add(textField);
textField.setEditable(false);
textField.setFocusable(false);
setBorder(BorderFactory.createTitledBorder("A"));
}
public void setText(String text) {
textField.setText(text);
}
}
class B1 extends JPanel {
private JButton button = new JButton("Press ME -- this does nothing");
public B1() {
add(button);
setBorder(BorderFactory.createTitledBorder("B1"));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
A a = new A();
a.setText("Hello from B1!");
}
});
}
}
class B2 extends JPanel {
private JButton button = new JButton("Press ME -- this works!!");
private A a;
public B2(final A a) {
this.a = a;
setBorder(BorderFactory.createTitledBorder("B2"));
add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
a.setText("Hello from B2!");
}
});
}
}
为什么在要让它们都将frmA作为参数传递给frmB的类中创建一个新的jframe
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.