Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

java - Transfer data from one Jframe to another jframe using static class or this reference?

I have one jFrame and its have one jTextbox and a button. Another jFrame have a single jLabel. I want to bring out the text written in first frame's textbox to second frame's jLabel when button is pressed. And as i have searched about this than i got some unreliable answers. But as per my knowledge it could be done by creating another static class or by calling this reference.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a question of "what" you want to achieve that will drive the "how"...

For example...

You could maintain a reference to the second frame within the first frame and when the button is clicked, tell the second frame that a change has occurred...

public class FirstFrame extends JFrame {
    // Reference to the second frame...
    // You will need to ensure that this is assigned correctly...
    private SecondFrame secondFrame;
    // The text field...
    private JTextField textField;

    /*...*/

    // The action handler for the button...
    public class ButtonActionHandler implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            secondFrame.setLabelText(textField.getText());
        }
    }
}

The problem with this is it exposes the SecondFrame to the first, allowing it to do nasty things to it, like remove all the components for example.

A better solution would be to provide a series of interfaces that would allow the two classes to talk with each other...

public interface TextWrangler {
    public void addActionListener(ActionListener listener);
    public void removeActionListener(ActionListener listener);
    public String getText();
}

public class FirstFrame extends JFrame implements TextWrangler {
    private JButton textButton;
    private JTextField textField;

    /*...*/

    public void addActionListener(ActionListener listener) {
        textButton.addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        textButton.removeActionListener(listener);
    }

    public String getText() {
        return textField.getText();
    }
}

public class SecondFrame extends JFrame {
    private JLabel textLabel;
    private JTextField textField;
    private TextWrangler textWrangler;

    public SecondFrame(TextWrangler wrangler) {
        textWrangler = wrangler;
        wrangler.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                textLabel.setText(textWrangler.getText());
            }
        });
        /*...*/
    }
}

This basically restricts what the SecondFrame can actually gain access to. While it can be argued that the ActionListener in the SecondFrame could use the ActionEvent source to find out more information, by it's nature, it would be an unreliable mechanism, as the interface makes no mention of how it should be implemented...

This is a basic example of the Observer Pattern


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...