Java事件侦听器代码不符合预期(初学者)

时间:2011-08-02 14:35:35

标签: java swing event-handling windowlistener

所以我现在更新了代码:但它仍然没有将字符串设置为输入。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Funclass extends JFrame implements WindowListener {

    FlowLayout layout = new FlowLayout();
    String[] Skillz = {"Analytical", "Numerical", "Leadership",
        "Communication", "Organisation", "Interpersonal"};
    String[] ROLEZ = {"Developer", "Sales", "Marketing", "Consultant"};
    String[] Industries = {"Consulting", "Tech"};
    public String R1, R2, R3, R4, RETURNROLE,
        RETURNTYPE, RETURNLIST, RETURNCOMPANY;
    JTextField Company = new JTextField("Company Name");
    JComboBox TYPE = new JComboBox(Industries);
    JList Skills = new JList(Skillz);
    JComboBox ROLE = new JComboBox(ROLEZ);
    JButton Submit = new JButton("Submit");

    public Funclass() {
        super("Input Interface");
        setLayout(layout);

        Skills.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        add(Company);
        add(TYPE);
        add(ROLE);
        add(Skills);

        Company.addActionListener(
            new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    if (event.getSource() == Company);
                    RETURNCOMPANY = event.getActionCommand();
                }
            });

        Skills.addListSelectionListener(
            new ListSelectionListener() {

                public void valueChanged(ListSelectionEvent event) {
                    RETURNLIST = (String) Skills.getSelectedValue();

                }
            });

        TYPE.addItemListener(
            new ItemListener() {

                public void itemStateChanged(ItemEvent event) {
                    if (event.getStateChange() == ItemEvent.SELECTED) {
                        RETURNTYPE = Industries[TYPE.getSelectedIndex()];
                    }
                }
            });

        ROLE.addItemListener(
            new ItemListener() {

                public void itemStateChanged(ItemEvent event) {
                    if (event.getStateChange() == ItemEvent.SELECTED) {
                        RETURNROLE = ROLEZ[ROLE.getSelectedIndex()];
                    }
                }
            });
    }

    public void windowClosed(WindowEvent e) {
        CreateCoverLetter creatorObject = new CreateCoverLetter();
        creatorObject.openFile();
        creatorObject.addBody(RETURNCOMPANY, RETURNROLE, RETURNLIST);
        creatorObject.closeFile();
        System.out.println(RETURNCOMPANY);
    }

    public void windowDeiconified(WindowEvent e) {}

    public void windowActivated(WindowEvent e) {}

    public void windowClosing(WindowEvent e) {}

    public void windowDe(WindowEvent e) {}

    public void windowIconified(WindowEvent e) {}

    public void windowDeactivated(WindowEvent e) {}

    public void windowOpened(WindowEvent e) {}
}

我是一个完全的初学者,我尝试构建一个简单的程序,该程序将从基本GUI获取用户输入并使用它们来创建文件。出于某种原因,当我运行此程序时,代码会执行,但字符串变量RETURNROLERETURNLISTRETURNCOMPANY未设置为用户输入的内容。任何人都可以解释原因吗?

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.event.*;

public class Funclass extends JFrame{
FlowLayout layout = new FlowLayout();
String[] Skillz = {"Analytical", "Numerical", "Leadership", "Communication",         "Organisation", "Interpersonal"};
String[] ROLEZ = {"Developer", "Sales", "Marketing", "Consultant"};
String[] Industries = {"Consulting", "Tech"};
public String R1, R2, R3, R4, RETURNROLE, RETURNTYPE, RETURNLIST, RETURNCOMPANY;
JTextField Company = new JTextField("Company Name");
JComboBox TYPE = new JComboBox(Industries);
JList Skills = new JList(Skillz);
JComboBox ROLE = new JComboBox(ROLEZ);
public Funclass(){
    super("Input Interface");
    setLayout(layout);

    Skills.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    add(Company);
    add(TYPE);
    add(ROLE);
    add(Skills);

    Company.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    if(event.getSource()==Company);
                    RETURNCOMPANY = event.getActionCommand();
                }
            }

            );


    Skills.addListSelectionListener(
            new ListSelectionListener(){
                public void valueChanged(ListSelectionEvent event){
                    RETURNLIST = (String) Skills.getSelectedValue();

                }
            });


    TYPE.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    if(event.getStateChange()==ItemEvent.SELECTED)
                        RETURNTYPE = Industries[TYPE.getSelectedIndex()];
                        }
            }
    );


    ROLE.addItemListener(
            new ItemListener(){
            public void itemStateChanged(ItemEvent event){
            if(event.getStateChange()==ItemEvent.SELECTED)
            RETURNROLE = ROLEZ[ROLE.getSelectedIndex()];
            }
            }
            );

    CreateCoverLetter creatorObject = new CreateCoverLetter();
    creatorObject.openFile();
    creatorObject.addBody(RETURNCOMPANY, RETURNROLE, RETURNLIST);
    creatorObject.closeFile();
    System.out.println(RETURNCOMPANY);
}

}   

3 个答案:

答案 0 :(得分:2)

您正在类的构造函数中创建输出文件,因此在用户甚至有机会填写或选择任何值之前创建文件。

我会添加一个提交按钮或其他内容,然后拉出UI元素的内容然后创建文件。您不需要所有不同的监听器,因为您可以在“提交”按钮上的ActionEvent时读取值。

答案 1 :(得分:2)

发生这种情况是因为您在定义侦听器之后,在触发任何将为这些变量赋值的事件之前,正在构造函数中写入文件。

例如,您可以在用户关闭窗口时写入文件,方法是使您的类实现WindowListener,然后实现windowClosed事件:

public class Funclass extends JFrame implements WindowListener { 

  // ...

  public void windowClosed(WindowEvent e) {
    CreateCoverLetter creatorObject = new CreateCoverLetter();
    creatorObject.openFile();
    creatorObject.addBody(RETURNCOMPANY, RETURNROLE, RETURNLIST);
    creatorObject.closeFile();
    System.out.println(RETURNCOMPANY);
  }
}

答案 2 :(得分:2)

Company添加ActionListener()

时出现了一些错误
Company.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == Company) {
                RETURNCOMPANY = event.getActionCommand();
            }
        }
    });

如果你想听一些改变

JTextField Company = new JTextField("Company Name");

然后你必须实现Document Listener,例子here