Java Event Listener code not behaving as intended (beginner)
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 wou开发者_运维问答ld 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);
}
}
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.
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 the windowClosed
event:
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);
}
}
there is some mistake for Company
is added ActionListener()
Company.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == Company) {
RETURNCOMPANY = event.getActionCommand();
}
}
});
if you want to listening for some changes into
JTextField Company = new JTextField("Company Name");
then you have to implements Document Listener, examples here
精彩评论