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);
}
}
So I now have updated the code thus: but it still isn't setting the Strings to the inputs.
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) {}
}
I'm a complete beginner and I tried to build a simple program that would take user inputs from a basic GUI and user them to create a file. For some reason, when I run this programme, the code executes but the String variables RETURNROLE
, RETURNLIST
, RETURNCOMPANY
aren't being set to what the user inputs. Can anyone explain why?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在类的构造函数中创建输出文件,因此在用户有机会填写或选择任何值之前就已经创建了该文件。
我会添加一个提交按钮或其他东西,然后提取 UI 元素的内容并创建文件。您不需要所有不同的侦听器,因为您只需在“提交”按钮上发生 ActionEvent 时读取值即可。
You are creating your output file in the Constructor of your class, so the file is being created before the user has even had a chance to fill in or select any values.
I would add a Submit button or something and have that pull the contents of your UI elements and create the file then. You won't need all of the different Listeners you have since you could just read the values at the time of the ActionEvent on the Submit button.
发生这种情况是因为您在定义侦听器之后、触发任何为这些变量分配任何值的事件之前,正在构造函数中写入文件。
例如,您可以在用户关闭窗口时写入文件,方法是让您的类实现 WindowListener,然后实现 windowClosed 事件:
That happens because you are writing to the file in the constructor, right after defining the listeners, and before firing any event that would assign any value to those variables.
You can, for example, write to the file when the user closes the window, by making your class implement
WindowListener
, and then implementing thewindowClosed
event:添加
Company
时存在一些错误ActionListener()
如果您想监听某些更改
,那么您必须实现 文档侦听器,示例 此处
there is some mistake for
Company
is addedActionListener()
if you want to listening for some changes into
then you have to implements Document Listener, examples here