在单独的类中使用ActionListener会使用原始类中的JTextfield数据

时间:2012-12-04 23:57:45

标签: java swing awt actionlistener jtextfield

我正在尝试学习Swing和JFrame,所以我创建了一个非常简单的程序,询问你的名字,然后显示一个框,告诉你你输入了什么。 我试图使用第一个单独的类作为ActionListener并显示输入的名称。但它不适合我。我尝试在第二个类中创建一个构造函数,它将实例变量设置为JTextfield中的值,但它没有像我预期的那样显示。请看看;

The prompt screen

I want it to say "You've Submitted the name Imray"

这是我的代码(我已正确导入所有库但为了空间而省略)

这是主要班级......

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 );
    }
}

3 个答案:

答案 0 :(得分:4)

您在创建StringActionListener时将其传入SubmitButton类,并且JTextField {{1所以什么都没有显示出来。

您可以传递textBoxToEnterName textBoxToEnterName,以便在需要时访问该值:

JTextField

答案 1 :(得分:2)

有问题:

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

对于侦听器,您传递的是String而不是textfield,将其更改为接受textfield:)

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

询问文本字段当前值

答案 2 :(得分: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); 
    }
}