Java Reference
In-Depth Information
sizeComboBox. setSelectedItem(12) ;
sizeComboBox . setEditable (
true
);
ActionListener changeListener =
new
ActionListener ()
{
public void
actionPerformed(ActionEvent e)
{
int
mask = Font .PLAIN;
if
(boldCheckBox . isSelected () )
{
mask += Font .BOLD;
if
(italicCheckBox . isSelected ())
{
mask += Font . ITALIC;
textArea . setFont(
new
Font((String) fontComboBox. getSelectedItem
() , mask, (Integer) sizeComboBox. getSelectedItem())) ;
}
;
controlPanel . add( fontComboBox) ;
controlPanel .add(sizeComboBox) ;
controlPanel . add(boldCheckBox) ;
controlPanel .add(italicCheckBox) ;
add( controlPanel , BorderLayout .NORTH) ;
boldCheckBox. addActionListener(changeListener) ;
italicCheckBox . addActionListener(changeListener) ;
sizeComboBox. addActionListener(changeListener) ;
fontComboBox. addActionListener(changeListener) ;
openMenuItem. addActionListener(
new
ActionListener ()
}
{
public void
actionPerformed(ActionEvent e)
{
JFileChooser fileChooser =
new
JFileChooser () ;
if
( f i leChooser . showDialog (NotepadFrame .
this
,
"Open"
)==
JFileChooser .APPROVEOPTION)
{
File newFile = fileChooser . getSelectedFile() ;
try
(FileInputStream fileIn =
new
FileInputStream(newFile))
{
ObjectInputStream in =
new
ObjectInputStream( fileIn ) ;
boldCheckBox. setSelected ( in . readBoolean() ) ;
italicCheckBox . setSelected(in . readBoolean()) ;
sizeComboBox. setSelectedItem(in . readObject()) ;
fontComboBox. setSelectedItem(in . readObject()) ;
textArea. setText((String)in. readObject());
}
{
catch
(Exception exception)
}
}
}
}
);
JMenuItem saveMenuItem =
new
JMenuItem(
"Save..."
);
fileMenu .add(saveMenuItem) ;
saveMenuItem. addActionListener(
new
ActionListener ()
{
public void
actionPerformed(ActionEvent e)
{
JFileChooser fileChooser =
new
JFileChooser () ;
if
( f i leChooser . showDialog (NotepadFrame .
this
,
"Save"
)==
JFileChooser .APPROVEOPTION)
{
File newFile = fileChooser . getSelectedFile() ;
try
(FileOutputStream fileOut =
new
FileOutputStream(newFile)
)
{
ObjectOutputStream out =
new
ObjectOutputStream( fileOut ) ;
out .writeBoolean(boldCheckBox. isSelected ()) ;
out .writeBoolean(italicCheckBox . isSelected ()) ;