3

私はSwingとJFrameを学ぼうとしているので、あなたの名前を尋ね、入力した内容を示すボックスを表示する非常に単純なプログラムを作成しています。ActionListenerとして機能し、入力された名前を表示するために、最初のクラスとは別のクラスを使用しようとしています。しかし、それは私には機能しません。2番目のクラスで、インスタンス変数をJTextfieldで取得した値に設定するコンストラクターを作成しようとしましたが、期待どおりに表示されません。ご覧ください。

プロンプト画面

言いたい

これが私のコードです(すべてのライブラリを適切にインポートしましたが、スペースのために省略しています)

これがメインクラスです...

public class NamePrompt extends JFrame{


    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField(21);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        submit.addActionListener(new SubmitButton(textBoxToEnterName.getText()));
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        //setSize(300, 150);
        pack();
        setLocationRelativeTo(null);


    }



public static void main(String[] args) {
    NamePrompt promptForName = new NamePrompt();
    promptForName.setVisible(true); 
}

}

そしてこれはActionListenerクラスです:

public class SubmitButton implements ActionListener {

    String nameInput;

    public SubmitButton(String textfield){
        nameInput = textfield;
    }

    @Override
    public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput );
    }
}
4

3 に答える 3

5

クラスの作成時に空StringActionListenerクラスに渡してSubmitButton おり、テキストが変更されると更新されJTextField textBoxToEnterNameないため、何も表示されません。

必要に応じて、を渡してtextBoxToEnterName JTextField値にアクセスできます。

class SubmitButtonListener implements ActionListener {

    private JTextField textfield;

    public SubmitButtonListener(JTextField textfield) {
        this.textfield = textfield;
    }

    @Override
    public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame, "You've Submitted the name "
                + textfield.getText());
    }
}
于 2012-12-05T00:08:00.327 に答える
2

NamePromptクラスの内部クラスとしてSubmitButtonクラスが必要だと思います。このようにして、テキストフィールドの変数を新しいクラスに渡さなくても使用できます。これにより、処理が複雑になる可能性があります。

class SubmitButton implements ActionListener {

//     Do not need this
   // public SubmitButton(String textfield){
     //   nameInput = textfield;
   // }

@Override
public void actionPerformed(ActionEvent submitClicked) {
    Component frame = new JFrame();
    JOptionPane.showMessageDialog(frame , "You've Submitted the name " + textBoxToEnterName.getText());
}

ただし、内部クラスで使用できるように、コンストラクターの外部で変数を定義してください。

public class NamePrompt extends JFrame{


private static final long serialVersionUID = 1L;

JLabel enterYourName;
JextField textBoxToEnterName;
JPanel panelTop;
JButton submit; 
JPanel panelBottom;

String name;

public NamePrompt(){ ..... (set up the variables here)

最終的なクラスは次のようになります。

    public class NamePrompt extends JFrame{


        private static final long serialVersionUID = 1L;

        String name;
        JLabel enterYourName;
        JTextField textBoxToEnterName;
        JPanel panelTop;
        JButton submit;
        JPanel panelBottom;


        public NamePrompt(){

            setLayout(new BorderLayout());

            enterYourName = new JLabel("Enter Your Name Here:");
            textBoxToEnterName = new JTextField(21);
            panelTop = new JPanel();
            panelTop.add(enterYourName);
            panelTop.add(textBoxToEnterName);

            submit = new JButton("Submit");
            submit.addActionListener(new SubmitButton(textBoxToEnterName.getText()));
            panelBottom = new JPanel();
            panelBottom.add(submit);

            //Add panelTop to JFrame
            add(panelTop, BorderLayout.NORTH);
            add(panelBottom, BorderLayout.SOUTH);

            //JFrame set-up
            setTitle("Name Prompt Program");
            //setSize(300, 150);
            pack();
            setLocationRelativeTo(null);
    }

    class SubmitButton implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent submitClicked) {
            Component frame = new JFrame();
            JOptionPane.showMessageDialog(frame , "You've Submitted the name " + textBoxToEnterName.getText());
        }
    }



    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true); 
    }
}
于 2012-12-05T00:28:00.427 に答える
1

問題があります:

submit.addActionListener(new SubmitButton(textBoxToEnterName.getText()))

テキストフィールドの代わりに文字列を渡すリスナーに、テキストフィールドを受け入れるように変更します:)

そしてで

public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput );
    }

テキストフィールドの現在の値を尋ねる

于 2012-12-05T00:08:39.327 に答える