好的,这是我建造的应用程序的原型。我已经添加了问题的代码和图像我希望正确的数据显示在正确的列中,因此我在图像中标记为1的应该位于标题下的第二行,我在图像中标记为2应该是在工作室等的第二排。谁能看到我哪里错了?这个应用程序还有其他一些问题,比如JMenuBar,但后来在一个新问题中的地址不好,谢谢!
//Date : 11-05-2014
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class DVD extends JFrame implements ActionListener
{
//construct components
JLabel sortPromt= new JLabel("Sort by:");
JComboBox fieldCombo= new JComboBox();
JTextPane textPane = new JTextPane();
//initalize data in arrays
String title[]={"Casablanca", "Citizen Kane", "Singin in the rain", "The wizzard of Oz"};
String studio[]={"Wanner Brothers", "RKO Pictures", "MGM", "MGM"};
String year[]={"1942", "1941", "1952", "1939"};
//construct an instance of DVD
public DVD()
{
super("Classic on DVD");
}
//create the menu system
public JMenuBar createMenuBar()
{
///create instance of menu bar
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 JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_E);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuBar.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
//construct and pop 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(1);
mnuBar.add(mnuEditInsert);
mnuEditInsert.setActionCommand("Insert");
mnuEditInsert.addActionListener(this);
JMenu mnuEditSearch = new JMenu("Search",true);
mnuEditSearch.setMnemonic(KeyEvent.VK_R);
mnuEditSearch.setDisplayedMnemonicIndex(3);
mnuEditSearch.add(mnuEditSearch);
JMenuItem mnuEditSearchByTitle =new JMenuItem("By Title");
mnuEditSearchByTitle.setMnemonic(KeyEvent.VK_T);
mnuEditSearchByTitle.setDisplayedMnemonicIndex(3);
mnuBar.add(mnuEditSearchByTitle);
mnuEditSearchByTitle.setActionCommand("title");
mnuEditSearchByTitle.addActionListener(this);
JMenuItem mnuEditSearchByStudio =new JMenuItem("By Studio");
mnuEditSearchByStudio.setMnemonic(KeyEvent.VK_S);
mnuEditSearchByStudio.setDisplayedMnemonicIndex(3);
mnuBar.add(mnuEditSearchByStudio);
mnuEditSearchByStudio.setActionCommand("title");
mnuEditSearchByStudio.addActionListener(this);
JMenuItem mnuEditSearchByYear =new JMenuItem("By Year");
mnuEditSearchByYear.setMnemonic(KeyEvent.VK_Y);
mnuEditSearchByYear.setDisplayedMnemonicIndex(3);
mnuBar.add(mnuEditSearchByYear);
mnuEditSearchByYear.setActionCommand("title");
mnuEditSearchByYear.addActionListener(this);
return mnuBar;
}//End menu contructor
//Create the conetnt 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 arow to display sort fields");
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(sortPromt);
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;
}//end create container method
//method to create thtab stops and set fontstyles
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 Style
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("blod",regular);
StyleConstants.setBold(s,true);
s = textPane .addStyle("lard",regular);
StyleConstants.setFontSize(s,16);
}
//method to ad text to textpane
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
//clear 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"));
}//end loop
} //end try
catch (BadLocationException ble)
{
System.err.println("Couldnlt Insert Text");
}//end catch
return textPane;
}///end addtexttotextpane method
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;
}//end swictch
} //end if
//user clicks exit on file menu
if (arg=="Exit")
System.exit(0);
//user clicks insert new dvd on edit menu
if (arg=="insert")
{
String newTitle = JOptionPane.showInputDialog(null, "Please enter the moive'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 to arrys
title[title.length-1] = newTitle;
studio[studio.length-1] = newStudio;
year[year.length-1] = newYear;
//call to sort method
sort(title);
fieldCombo.setSelectedIndex(0);
}//end if
//user clicks title on search submeu
if(arg=="title")
search(arg,title);
//user clicks title on studio submeu
if(arg=="studio")
search(arg,studio);
//user clicks title on year submeu
if(arg=="year")
search(arg,year);
}//end of actionPerfomed method
//Method to enlarge an arry 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;
}//end enlarg arry method
//method to sort arrays
public void sort(String tempArray[])
{
//loop to 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(studio,element, element+1);
}//end if
}//end of loop
addTextToTextPane();
}//end of sort method
//method to swap two elements of an array
public void swap(String swapArray[], int first, int second)
{
String hold;//temp area to hold for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
} //end swap method
public void search (String searchField, String searchArray[])
{
try
{
Document doc = textPane.getDocument();//assing text to document object
doc.remove(0,doc.getLength());//clear previous text
//display colomTitles
doc.insertString(0,"TITLE\tSTUDIO\tYEAR\n",textPane.getStyle("large"));
//Prompt users 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"));
}//end if
}//end for
if (found = false)
{
JOptionPane.showMessageDialog(null,"Your search produced no results.", "no results found", JOptionPane.INFORMATION_MESSAGE);
sort(title);
}//end if
}//end try
catch(BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}//end catch
}//End serach method
//main method executes at runtime
public static void main(String args[])
{
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);
}//end main method
}//end dvd class
答案 0 :(得分:1)
在方法addTextToTextPane()中, 评论后//插入细节, 在doc.insertString(doc.getLength(),year [j] +&#34; \ t&#34;,textPane.getStyle(&#34; regular&#34;)); 替换&#34; \ t&#34;用&#34; \ n&#34;,移到下一行。