简体   繁体   English

将JButton操作从一个类传递到另一个类的JTextfield

[英]Passing JButton action from one class to JTextfield in another class

I am experimenting JButton's action and I am trying to clear a textfield in class Test2 using a button in class Test1. 我正在试验JButton的动作,并尝试使用Test1类中的按钮清除Test2类中的文本字段。 Here is the code 这是代码

public class Test2 {
private JFrame frame;
private JTextField t1;
private JTextField t2;

/**
 * Launch the application.
 */
public void start() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test2 window = new test2();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public test2() {
    initialize();
}
public void Reset(){
    t1 = new JTextField();
    t1.setText("");

}
/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    t1 = new JTextField();
    t1.setColumns(10);
    t1.setText("Start");


    GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addGap(35)
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(t2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(t1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(281, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addGap(38)
                .addComponent(t1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(75)
                .addComponent(t2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(98, Short.MAX_VALUE))
    );
    frame.getContentPane().setLayout(groupLayout);
}

} }

public class Test1 {

private JFrame frame;



/**
 * Launch the application.
 */
static test1 window = new test1();
static test2 window2 = new test2();
private JTextField textField;
private JTextField ownText;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                window2.start();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 * @wbp.parser.entryPoint
 */
public test1() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String test ="";
            window2.Reset(test);

        }
    });
    frame.getContentPane().add(btnNewButton, BorderLayout.WEST);

    ownText = new JTextField();
    frame.getContentPane().add(ownText, BorderLayout.EAST);
    ownText.setColumns(10);


}

} }

At the moment, when I click the button in Test1 class, the textfield in Test2 class is not cleared. 此刻,当我单击Test1类中的按钮时,不会清除Test2类中的文本字段。 Hope to have advice from all the seniors here. 希望从这里得到所有前辈的建议。 Let me know too if my questions has any shortcomings. 如果我的问题有任何不足之处,也请告诉我。 Thank you so much. 非常感谢。

Updated your Reset() method with this 以此更新了您的Reset()方法

public void Reset(){
        t1.setText("");     
}

You can't instantiate the Text Field again in Reset method.Because frame contain text field.Please read the Java Coding rule use reset() instead of Reset() 您不能在Reset方法中再次实例化Text Field。因为框架包含text field。请阅读Java编码规则,使用reset()而不是Reset()

UPDATED Also one thing is you use two instant of Test2 . 更新还有一件事是您使用Test2两个瞬间。 So you make your start() method in Test2 like that 因此,您可以像这样在Test2创建start()方法

public void start() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //test2 window = new test2();
                 frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM