📄 coursespanel.java
字号:
gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 18);
jPanel2.add(tfCredits, gridBagConstraints);
tfSectionPrefix.setToolTipText("usually a single character. may be left blank.");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 8;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 18);
jPanel2.add(tfSectionPrefix, gridBagConstraints);
tfHowMany.setToolTipText("add this many sections to the list at one time.");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 10;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 18);
jPanel2.add(tfHowMany, gridBagConstraints);
jButton2.setText("Add Course Sections");
jButton2.setToolTipText("Add these sections to the list");
jPanel4.add(jButton2);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridy = 11;
gridBagConstraints.gridwidth = 6;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(17, 0, 17, 0);
jPanel2.add(jPanel4, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(17, 17, 0, 17);
add(jPanel2, gridBagConstraints);
}//GEN-END:initComponents
private void tfNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfNameActionPerformed
// Add your handling code here:
}//GEN-LAST:event_tfNameActionPerformed
private void tfPrefixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfPrefixActionPerformed
// Add your handling code here:
}//GEN-LAST:event_tfPrefixActionPerformed
public void update(java.util.Observable observable, Object obj) {
ptm.fireTableDataChanged();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField tfCredits;
private javax.swing.JTextField tfHowMany;
private javax.swing.JTextField tfName;
private javax.swing.JTextField tfNumber;
private javax.swing.JTextField tfPrefix;
private javax.swing.JTextField tfSectionPrefix;
// End of variables declaration//GEN-END:variables
private CoursesPanelListener coursesPanelListener;
private Schedule schedule = Schedule.getSchedule();
private CoursesTableModel ptm;
private TableSorter sorter;
private TableColumn prefixColumn;
private TableColumn numberColumn;
private TableColumn sectionColumn;
private TableColumn nameColumn;
private TableColumn creditsColumn;
private JTextField editPrefix = new JTextField();
private JTextField editNumber = new JTextField();
private JTextField editSection = new JTextField();
private JTextField editName = new JTextField();
private JTextField editCredits = new JTextField();
class CoursesPanelListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("I hear " + "|"+e.getActionCommand()+"|");
if (e.getActionCommand().equals("Add Course Sections")){
// Check for valid info
String prefix = tfPrefix.getText().trim().toUpperCase();
String courseNumber = tfNumber.getText().trim();
String courseName = tfName.getText().trim();
String creditHoursString = tfCredits.getText().trim();
int creditHours = 0;
try {
creditHours = Integer.parseInt(creditHoursString);
} catch (Exception ex) {
// if it didn't parse try a double
try {
creditHours = (int) Double.parseDouble(creditHoursString);
} catch (Exception ex2){
// if it didn't parse just ignore the field.
}
}
if (prefix.length() != 0
&& courseNumber.length() != 0
&& courseName.length() != 0) {
int sections = getSectionCount();
String sectionPrefix;
if (tfSectionPrefix.getText().trim().length() == 0)
sectionPrefix = "Sect";
else
sectionPrefix = tfSectionPrefix.getText().trim();
// Create the course sections and add each one to the vector and the list
for (int i = 1; i <= sections; i++) {
Course hold = new Course(prefix, courseNumber, courseName,
sectionPrefix, i, creditHours);
schedule.addCourse(hold);
}
// reset the widgets to blank
tfPrefix.setText("");
tfNumber.setText("");
tfName.setText("");
tfSectionPrefix.setText("");
tfHowMany.setText("");
}
} else if (e.getActionCommand().equals("Delete Selected Course(s)")){
System.out.println("I'm deleting a course over here");
// Determine which items to be deleted
int[] selected = jTable1.getSelectedRows();
for (int i = selected.length - 1 ; i >= 0 ; i--) { // Why backwards??? Becuase its a vector and shifts down
// for (int i = 0; i < selected.length; i++) {
int realIndex = sorter.map(selected[i]); // Whats this for???
// Removes the items from the list
schedule.removeCourse((Course)
schedule.getCourses().get(realIndex));
}
}
}
private int getSectionCount() {
int sections;
try {
sections = Integer.parseInt(tfHowMany.getText());
}
catch (Exception e) {
sections = 1;
}
System.out.println("Making sections: " + sections);
return sections;
}
} // end inner class
/** this one is to hear the modifications inthe table columns.
*/
class CourseColListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int row = jTable1.getSelectedRow();
int theRealIndex = sorter.map(row);
Course courseModified = (Course)schedule.getCourses().get(theRealIndex);
if (e.getSource() == editPrefix){
System.out.println("I hear edit the prefix");
String prefixString = editPrefix.getText().trim().toUpperCase();
courseModified.setField(prefixString);
} else if (e.getSource() == editNumber){
String prefixString = editNumber.getText().trim();
courseModified.setCourseNumber(prefixString);
} else if (e.getSource() == editSection) {
String sectionPrefix, sectionNumber;
String whole = editSection.getText().trim();
if (! (whole.indexOf('-') > 0)){
} else {
StringTokenizer stTok = new StringTokenizer(whole, "-");
if(stTok.hasMoreTokens())
courseModified.setSectionPrefix(stTok.nextToken());
if(stTok.hasMoreTokens()){
String number = stTok.nextToken().trim();
try{
int num = Integer.parseInt(number);
courseModified.setSectionNumber(num);
} catch (NumberFormatException nfe){
// do nothing, bail out
return;
}
}
}
} else if( e.getSource() == editName){
String nameString = editName.getText().trim();
courseModified.setCourseName(nameString);
} else if(e.getSource() == editCredits){
int creditHours;
String creditsString = editPrefix.getText().trim();
try {
creditHours = (int) Double.parseDouble(creditsString);
} catch (NumberFormatException nfe){
return; // bail out of the listener, doing nothing
}
courseModified.setCreditHours(creditHours);
}
//ptm.fireTableDataChanged();
schedule.setChanged(true);
}
} //end inner class
}
class CoursesTableModel extends AbstractTableModel {
private Schedule schedule;
final String[] columnNames = {
"Pre",
"Num",
"Section",
"Name",
"Credits"
};
/** Creates a new instance of RoomsTableModel */
public CoursesTableModel() {
schedule = Schedule.getSchedule();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return schedule.getCourses().size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int c) {
return " ".getClass();
}
public boolean isCellEditable(int row, int col) {
return true;
}
public Object getValueAt(int row, int col) {
Course course = (Course)schedule.getCourses().get(row);
switch(col){
case 0: return course.getField();
case 1: return course.getCourseNumber();
case 2: return course.getSectionString();
case 3: return course.getCourseName();
case 4: return ""+course.getCreditHours();
}
return ""; // should never do this
}
public void setValueAt(Object value, int row, int col) {
//fireTableCellUpdated(row, col);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -