简体   繁体   English

在按下JButton时摆动获取JTextField

[英]Swing get JTextField upon JButton press

Apparently my Google-fu skills are bit lacklustre and I can't figure out how to get JTextField when pressing a JButton. 显然,我的Google-fu技能有些迟钝,按JButton时我不知道如何获取JTextField。

Please note that I've removed some parts of the code for ease of reading. 请注意,为了便于阅读,我删除了部分代码。

If you see some variable that's not defined assume that it was part of that code. 如果看到一些未定义的变量,请假定它是该代码的一部分。

As it stands, the code works fine. 就目前而言,代码可以正常工作。

public final class Main {
    // Some removed code was here
    private void prepareGUI() {

        // Top right stuff
        JPanel topRightPanel = new JPanel();
        topRightPanel.setLayout(new FlowLayout());
        JLabel topRightLabel = new JLabel("Address");
        JTextField topRightTextField = new JTextField("", 15);
        topRightTextField.setName("add_address");
        JButton topRightButton = new JButton("Add");
        topRightButton.setName("add_btn");

        topRightPanel.add(topRightLabel);
        topRightPanel.add(topRightTextField);
        topRightPanel.add(topRightButton);
        mainFrame.add(topRightPanel);

        // The button in question. Very suggestive name, I know.
        topRightButton.addActionListener(new GenericButtonListener());

        genericButtonListener.setKernel(kernel);

        // some other non relevant stuff here

        mainFrame.setVisible(true);
    }

}

public class GenericButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton) e.getSource();
        String btnName = btn.getName();

        if(btnName.toLowerCase().contains("add_btn")) {
            addBtn(btn);
        }
    }

    public void addBtn(JButton button){
        SshFileIO sshFileIO = kernel.getFileIO();
        // Get field text here
    }
}

My current dilemma is how to get said textfield value inside GenericButtonListener . 我当前的困境是如何在GenericButtonListener获取所述textfield值。

I realize that I can use getText to get the text field value, however I don't have access to that variable inside the actionPerformed function. 我意识到我可以使用getText来获取文本字段值,但是我无法访问actionPerformed函数中的该变量。

I suppose this is more of a scoping problem rather than anything else. 我认为这更多是一个范围界定问题,而不是其他任何问题。

I just need some pointing in the right direction, no hand holding required. 我只需要指向正确的方向,无需手握。

It's painfully obvious that I'm very new to Java. 很痛苦的是,我对Java还是很陌生。

Please try to get a reference of topRightTextField with the constructor of GenericButtonListener. 请尝试使用GenericButtonListener的构造函数获取topRightTextField的引用。 Store as property of the class and use it inside actionPerformed. 存储为类的属性,并在actionPerformed中使用它。

Change this one: 更改此一项:

 topRightButton.addActionListener(new GenericButtonListener());

To this: 对此:

 topRightButton.addActionListener(new GenericButtonListener(topRightTextField));

And inside class GenericButtonListener add field: 并在类GenericButtonListener中添加字段:

private JTextField topRightTextField;// set it in the constructor

And then use it inside of your method actionPerformed. 然后在您的actionPerformed方法中使用它。

Have a nice coding and good luck! 祝您编程愉快,万事如意!

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

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