gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:19:01 27 4
gpt4 key购买 nike

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

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

}

最佳答案

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

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

关于Java 事件监听器代码未按预期运行(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913788/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com