import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyEditor extends JFrame
{
public MyEditor()
{
super("Editor");
setLocation(300, 300);
setSize(500, 500);
addWindowListener( new MyWindowAdapter() );
setLocationRelativeTo(null);
JPanel panel = new JPanel();
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Datei");
JMenu helpMenu = new JMenu("Hilfe");
getContentPane().add(panel);
menuBar.add(fileMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
fileMenu.setMnemonic(KeyEvent.VK_D); // Mnemonic ALT + Taste
helpMenu.setMnemonic(KeyEvent.VK_H);
final JFileChooser fc = new JFileChooser();
final JTextArea textArea = new JTextArea();
add( new JScrollPane(textArea) );
//################# Filemenu => Untermenus ###############################################################################################
fileMenu.add(new AbstractAction()
{
{
putValue( Action.NAME, "Datei öffnen..." );
putValue( Action.ACCELERATOR_KEY , KeyStroke.getKeyStroke("ctrl O") );
putValue( Action.SMALL_ICON, new ImageIcon(MyEditor.class.getResource("/toolbarButtonGraphics/general/Open16.gif")) );
}
public void actionPerformed( ActionEvent e )
{
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images & Text Files", "jpg", "gif", "txt"); //zeigt nur die angegebenen Dateien an
fc.setFileFilter(filter);
//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = fc.showOpenDialog( null );
//if(returnVal == JFileChooser.APPROVE_OPTION) { //bei Drücken von "Öffnen" if Bedingung ausführen
// System.out.println("You chose to open this file: " +
// fc.getSelectedFile().getName());
//}
if ( fc.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION )
{
FileReader in = null;
try {
textArea.read( in=new FileReader(fc.getSelectedFile()), null );
}
catch ( IOException ex ) {
textArea.setText( ex.getMessage() );
}
finally {
try { in.close(); } catch ( Exception ex ) { }
}
}
}
});
fileMenu.addSeparator();
fileMenu.add(new AbstractAction()
{
{
putValue( Action.NAME, "Speichern als..." );
putValue( Action.ACCELERATOR_KEY , KeyStroke.getKeyStroke("ctrl S") );
putValue( Action.SMALL_ICON, new ImageIcon(MyEditor.class.getResource("/toolbarButtonGraphics/general/SaveAs16.gif")) );
}
public void actionPerformed( ActionEvent e )
{
int returnVal = fc.showSaveDialog(null);
}
});
fileMenu.addSeparator();
fileMenu.add(new AbstractAction()
{
{
putValue( Action.NAME, "Drucken als..." );
putValue( Action.ACCELERATOR_KEY , KeyStroke.getKeyStroke("ctrl P") );
putValue( Action.SMALL_ICON, new ImageIcon(MyEditor.class.getResource("/toolbarButtonGraphics/general/Print16.gif")) );
}
public void actionPerformed( ActionEvent e )
{
System.out.println("Drucken als...");
}
});
fileMenu.addSeparator();
fileMenu.add(new AbstractAction("Beenden")
{
public void actionPerformed(ActionEvent e){System.exit(0);}
});
//###################### Filemenu => Untemenues (Ende) #######################################################################################
//##################### Helpmenu => Untermenues ##############################################################################################
helpMenu.add(new AbstractAction()
{
{
putValue( Action.NAME, "Editor-Hilfe" );
putValue( Action.ACCELERATOR_KEY , KeyStroke.getKeyStroke("F1") );
putValue( Action.SMALL_ICON, new ImageIcon(MyEditor.class.getResource("/toolbarButtonGraphics/general/Help16.gif")) );
}
public void actionPerformed( ActionEvent e )
{
System.out.println("Hilfe");
}
});
//##################### Helpmenu => Untermenues (Ende) ########################################################################################
setVisible(true);
}
}