Java事件监听器代码未按预期运行(初学者)



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

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和用户创建一个文件。由于某种原因,当我运行这个程序时,代码执行,但是字符串变量RETURNROLE, RETURNLIST, RETURNCOMPANY没有被设置为用户输入的内容。有人能解释一下原因吗?

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

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

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

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

例如,您可以在用户关闭窗口时写入文件,方法是让您的类实现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);
  }
}

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

那么你必须实现文档监听器,这里的例子

相关内容

  • 没有找到相关文章

最新更新