我试图将有关学生的一些细节输入该计划。当我按下保存按钮时,会发出这样的异常错误,
"线程中的异常" AWT-EventQueue-0" java.lang.UnsupportedOperationException:尚不支持。"
public static ArrayList<Student> arrayStudent = new ArrayList<Student>();
public static void Student(Student student){
try{
BufferedWriter buffer = new BufferedWriter(new FileWriter("Students.txt", true));
buffer.write("Name : "+student.getFirstName()+" "+student.getSurname()+"\nNIC No : "+student.getNICNo()+" "+"\nBatch Code : "+student.getBatchCode()+" "+"\nStudent No : "+student.getStudentNo()+" "+"\nTelephone No : "+student.getTelNo());
buffer.newLine();
buffer.close();
}
catch(IOException ex){
Logger.getLogger(Arraylists.class.getName()).log(Level.SEVERE, null, ex);
}
public class addStudent extends JFrame {
public addStudent() {
initComponents();
setTitle("Add Student");
setBounds(0, 0, 410, 550);
setIcon();
getContentPane().setBackground(Color.BLACK);
jTextField1.setText("");
jTextField2.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
}
public void addStudent(){
Student newStudent= new Student();
newStudent.setFirstName(jTextField1.getText());
newStudent.setSurname(jTextField2.getText());
newStudent.setNICNo(jTextField4.getText());
newStudent.setBatchCode(jTextField5.getText());
newStudent.setStudentNo(jTextField6.getText());
newStudent.setTelNo(jTextField7.getText());
}
public boolean checkEmpty(){
boolean checkEmpty = false;
if(jTextField1.getText().isEmpty()||jTextField1.getText().isEmpty()||jTextField2.getText().isEmpty()||jTextField4.getText().isEmpty()||jTextField5.getText().isEmpty()||jTextField6.getText().isEmpty()||jTextField7.getText().isEmpty())
{
checkEmpty = true;
}
return checkEmpty;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Student newStudent = new Student();
newStudent.setFirstName(jTextField1.getText());
newStudent.setSurname(jTextField2.getText());
newStudent.setNICNo(jTextField4.getText());
newStudent.setBatchCode(jTextField5.getText());
newStudent.setStudentNo(jTextField6.getText());
newStudent.setTelNo(jTextField7.getText());
if(checkEmpty()){
JPanel msg = new JPanel();
JOptionPane.showMessageDialog(msg, "Please Fill all the Fields!", "Error", JOptionPane.ERROR_MESSAGE);
}
else{
addStudent();
Arraylists.arrayStudent.add(newStudent);
JPanel msg = new JPanel();
JOptionPane.showMessageDialog(msg, "Student Added Successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
newStudent = null;
this.setVisible(false);
addStudent studentAdded = new addStudent();
studentAdded.setVisible(true);
}
}
public class Student {
private String firstName;
private String surName;
private String NICNo;
private String batchCode;
private String studentNo;
private String telephoneNo;
public Student(String fName, String sName, String nicNo, String batCode, String studNo, String telNo){
this.firstName = fName;
this.surName = sName;
this.NICNo = nicNo;
this.batchCode = batCode;
this.studentNo = studNo;
this.telephoneNo = telNo;
}
public Student() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void setFirstName(String fName){
this.firstName = fName;
}
public String getFirstName(){
return firstName;
}
public void setSurname(String sName){
this.surName = sName;
}
public String getSurname(){
return surName;
}
public void setNICNo(String nicNo){
this.NICNo = nicNo;
}
public String getNICNo(){
return NICNo;
}
public void setBatchCode(String batCode){
this.batchCode = batCode;
}
public String getBatchCode(){
return batchCode;
}
public void setStudentNo(String studNo){
this.studentNo = studNo;
}
public String getStudentNo(){
return studentNo;
}
public void setTelNo(String telNo){
this.telephoneNo = telNo;
}
public String getTelNo(){
return telephoneNo;
}
}
public class addStudent extends JFrame {
public addStudent() {
initComponents();
setTitle("Add Student");
setBounds(0, 0, 410, 550);
setIcon();
getContentPane().setBackground(Color.BLACK);
jTextField1.setText("");
jTextField2.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
}
public void addStudent(){
Student newStudent= new Student();
newStudent.setFirstName(jTextField1.getText());
newStudent.setSurname(jTextField2.getText());
newStudent.setNICNo(jTextField4.getText());
newStudent.setBatchCode(jTextField5.getText());
newStudent.setStudentNo(jTextField6.getText());
newStudent.setTelNo(jTextField7.getText());
}
public boolean checkEmpty(){
boolean checkEmpty = false;
if(jTextField1.getText().isEmpty()||jTextField1.getText().isEmpty()||jTextField2.getText().isEmpty()||jTextField4.getText().isEmpty()||jTextField5.getText().isEmpty()||jTextField6.getText().isEmpty()||jTextField7.getText().isEmpty())
{
checkEmpty = true;
}
return checkEmpty;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel7 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jTextField6 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField7 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel3.setBackground(new java.awt.Color(0, 0, 0));
jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.white, java.awt.Color.white, java.awt.Color.white, java.awt.Color.white));
jPanel2.setBackground(new java.awt.Color(0, 0, 0));
jLabel7.setForeground(new java.awt.Color(255, 255, 255));
jLabel7.setText("Tel. No : +94");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("First Name :");
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Surname :");
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Sex :");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("NIC No :");
jButton1.setText("Save");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("Batch Code : CF");
jButton2.setText("Cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel6.setForeground(new java.awt.Color(255, 255, 255));
jLabel6.setText("Student Number : CB");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(39, 39, 39)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(0, 95, Short.MAX_VALUE)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2))
.addComponent(jTextField6)
.addComponent(jTextField7)
.addComponent(jTextField5))))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(27, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap())
);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jLabel8.setFont(new java.awt.Font("Segoe UI Symbol", 1, 18)); // NOI18N
jLabel8.setForeground(new java.awt.Color(255, 255, 255));
jLabel8.setText("Enter Student Details");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel8)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(21, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Student newStudent = new Student();
newStudent.setFirstName(jTextField1.getText());
newStudent.setSurname(jTextField2.getText());
newStudent.setNICNo(jTextField4.getText());
newStudent.setBatchCode(jTextField5.getText());
newStudent.setStudentNo(jTextField6.getText());
newStudent.setTelNo(jTextField7.getText());
if(checkEmpty()){
JPanel msg = new JPanel();
JOptionPane.showMessageDialog(msg, "Please Fill all the Fields!", "Error", JOptionPane.ERROR_MESSAGE);
}
else{
addStudent();
Arraylists.arrayStudent.add(newStudent);
JPanel msg = new JPanel();
JOptionPane.showMessageDialog(msg, "Student Added Successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
newStudent = null;
this.setVisible(false);
addStudent studentAdded = new addStudent();
studentAdded.setVisible(true);
}
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(addStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(addStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(addStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(addStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new addStudent().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
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.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
// End of variables declaration
private void setIcon() {
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Icon.png")));
}
}
答案 0 :(得分:1)
您的自己的程序中有代码,您可能尚未向我们展示,可能是在ActionListener中,或者是您的某个ActionListener调用的方法,它会抛出此异常。找到它并修复它,就像那样简单。
正如许多人所指出的,异常自己的Stacktrace消息会告诉你导致异常的行是什么,但我再次认为它将是一个由NetBeans创建的方法,它看起来像:
// where the X below is some number
private void jButtonXActionPerformed(java.awt.event.ActionEvent evt) {
throws new UnsupportedOperationException("Not supported yet.");
}
要解决此问题,请删除throws语句,然后添加在按下此按钮时应调用的代码。
好的,我对该位置的看法不对,但我的整体想法是正确的,您的代码正在您的学生构造函数中抛出异常:
public Student() {
throw new UnsupportedOperationException("Not supported yet.");
}
再次,摆脱这个,而是为这个构造函数添加适当的代码
public Student() {
// add appropriate code here
}