dimanche 29 mars 2015

How do I change the field names in this java GUI code..?

Here's the code I wrote out but my instructor decided to change things in the GUI field names and I can't understand what she meant and it's due tomorrow and she's not responding to e-mails


Here's her course changes:



Online;Java 1;Davis;125.00;1;1;2015;2;1;2015;programming;UTA;true;12
Online;Relieve Stress;Jones;35.00;3;2;2015;3;31;2015;misc;none;false;5
Online;Painter 2015;Vikram;59.00;3;2;2015;3;31;2015;painting;TCU;false;8
Inclass;Canon Pictures;Long;75.00;1;15;2015;2;3;2015;photography;COB142;17;30;18;50

Here's the code I typed out



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class StudentGUI extends JFrame
{
private JLabel nameLabel; //4 rows: name, id, class, major
private JTextField nameField;
private JLabel idLabel;
private JTextField idField;
private JLabel classLabel;
private JLabel majorLabel;

private JRadioButton freshmanButton;
private JRadioButton sophmoreButton;
private JRadioButton juniorButton;
private JRadioButton seniorButton;

private ButtonGroup classGroup;

private String majors[] = {"INSY", "MARK", "MANA", "OPMA", "FINA"};
private JComboBox<String> majorBox;

private JLabel submitLabel;
private JLabel finishLabel;
private JButton submitButton;
private JButton finishButton;

JPanel classPanel;

ArrayList<Student> studentList = new ArrayList<Student>();

public StudentGUI()
{
super("Student GUI");

//readCourses(); another method called readCourses for the HW

setLayout(new GridLayout(6,2));

nameLabel = new JLabel(" Enter Name");
nameField = new JTextField(20);

idLabel = new JLabel(" Enter ID");
idField = new JTextField(20);
idField.setToolTipText("Enter a 5-digit Integer"); //message comes up if cursor hovers over it

classLabel = new JLabel(" Select a classification");
majorLabel = new JLabel(" Select a major");

freshmanButton = new JRadioButton("FRESHMAN");
sophmoreButton = new JRadioButton("SOPHMORE");
juniorButton = new JRadioButton("JUNIOR");
seniorButton = new JRadioButton("SENIOR");

classGroup = new ButtonGroup();
classGroup.add(freshmanButton);
classGroup.add(sophmoreButton);
classGroup.add(juniorButton);
classGroup.add(seniorButton);

classPanel = new JPanel();
classPanel.setLayout(new GridLayout(1,4)); //1 row of 4 columns
classPanel.add(freshmanButton);
classPanel.add(sophmoreButton);
classPanel.add(juniorButton);
classPanel.add(seniorButton);

majorBox = new JComboBox<String>(majors);
majorBox.setMaximumRowCount(2); //doing 3 on hw

submitLabel = new JLabel(" Click to Submit");
finishLabel = new JLabel(" Click to Finish");

submitButton = new JButton("SUBMIT");
finishButton = new JButton("FINISH");

submitButton.addActionListener(new ActionListener() //**test
{
public void actionPerformed(ActionEvent ae)
{
if (confirm())
{
studentList.add(new Student(nameField.getText(), Integer.parseInt(idField.getText()), getClassification(), getMajor()));
}
clearAll();
}
});

finishButton.addActionListener(new ActionListener() //finishButton in hw calls generate invoice; action listener is an interface, has abstract methods, method to implement action listener is "actionPerformed", annonymous listener does not have a name
{
public void actionPerformed(ActionEvent ae)
{
for(Student s: studentList)
System.out.println(s.toString());
System.exit(0);
}
});
add(nameLabel); //first 2 rows
add(nameField);
add(idLabel);
add(idField);

add(classLabel);
add(classPanel); //adds all 4 buttons which are all in a group

add(majorLabel);
add(majorBox);

add(submitLabel);
add(submitButton);

add(finishLabel);
add(finishButton);
}

public static void main(String args[])
{
StudentGUI s = new StudentGUI();

s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setSize(850,350);

s.setLocationRelativeTo(null);

s.setVisible(true);
}

public void clearAll() //**test; clears the fields and resets the focus
{
nameField.setText("");
idField.setText("");
classGroup.clearSelection();
majorBox.setSelectedIndex(0);

nameField.requestFocus();
}

public String getMajor()
{
return majors[majorBox.getSelectedIndex()];
}

public String getClassification()
{
if (freshmanButton.isSelected())
return freshmanButton.getText();
else if (sophmoreButton.isSelected())
return sophmoreButton.getText();
else if (juniorButton.isSelected())
return juniorButton.getText();
else
return seniorButton.getText();
}

public boolean confirm() //**test
{
JFrame frame = new JFrame();

int result;

String message;

message = String.format("%s %s", nameField.getText(), idField.getText());

result = JOptionPane.showConfirmDialog(frame, message);

if (result==JOptionPane.YES_OPTION)
return true;
return false;
}

public class Student
{
private String name;
private int id;
private String classification;
private String major;

public Student(String n, int i, String c, String m)
{
name = n;
id = i;
classification = c;
major = m;
}

public String toString()
{
return (" Name " + name + " ID " + id + " Classification " + classification + " Major " + major );
}

}
}

Aucun commentaire:

Enregistrer un commentaire