有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当按下主GUI上的按钮时,javascript编辑GUI JDialog不会打开,即使正确地遵循了步骤

我是Java代码的初学者,最近遇到了一个关于编辑GUI JDialog的问题。我正在做一项任务,收集DVD上的信息,并将其添加到主GUI的JList中。虽然到目前为止,一切似乎都在运行,但唯一的问题是编辑GUI。用户需要在主GUI的JList中选择一个对象,然后单击edit按钮打开JDialog供用户编辑对象中的信息。这不起作用,一直说用户必须在JList中选择要编辑的对象,即使用户已选择或未选择对象

我已经提供了下面的所有代码:

主GUI:

public class DVDGUI extends javax.swing.JFrame {

    private DVDCollection dvdcollection = new DVDCollection();
    public DVDGUI() {
        initComponents();
        loadData();
    }

    private void loadData()
    {
        try
        {
            ObjectInputStream infile = new ObjectInputStream(new FileInputStream("heatherdunne.txt"));
            dvdcollection = (DVDCollection)infile.readObject();
            infile.close();
            updateDVDList();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(this,"Error found - " + e);
        }
    }

    public void saveData()
    {
        try
        {            
            ObjectOutputStream outfile = new ObjectOutputStream(new FileOutputStream("heatherdunne.txt"));
            outfile.writeObject(dvdcollection);
            outfile.close();
            System.exit(0);
        }
        catch (Exception e)
        {
               JOptionPane.showMessageDialog(this,"Error writing file - " + e);        
        }
    }

    @SuppressWarnings("unchecked")
    private void lstDVDsValueChanged(javax.swing.event.ListSelectionEvent evt) {                                     
        if (lstDVDs.getSelectedIndex() >= 0)
            txtDetails.setText(((DVD)lstDVDs.getSelectedValue()).getDetails());
        else
            txtDetails.setText(" ");                            

        int selectedIndex = lstDVDs.getSelectedIndex();
        ListModel lModel= lstDVDs.getModel();
        DVD item = (DVD)lModel.getElementAt(selectedIndex);
        System.out.println("Title= "+ item.getTitle());  
        boolean favourite = false;
        favourite = true;
        favourite = item.favourite;
        if (item.favourite == true)
        uebbtnFav.setSelected(true);
        else
        uebbtnFav.setSelected(false);
    }
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (lstDVDs.getSelectedIndex() >= 0)
        {
          JOptionPane.showMessageDialog(this,"You need to select a DVD to be edited");
        }
        else
        {
            DVD dvvvd = (DVD)lstDVDs.getSelectedValue();
            EditDVD idk = new EditDVD(this, dvvvd);  
        }
    }
    private void loadDVDsfromfile(){
        DVD dvd = null;
        DVD dvd2 = null;        
        DVD dvd3 = null;        
        try
        {    
            FileInputStream file = new FileInputStream("C:\\Users\\User\\Desktop\\code\\Assign2\\heatherdunne.txt"); 
            ObjectInputStream in = new ObjectInputStream(file); 

            dvd = (DVD)in.readObject(); 
            dvd2 = (DVD)in.readObject();             
            dvd3 = (DVD)in.readObject();             

            dvdcollection.addDVD(dvd);
            dvdcollection.addDVD(dvd2);
            dvdcollection.addDVD(dvd3);

            in.close(); 
            file.close(); 

            System.out.println("Object has been deserialized "); 
            System.out.println(dvd);
            System.out.println(dvd2);
            System.out.println(dvd3);      

        } 

        catch(IOException ex) 
        { 
            System.out.println("IOException is caught"); 
        } 

        catch(ClassNotFoundException ex) 
        { 
            System.out.println("ClassNotFoundException is caught"); 
        }                 
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        loadDVDsfromfile();  
        DefaultListModel lstmdl = new DefaultListModel();
        lstmdl.addElement(dvdcollection.getDVDs().get(0)); 
        lstDVDs.setModel(lstmdl);
    }                                 
    private void crtFileActionPerformed(java.awt.event.ActionEvent evt) {                                        
          DVD dvd = new DVD("Avengers: Endgame", 2019, Boolean.TRUE);
          DVD dvd2 = new DVD("Shrek 2", 2004, Boolean.TRUE);
          DVD dvd3 = new DVD("IT", 2017, Boolean.TRUE);

        try
        {    
            FileOutputStream file = new FileOutputStream("C:\\Users\\User\\Desktop\\code\\Assign2\\heatherdunne.txt"); 
            ObjectOutputStream out = new ObjectOutputStream(file); 

            out.writeObject(dvd); 
            out.writeObject(dvd2); 
            out.writeObject(dvd3);



            out.close(); 
            file.close(); 

            System.out.println("Object has been serialized"); 
        } 

        catch(IOException ex) 
        { 
            System.out.println("IOException is caught"); 
        }    

    }                                       
    public void updateDVDList()
    {       
        if (rbtTitle.isSelected())
            sortByTitle();
        else
                sortByID();
        lstDVDs.setListData(dvdcollection.getDVDs().toArray());
    }                                

编辑DVD GUI

private DVD selectedDVD;
    public EditDVD(DVDGUI inParent, DVD inDVD)
    {
        super(inParent, true);          
        initComponents();
        parent = inParent;
        Title.setText(selectedDVD.getTitle());        
        Year.setText(selectedDVD.getYear() + "");
        setVisible(true);        
    }

@SuppressWarnings("unchecked")
private void EditActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String title = Title.getText();
    String year = Year.getText();

    String msgTitle = DVD.checkTitle(title);
    String msgYear = DVD.checkYear(year);        

    if (msgTitle.length()>0)
        JOptionPane.showMessageDialog(this, msgTitle);
    else
    if (msgYear.length()>0)
        JOptionPane.showMessageDialog(this, msgYear);
    else    
    {
        //validation was ok
            selectedDVD.setTitle(title);
            selectedDVD.setYear(Integer.parseInt(year));
            parent.updateDVDList();
            JOptionPane.showMessageDialog(this, "Update sucessful");
            dispose();
    }
}    

DVD类:

public class DVD implements Serializable
{
    private String id;     
    private String title;                                                                     
    private int year;                                                          
    boolean favourite;  
    private int nextID=1;

    public DVD(String inID, String inTitle, int inYear, boolean inFavourite)
    {
        id = inID;
        title = inTitle;
        year = inYear;
        favourite = inFavourite;
    }
    public DVD (String inTitle, int inYear, boolean inFavourite)
    {  
        id = "0";
        nextID++; 
        title = inTitle;
        year = inYear;
        favourite = inFavourite;        
    }


    public String toString()
    {
        return title;
    } 

    public String getID()
    {
        return id;
    }

    public void setID(String inID)
    {
        id = inID;
    }

    public String getDetails()
    {
        return "id(" + id + ") " + title + " <" + year + "> ";
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String inTitle)
    {
        title = inTitle;
    }

    public String getYear()
    {
        if (year==0)
            return " ";
        else
            return year + "";
    } 

    public void setYear(int inYear)
    {
        year = inYear;
    }

    public boolean isFavourite()
    {
        return favourite;
    }    

    public void setFavourite(boolean inFavourite)
    {
        favourite = inFavourite;
    }  

    public static String checkTitle(String inTitle)
    {
        if (inTitle.trim().length()>0)
            return "";
        else
            return "Title must have at least 1 non-blank character";
    } 

    public static String checkYear(String inYear)
    {
        Calendar now = Calendar.getInstance();  
        int currYear = now.get(Calendar.YEAR);         
        try
        {
            int year = Integer.parseInt(inYear);
            if ((year >= 1997)&&(year <= currYear))
                return ""; 
            else
                return "Year must be between 1997 and current year";
        }
        catch (Exception ex)
        {
            return "Year must be a 4 digit integer";
        }
    }

    public static String checkID(String inID)
    {
        if (inID.trim().length()>0)
            return "";
        else
            return "ID must have at least 1 non-blank character";
    } 

    public static boolean checkFavourite(boolean inFav)
    {
        boolean Favs = false;
        int total = 0;
        if ( !Favs ) 
        {
        total = total + 1;
        }
        return false;
    }
    public int compareTo(DVD d2)
    {
//        if (ID<c2.getID())
//      return -1;
//  else 
//         if (ID>c2.getID())
//          return 1;
//       else return 0;        
        return id.compareTo(d2.getID());
    }        
}

DVD收藏类别:

public class DVDCollection implements Serializable
{
    private ArrayList<DVD> dvds = new ArrayList<DVD>();
    private int nextID=1;
    public boolean addDVD(DVD inDVD)
    {   
        return 
        dvds.add(inDVD);
    }

    public DVDCollection()
    {
    }

    public ArrayList<DVD> getDVDs()
    {
        return dvds;
    }

    public int getNumDVDs()
    {
        return dvds.size();
    }

    public boolean deleteDVD(DVD inDVD)
    {
       return dvds.remove(inDVD);
    }

    public int CountFavourites()
    {
        int total = 0;

        if (DVD.checkFavourite(true))
        {
            boolean fav = true;   
            int val = fav? 1 : 0;
            total = 1 + total;
        }
        else
        {
            boolean fav = false;   
            int val = fav? 1 : 0;
        }


        return total;
    }
}

我已经按照类中提供的编辑GUI的步骤进行了操作,但是这个问题仍然存在。我尝试过的另一件事是将所有变量转换为字符串,并删除了最受欢迎的jcheckbox的编辑函数,以防出现问题。我做了更多的加载,但都导致了失败,尽管如此,还是出现了相同的问题

非常感谢您的帮助

更新:问题已解决


共 (1) 个答案

  1. # 1 楼答案

    我已经解决了我的代码问题