3

我正在尝试学习 Swing 和 JFrame,因此我正在创建一个非常简单的程序,它会询问您的姓名,然后显示一个框,告诉您输入的内容。我正在尝试使用与第一个类不同的类来充当 ActionListener 并显示输入的名称。但是它对我不起作用。我尝试在第二个类中创建一个构造函数,将实例变量设置为 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

您在创建类 时将一个空值传递给类,并且一旦文本更改,它就永远不会更新,String因此不会显示任何内容。ActionListenerSubmitButtonJTextField 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

我认为您应该将 SubmitButton 类作为 NamePrompt 类的内部类。这样您就可以使用文本字段的变量,而不必将其传递给新类,这可能会使事情复杂化。

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 回答