2013-10-27 139 views
0

下午好。我创建了一个介绍性java课程的应用程序,允许用户按照标题,工作室或年份对他们的DVD收藏进行分类。该应用程序还允许用户添加额外的DVD到他们的收藏,从而扩大他们的阵列。代码正确地编译,但是标题,工作室和年代正在混淆,显然有些问题。清楚的是,这些代码来自教科书,是实验的一部分,需要使用代码中看到的确切方法,软件包等。我并没有要求如何提高代码的效率(尽管这是我的学习所欢迎的),但具体而言,导致“争夺”的代码有什么问题。这里是我有的代码:阵列排序不正确

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class DVD extends JFrame implements ActionListener 
{ 
    // construct components 
    JLabel sortPrompt = new JLabel("Sort by:"); 
    JComboBox fieldCombo = new JComboBox(); 
    JTextPane textPane = new JTextPane(); 

    // initialize data in arrays 
    String title[] = {"Casablanca", "Citizen Kane", "Singin' in the Rain", "The Wizard of Oz"}; 
    String studio[] = {"Warner Brothers", "RKO Pictures", "MGM", "MGM"}; 
    String year[] = {"1942", "1941", "1952", "1939"}; 

    // construct instance of DVD 
    public DVD() 
    { 
     super("Classics on DVD"); 
    } 

    // create the menu system 
    public JMenuBar createMenuBar() 
    { 
     // create an instance of the menu 
     JMenuBar mnuBar = new JMenuBar(); 
     setJMenuBar(mnuBar); 

     // construct and populate the File menu 
     JMenu mnuFile = new JMenu("File", true); 
      mnuFile.setMnemonic(KeyEvent.VK_F); 
      mnuFile.setDisplayedMnemonicIndex(0); 
      mnuBar.add(mnuFile); 

     JMenuItem mnuFileExit = new JMenu("Exit"); 
      mnuFileExit.setMnemonic(KeyEvent.VK_X); 
      mnuFileExit.setDisplayedMnemonicIndex(1); 
      mnuFile.add(mnuFileExit); 
      mnuFileExit.setActionCommand("Exit"); 
      mnuFileExit.addActionListener(this); 

     // contruct and populate the Edit menu 
     JMenu mnuEdit = new JMenu("Edit", true); 
      mnuEdit.setMnemonic(KeyEvent.VK_E); 
      mnuEdit.setDisplayedMnemonicIndex(0); 
      mnuBar.add(mnuEdit); 

     JMenuItem mnuEditInsert = new JMenuItem("Insert New DVD"); 
      mnuEditInsert.setMnemonic(KeyEvent.VK_I); 
      mnuEditInsert.setDisplayedMnemonicIndex(0); 
      mnuEdit.add(mnuEditInsert); 
      mnuEditInsert.setActionCommand("Insert"); 
      mnuEditInsert.addActionListener(this); 

     JMenu mnuEditSearch = new JMenu("Search"); 
      mnuEditSearch.setMnemonic(KeyEvent.VK_R); 
      mnuEditSearch.setDisplayedMnemonicIndex(3); 
      mnuEdit.add(mnuEditSearch); 

     JMenuItem mnuEditSearchByTitle = new JMenuItem("by Title"); 
      mnuEditSearchByTitle.setMnemonic(KeyEvent.VK_T); 
      mnuEditSearchByTitle.setDisplayedMnemonicIndex(3); 
      mnuEditSearch.add(mnuEditSearchByTitle); 
      mnuEditSearchByTitle.setActionCommand("title"); 
      mnuEditSearchByTitle.addActionListener(this); 

     JMenuItem mnuEditSearchByStudio = new JMenuItem("by Studio"); 
      mnuEditSearchByStudio.setMnemonic(KeyEvent.VK_S); 
      mnuEditSearchByStudio.setDisplayedMnemonicIndex(3); 
      mnuEditSearch.add(mnuEditSearchByStudio); 
      mnuEditSearchByStudio.setActionCommand("studio"); 
      mnuEditSearchByStudio.addActionListener(this); 

     JMenuItem mnuEditSearchByYear = new JMenuItem("by Year"); 
      mnuEditSearchByYear.setMnemonic(KeyEvent.VK_Y); 
      mnuEditSearchByYear.setDisplayedMnemonicIndex(3); 
      mnuEditSearch.add(mnuEditSearchByYear); 
      mnuEditSearchByYear.setActionCommand("year"); 
      mnuEditSearchByYear.addActionListener(this); 

     return mnuBar; 
    } 

    // create the content pane 
    public Container createContentPane() 
    { 
     // populate the JComboBox 
     fieldCombo.addItem("Title"); 
     fieldCombo.addItem("Studio"); 
     fieldCombo.addItem("Year"); 
     fieldCombo.addActionListener(this); 
     fieldCombo.setToolTipText("Click the drop-down arrow to display sort fields."); 

     // construct and populate the north panel 
     JPanel northPanel = new JPanel(); 
      northPanel.setLayout(new FlowLayout()); 
      northPanel.add(sortPrompt); 
      northPanel.add(fieldCombo); 

     // create the JTextPane and center panel 
     JPanel centerPanel = new JPanel(); 
      setTabsAndStyles(textPane); 
      textPane = addTextToTextPane(); 
      JScrollPane scrollPane = new JScrollPane(textPane); 
       scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
       scrollPane.setPreferredSize(new Dimension(500, 200)); 
      centerPanel.add(scrollPane); 

     // create Container and set attributes 
     Container c = getContentPane(); 
      c.setLayout(new BorderLayout(10,10)); 
      c.add(northPanel,BorderLayout.NORTH); 
      c.add(centerPanel,BorderLayout.CENTER); 

     return c; 
    } 

    // method to create tab stops and set font styles 
    protected void setTabsAndStyles(JTextPane textPane) 
    { 
     // create Tab Stops 
     TabStop[] tabs = new TabStop[2]; 
      tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); 
      tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); 
     TabSet tabset = new TabSet(tabs); 

     // set Tab Style 
     StyleContext tabStyle = StyleContext.getDefaultStyleContext(); 
     AttributeSet aset = 
      tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset); 
     textPane.setParagraphAttributes(aset, false); 

     // set Font styles 
     Style fontStyle = 
      StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 

     Style regular = textPane.addStyle("regular", fontStyle); 
     StyleConstants.setFontFamily(fontStyle, "SansSerif"); 

     Style s = textPane.addStyle("italic", regular); 
     StyleConstants.setItalic(s, true); 

     s = textPane.addStyle("bold", regular); 
     StyleConstants.setBold(s, true); 

     s = textPane.addStyle("large", regular); 
     StyleConstants.setFontSize(s, 16); 
    } 

    // method to add new text to the JTextPane 
    public JTextPane addTextToTextPane() 
    { 
     Document doc = textPane.getDocument(); 
     try 
     { 
      // clear the previous text 
      doc.remove(0, doc.getLength()); 

      // insert title 
      doc.insertString(0,"TITLE\tSTUDIO\tYEAR\n",textPane.getStyle("large")); 

      // insert detail 
      for (int j = 0; j<title.length; j++) 
      { 
       doc.insertString(doc.getLength(), title[j] + "\t", textPane.getStyle("bold")); 
       doc.insertString(doc.getLength(), studio[j] + "\t", textPane.getStyle("italic")); 
       doc.insertString(doc.getLength(), year[j] + "\t", textPane.getStyle("regular")); 
      } 
     } 
     catch(BadLocationException ble) 
     { 
      System.err.println("Couldn't insert text."); 
     } 

     return textPane; 
    } 

    // event to process user clicks 
    public void actionPerformed(ActionEvent e) 
    { 
     String arg = e.getActionCommand(); 

     // user clicks the sort by combo box 
     if (e.getSource() == fieldCombo) 
     { 
      switch(fieldCombo.getSelectedIndex()) 
      { 
       case 0: 
        sort(title); 
        break; 
       case 1: 
        sort(studio); 
        break; 
       case 2: 
        sort(year); 
        break; 
      } 
     } 

     // user clicks Exit on the File menu 
     if (arg == "Exit") 
      System.exit(0); 

     // user clicks Insert New DVD on the Edit Menu 
     if (arg == "Insert") 
     { 
      // accept new data 
      String newTitle = JOptionPane.showInputDialog(null, "Please enter the new movie's title"); 
      String newStudio = JOptionPane.showInputDialog(null, "Please enter the studio for " + newTitle); 
      String newYear = JOptionPane.showInputDialog(null, "Please enter the year for " + newTitle); 

      // enlarge arrays 
      title = enlargeArray(title); 
      studio = enlargeArray(studio); 
      year = enlargeArray(year); 

      // add new data to arrays 
      title[title.length-1] = newTitle; 
      studio[studio.length-1] = newStudio; 
      year[year.length-1] = newYear; 

      // call sort method 
      sort(title); 
      fieldCombo.setSelectedIndex(0); 
     } 

     // user clicks Title on the Search submenu 
     if (arg == "title") 
      search(arg, title); 

     // user clicks Studio on the Search submenu 
     if (arg == "studio") 
      search(arg, studio); 

     // user clicks Year on the Search submenu 
     if (arg == "year") 
      search(arg, year); 
    } 

    // method to enlarge an array by 1 
    public String[] enlargeArray(String[] currentArray) 
    { 
     String[] newArray = new String[currentArray.length + 1]; 
     for (int i = 0; i<currentArray.length; i++) 
      newArray[i] = currentArray[i]; 
     return newArray; 
    } 

    // method to sort arrays 
    public void sort(String tempArray[]) 
    { 
     // loop ton control number of passes 
     for (int pass = 1; pass < tempArray.length; pass++) 
     { 
      for (int element = 0; element < tempArray.length - 1; element++) 
       if (tempArray[element].compareTo(tempArray[element + 1])>0) 
       { 
        swap(title, element, element + 1); 
        swap(studio, element, element + 1); 
        swap(year, element, element + 1); 
       } 
     } 
     addTextToTextPane(); 
    } 

    // method to swap two elements of an array 
    public void swap(String swapArray[], int first, int second) 
    { 
     String hold; // temporary holding area for swap 
     hold = swapArray[first]; 
     swapArray[first] = swapArray[second]; 
     swapArray[second] = hold; 
    } 

    public void search(String searchField, String searchArray[]) 
    { 
     try 
     { 
      Document doc = textPane.getDocument(); // assign text to document object 
      doc.remove(0,doc.getLength()); // clear previous text 

      // display column titles 
      doc.insertString(0,"TITLE\tSTUDIO\tYEAR\n",textPane.getStyle("large")); 

      // prompt user for search data 
      String search = JOptionPane.showInputDialog(null, "Please enter the "+searchField); 
      boolean found = false; 

      // search arrays 
      for (int i = 0; i<title.length; i++) 
      { 
       if (search.compareTo(searchArray[i])==0) 
       { 
        doc.insertString(doc.getLength(), title[i] + "\t", textPane.getStyle("bold")); 
        doc.insertString(doc.getLength(), studio[i] + "\t", textPane.getStyle("italic")); 
        doc.insertString(doc.getLength(), year[i] + "\t", textPane.getStyle("regular")); 
        found = true; 
       } 
      } 
      if (found == false) 
      { 
       JOptionPane.showMessageDialog(null, "Your search produced no results.","No results found",JOptionPane.INFORMATION_MESSAGE); 
       sort(title); 
      } 
     } 
     catch(BadLocationException ble) 
     { 
      System.err.println("Couldn't insert text."); 
     } 
    } 

    // main method executes at run time 
    public static void main(String arg[]) 
    { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     DVD f = new DVD(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setJMenuBar(f.createMenuBar()); 
     f.setContentPane(f.createContentPane()); 
     f.setSize(600,375); 
     f.setVisible(true); 
    } 
} 

这里是一个截图,让你知道我的意思是“炒”。 Scrambled Application

很明显,所有的标题应该在标题栏,工作室和工作室中,并且年复一年。

一如既往,我欣赏指导。

回答

2

问题是此行

doc.insertString(doc.getLength(), year[j] + "\t", textPane.getStyle("regular")); 

,你把一个标签("\t")后年,而是你想有一个换行符("\n")。所以行变成

doc.insertString(doc.getLength(), year[j] + "\n", textPane.getStyle("regular")); 
+0

谢谢。因为我刚开始学习java,我仍然犯了很多简单的错误。迄今为止,所有提供的帮助都对我的发展起到了重要作用。 – Jerry

1

你是否简单地错过一个新行后,添加一个DVD的文件?即

doc.insertString(doc.getLength(), year[j] + "\n", textPane.getStyle("regular"));