1

I have Jframe that has a JTextField and a JButton. It should return text of Jtextfield to anotherClass (MainPage). but when program starts, It returns null to the class.

    public class JframeFoo extends JFrame {

    private String username = new String();

    public JframeFoo() {
        // --------------------------------------------------------------
        // Making Frame for login

        final JTextField usernameFiled = new JTextField();
        this.add(usernameFiled);

        JButton signinButton = new JButton();
        // ------------------------------------------------------------

        signinButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                setVisible(false);
                Main.mainpage.setVisible(true);

            }
        });
        // ----------------------------------------------------------------------
        username = usernameFiled.getText();
    }

    public String getuserName() {
        return this.username;
    }
}

(this Jframe should run at the start of program and when it gets text, it should go to invisible and another class should become visible.)

4

3 に答える 3

3

username = usernameField.getText() への呼び出しを actionPerformed メソッドに移動する必要があります。現在の方法でのみ null に設定されます。

于 2013-07-30T13:16:35.403 に答える
3

JFrameFoo()そのフレームが作成されると、コンストラクターが呼び出されます。したがって、この行:

username = usernameFiled.getText();

も同時に呼び出されます。代わりにやりたいことは次のとおりです。

signinButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {            
        username = usernameFiled.getText();
        setVisible(false);
        Main.mainpage.setVisible(true);
    }
});

編集

私が期待しているのは、userName初期化される前にメインクラスで使用することです。次の 2 点をお勧めします。

  1. イベント駆動プログラミングとコールバックについて学びます。ソース内である行が別の行の下にあるという単純な事実は、後で実行されるという意味ではありません。
  2. を呼び出す代わりに、次のmainPage.setVisibleようなことを行います

signinButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {            
        setVisible(false);
        Main.mainpage.open(usernameFiled.getText());
    }
});

そのメソッドをメインページに追加して、次のようにします

public void open(String username) {
    this.setVisible(true);
    // do whatever you want to do with username
}
于 2013-07-30T13:17:15.493 に答える
2

オーバーライドされたメソッドgetText()内からメソッドを呼び出すことに加えて、actionPerformedthis.dispose();setVisible(false);

したがって、コードは次のようになります。

        signinButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                username = usernameFiled.getText();
                if ((username != null) || !(username.length() == 0)) {
                    this.dispose();
                    Main.mainpage.setVisible(true);
                } else {
                    // Appropriate error here...
                }
            }
        });

getText()内部から呼び出すactionPerformedと、フレームを破棄して続行する前に、ユーザー名変数でいくつかのチェックを行うこともできます (上記のスニペットを参照してください)。

幸運を!

于 2013-07-30T13:31:57.470 に答える