— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet416/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   412   413   414   415   416   417   418   419   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

serialization 
and
file I/O
you are here
4
451
JMenuItem saveMenuItem = new JMenuItem(“Save”);
newMenuItem.addActionListener(new NewMenuListener());
saveMenuItem.addActionListener(new SaveMenuListener());
fileMenu.add(newMenuItem);
fileMenu.add(saveMenuItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(500,600);
frame.setVisible(true);
}
public 
class NextCardListener implements ActionListener
{
public void actionPerformed(ActionEvent ev) {
QuizCard card = new QuizCard(question.getText(), answer.getText());
cardList.add(card);
clearCard();
}
}
public 
class SaveMenuListener implements ActionListener
{
public void actionPerformed(ActionEvent ev) {
QuizCard card = new QuizCard(question.getText(), answer.getText());
cardList.add(card);
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(frame);
saveFile(fileSave.getSelectedFile());
}
}
public 
class NewMenuListener implements ActionListener
{
public void actionPerformed(ActionEvent ev) {
cardList.clear();
clearCard();
}
}
private void 
clearCard()
{
question.setText(“”);
answer.setText(“”);
question.requestFocus();
}
private void 
saveFile(File file)
{
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(QuizCard card:cardList) {
writer.write(card.getQuestion() + “/”);
writer.write(card.getAnswer() + “\n”);
}
writer.close();
} catch(IOException ex) {
System.out.println(“couldn’t write the cardList out”);
ex.printStackTrace();
}

}
Brings up a file dialog box and waits on this 
line until the user chooses ‘Save’ from the 
dialog box. All the file dialog navigation and 
selecting a file, etc. is done for you by the 
JFileChooser! It really is this easy.
The method that does the actual file writing 
(called by the SaveMenuListener’s event handler). 
The argument is the ‘File’ object the user is saving. 
We’ll look at the File class on the next page.
We chain a BufferedWriter on to a new 
FileWriter to make writing more efficient. 
(We’ll talk about that in a few pages).
Walk through the ArrayList of 
cards and write them out, one card 
per line, with the question and an-
swer separated by a “/”, and then 
add a newline character (“\n”)
We make a menu bar, make a File 
menu, then put ‘new’ and ‘save’ menu 
items into the File menu. We add 
the 
menu to the menu bar, then tell the 
frame to use this menu bar. Menu 
items can fire an ActionEvent



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   412   413   414   415   416   417   418   419   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish