First: here are the instructions I have been given.
"As you can see, this GUI shall have three labels, three text fields, a text area, and a single button. Upon entering text, and pressing the button, your GUI should create an instance of your user-defined Dog class. Then, when printing back to the GUI, use methods defined within the Dog class to do this in an easier way than we had done before (I’d suggest using the toString() method)."
My problem is getting the toString method to work, I can get text from the textFields and output it to a string but that's just not what I am being asked to do. I've tried various ways to pull a String from the Dog class and output it in GUI(Lab3) class, but just don't know what I need to do. Any help is greatly appreciated.
public class Lab3 extends JFrame {
public JLabel nameLabel, breedLabel, ageLabel;
public JTextField nameField, breedField, ageField;
public JButton dogButton = new JButton("Generate Dog");
public JTextArea textArea;
public JPanel panel1, panel2, panel3;
public String dogName, dogBreed, dogAge;
public Lab3() {
nameLabel = new JLabel("Name");
breedLabel = new JLabel("Breed");
ageLabel = new JLabel("Age");
nameField = new JTextField(20);
breedField = new JTextField(20);
ageField = new JTextField(20);
panel1 = new JPanel();
panel1.add(nameLabel);
panel1.add(breedLabel);
panel1.add(ageLabel);
panel1.add(nameField);
panel1.add(breedField);
panel1.add(ageField);
panel1.setLayout(new GridLayout(2,3));
add(panel1, BorderLayout.NORTH);
panel2 = new JPanel();
panel2.add(dogButton);
DogListener listener1 = new DogListener();
dogButton.addActionListener(listener1);
add(panel2, BorderLayout.CENTER);
textArea = new JTextArea(20,20);
textArea.setEditable(false);
panel3 = new JPanel();
panel3.add(textArea);
add(panel3, BorderLayout.SOUTH);
}
class DogListener implements ActionListener {
public void actionPerformed(ActionEvent e){
dogName = nameField.getText();
dogBreed = breedField.getText();
dogAge = ageField.getText();
textArea.setText("\n" + "Name: " + dogName + "\n" + "Breed: " + dogBreed + "\n" + "Age: " + dogAge );
}
}
public static void main(String[] args){
Lab3 frame = new Lab3();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setTitle("Dog Generator");
frame.setVisible(true);
frame.setSize(600,450);
}
}
__________________________________________________ ________________________________
Second the Dog class w/methods
public class Dog extends Lab3 {
public Dog(){
}
public Dog(String dogName, String dogBreed, String dogAge) {
this.dogName = dogName;;
this.dogBreed= dogBreed;
this.dogAge = dogAge;
}
public String getName() {
return dogName;
}
public String getBreed(){
return dogBreed;
}
public String getAge() {
return dogAge;
}
public String toString() {
return "Name: " + dogName + "\n" + "Breed: " + dogBreed + "/n" + "Age: " + dogAge;
}
}