import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Editor extends JFrame implements ActionListener
{
JTextArea textfeld = new JTextArea(300,200);
JScrollPane pane = new JScrollPane(textfeld);
JPanel panel = new JPanel();
JMenuBar menubar = new JMenuBar();
JMenu data;
JMenuItem open;
JMenuItem save;
JMenuItem saveAs;
JMenuItem print;
JMenuItem close;
JMenuItem neu;
JMenu edit;
JMenuItem copy;
JMenuItem paste;
JMenuItem cut;
JMenuItem mark;
JMenu view;
JMenuItem preview;
JMenuItem zoomIn;
JMenuItem zoomOut;
JMenu ensertion;
JMenuItem hyperlink;
JMenuItem grafic;
JMenu format;
JMenu font;
JMenuItem size;
JMenuItem art;
JMenu table;
JMenuItem insertTable;
JMenu insert;
JMenu window;
JMenuItem newWindow;
JFileChooser oeffnen = new JFileChooser();
JFileChooser speichern = new JFileChooser();
String[ ] schriftart = {"Arial", "Times New Roman", "Serif", "SansSerif", "Monospaced"};
JComboBox schrift = new JComboBox(schriftart);
String[ ] schriftgroeße = {"8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26"};
JComboBox fontsize = new JComboBox(schriftgroeße);
JButton fett;
JButton kursiv;
JButton unterstrichen;
JButton kopieren;
JButton einfuegen;
JButton ausschneiden;
JButton speicher;
JButton oeffne;
JButton drucke;
JButton newDocument;
JButton verweiß;
JButton tabelle;
JToolBar toolBar;
JToolBar toolBar2;
Object action;
File dateiName;
public Editor()
{
//Fenster
this.setSize(500, 500);
this.setTitle("Editor");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
Container cp = getContentPane();
this.panel.setLayout(null);
panel.add(pane);
panel.add(textfeld);
cp.add(panel);
//panel.add(this.textfeld);
this.textfeld.setTabSize(5);
this.textfeld.setLineWrap(true);
this.textfeld.setWrapStyleWord(true);
this.textfeld.setBounds(0, 60, 500, 440);
//Toolbar -und tipps
this.setToolBar();
this.setTooltips();
//Menü
this.menubar.add(createBarEntryData());
this.menubar.add(createBarEntryEdit());
this.menubar.add(createBarEntryView());
this.menubar.add(createBarEntryEnsertion());
this.menubar.add(createBarEntryFormat());
this.menubar.add(createBarEntryTable());
this.menubar.add(createBarEntryWindow());
setJMenuBar(this.menubar);
this.setVisible(true);
}
public static void main (String[ ] args)
{
new Editor();
}
//Menü
public JMenu createBarEntryData()
{
this.data = new JMenu("Datei");
this.neu = new JMenuItem("Neu", new ImageIcon("neuesDokument.jpg"));
this.open = new JMenuItem("Öffnen", new ImageIcon("open.png"));
this.save = new JMenuItem("Speichern", new ImageIcon("speichern.jpg"));
this.saveAs = new JMenuItem("Speichern unter...");
this.print = new JMenuItem("Drucken");
this.close = new JMenuItem("Beenden");
this.data.add(this.neu);
this.data.add(this.open);
this.data.addSeparator();
this.data.add(this.save);
this.data.add(this.saveAs);
this.data.addSeparator();
this.data.add(this.print);
this.data.addSeparator();
this.data.add(this.close);
this.neu.setAccelerator(KeyStroke.getKeyStroke('N', Event.ALT_MASK));
this.open.setAccelerator(KeyStroke.getKeyStroke('O', Event.ALT_MASK));
this.save.setAccelerator(KeyStroke.getKeyStroke('S', Event.ALT_MASK));
this.print.setAccelerator(KeyStroke.getKeyStroke('D', Event.ALT_MASK));
this.open.addActionListener(this);
this.save.addActionListener(this);
this.close.addActionListener(this);
this.neu.addActionListener(this);
this.saveAs.addActionListener(this);
this.print.addActionListener(this);
return this.data;
}
public JMenu createBarEntryEdit()
{
this.edit = new JMenu("Bearbeiten");
this.copy = new JMenuItem("Kopieren", new ImageIcon("kopieren.jpg"));
this.paste = new JMenuItem("Einfügen", new ImageIcon("einfuegen.jpg"));
this.cut = new JMenuItem("Ausschneiden", new ImageIcon("ausschneiden.jpg"));
this.mark = new JMenuItem("Alles markieren");
this.edit.add(this.copy);
this.edit.add(this.paste);
this.edit.add(this.cut);
this.edit.addSeparator();
this.edit.add(this.mark);
this.paste.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
this.copy.setAccelerator(KeyStroke.getKeyStroke('K', Event.CTRL_MASK));
this.cut.setAccelerator(KeyStroke.getKeyStroke('A', Event.CTRL_MASK));
this.mark.setAccelerator(KeyStroke.getKeyStroke('M', Event.CTRL_MASK));
this.copy.addActionListener(this);
this.paste.addActionListener(this);
this.cut.addActionListener(this);
this.mark.addActionListener(this);
return this.edit;
}
public JMenu createBarEntryView()
{
this.view = new JMenu("Ansicht");
this.preview = new JMenuItem("Vorschau", new ImageIcon("vorschau.jpg"));
this.zoomIn = new JMenuItem("Zoom +");
this.zoomOut = new JMenuItem("Zoom -");
this.view.add(this.preview);
this.view.addSeparator();
this.view.add(this.zoomIn);
this.view.add(this.zoomOut);
this.preview.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));
this.preview.addActionListener(this);
this.zoomIn.addActionListener(this);
this.zoomOut.addActionListener(this);
return this.view;
}
public JMenu createBarEntryEnsertion()
{
this.ensertion = new JMenu("Einfügen");
this.hyperlink = new JMenuItem("Hyperlink einfügen", new ImageIcon("hyperlink.jpg"));
this.grafic = new JMenuItem("Grafik einfügen", new ImageIcon("grafik.jpg"));
this.ensertion.add(this.grafic);
this.ensertion.addSeparator();
this.ensertion.add(this.hyperlink);
this.hyperlink.setAccelerator(KeyStroke.getKeyStroke('V', Event.SHIFT_MASK));
this.grafic.setAccelerator(KeyStroke.getKeyStroke('G', Event.SHIFT_MASK));
this.grafic.addActionListener(this);
return this.ensertion;
}
public JMenu createBarEntryFormat()
{
this.format = new JMenu("Format");
this.font = new JMenu("Schrift");
this.size = new JMenuItem("Schriftgröße", new ImageIcon("schriftart.jpg"));
this.art = new JMenuItem("Schriftart");
this.format.add(this.font);
this.font.add(this.size);
this.font.add(this.art);
this.font.addActionListener(this);
this.size.addActionListener(this);
return this.format;
}
public JMenu createBarEntryTable()
{
this.table = new JMenu("Tabelle");
this.insert = new JMenu("Einfügen");
this.insertTable = new JMenuItem("Tabelle", new ImageIcon("tabelle.jpg"));
this.insert.add(this.insertTable);
this.table.add(this.insert);
this.insertTable.setAccelerator(KeyStroke.getKeyStroke('T', Event.SHIFT_MASK));
this.insertTable.addActionListener(this);
return this.table;
}
public JMenu createBarEntryWindow()
{
this.window = new JMenu("Fenster");
this.newWindow= new JMenuItem("neues Fenster", new ImageIcon("fenster.jpg"));
this.window.add(this.newWindow);
this.newWindow.setAccelerator(KeyStroke.getKeyStroke('F', Event.ALT_MASK));
this.newWindow.addActionListener(this);
return this.window;
}
//ToolBar
public void setToolBar()
{
//erste Toolbar
this.toolBar = new JToolBar();
this.toolBar.add(this.newDocument = new JButton(new ImageIcon("neuesDokument.jpg")));
this.toolBar.add(this.oeffne = new JButton(new ImageIcon("open.png")));
this.toolBar.add(this.speicher = new JButton(new ImageIcon("speichern.jpg")));
this.toolBar.add(this.drucke = new JButton(new ImageIcon("drucken.jpg")));
this.toolBar.add(this.ausschneiden = new JButton(new ImageIcon("ausschneiden.jpg")));
this.toolBar.add(this.kopieren = new JButton(new ImageIcon("kopieren.jpg")));
this.toolBar.add(this.einfuegen = new JButton(new ImageIcon("einfuegen.jpg")));
this.toolBar.add(this.verweiß = new JButton(new ImageIcon("hyperlink.jpg")));
this.toolBar.add(this.tabelle = new JButton(new ImageIcon("tabelle.jpg")));
this.panel.add(this.toolBar);
this.toolBar.setBounds(0 , 0, 500, 25);
this.kopieren.addActionListener(this);
this.ausschneiden.addActionListener(this);
this.einfuegen.addActionListener(this);
this.oeffne.addActionListener(this);
this.speicher.addActionListener(this);
this.newDocument.addActionListener(this);
this.tabelle.addActionListener(this);
this.verweiß.addActionListener(this);
//Zweite Toolbar
this.toolBar2 = new JToolBar();
this.toolBar2.add(this.schrift);
this.toolBar2.add(this.fontsize);
this.toolBar2.add(this.fett = new JButton(new ImageIcon("bold.jpg")));
this.toolBar2.add(this.kursiv = new JButton(new ImageIcon("kursiv.jpg")));
this.toolBar2.add(this.unterstrichen = new JButton(new ImageIcon("unterstrichen.jpg")));
this.panel.add(this.toolBar2);
this.toolBar2.setBounds(175, 30, 300, 25);
this.fett.addActionListener(this);
this.kursiv.addActionListener(this);
this.unterstrichen.addActionListener(this);
this.schrift.addActionListener(this);
this.fontsize.addActionListener(this);
}
//Tooltips
public void setTooltips()
{
this.oeffne.setToolTipText("Öffnen");
this.speicher.setToolTipText("Speichern");
this.newDocument.setToolTipText("neues Dokument");
this.drucke.setToolTipText("Drucken");
this.ausschneiden.setToolTipText("Ausschneiden");
this.kopieren.setToolTipText("Kopieren");
this.einfuegen.setToolTipText("Einfügen");
this.verweiß.setToolTipText("Hyperlink");
this.tabelle.setToolTipText("Tabelle");
this.fett.setToolTipText("Fett");
this.kursiv.setToolTipText("Kursiv");
this.unterstrichen.setToolTipText("Unterstrichen");
}
// Action- Events
public void actionPerformed(ActionEvent e)
{
this.action = e.getSource();
if((action==open) || (action==oeffne))
{
int chooser=oeffnen.showOpenDialog(this);
byte zeichen;
char buchstabe;
String text="";
try
{
dateiName = this.oeffnen.getSelectedFile();
FileInputStream in = new FileInputStream(dateiName);
do
{
zeichen = (byte)in.read();
textfeld.setText(zeichen+" ");
text +=(char)zeichen;
}while(zeichen !=-1);
in.close();
textfeld.setText(text);
}
catch(Exception ex1)
{
}
}
if((action==save) || (action==speicher))
{
int chooser=speichern.showSaveDialog(this);
dateiName=this.speichern.getSelectedFile();
String text= textfeld.getText();
try
{
FileOutputStream out = new FileOutputStream(dateiName);
for(int i=0; i<=text.length(); i++)
{
out.write((byte)text.charAt(i));
}
out.close();
}
catch(Exception ex2)
{
}
}
if((action==neu) || (action==newDocument))
{
textfeld.setText("");
}
if((action==this.copy) || (action==this.kopieren))
{
this.textfeld.copy();
}
if((action==this.paste) || (action==this.einfuegen))
{
this.textfeld.paste();
}
if((action==this.cut) || (action==this.ausschneiden))
{
this.textfeld.cut();
}
if(action==this.mark)
{
this.textfeld.selectAll();
}
if(action==this.close)
{
this.setVisible(false);
System.exit(0);
}
if(action==this.print)
{
}
if(action==this.hyperlink)
{
}
if(action==newWindow)
{
new Editor();
}
if(action==this.size)
{
}
if(action==fett)
{
int index = schrift.getSelectedIndex();
String schrift = Integer.toString(index);
textfeld.setFont(new Font(schrift, Font.BOLD, textfeld.getFont().getSize()));
}
if(action==kursiv)
{
int index = schrift.getSelectedIndex();
String schrift = Integer.toString(index);
textfeld.setFont(new Font(schrift, Font.ITALIC, textfeld.getFont().getSize()));
}
if(action==unterstrichen)
{
int index = schrift.getSelectedIndex();
String schrift = Integer.toString(index);
textfeld.setFont(new Font(schrift, Font.PLAIN, textfeld.getFont().getSize()));
}
if(action==schrift)
{
int index = schrift.getSelectedIndex();
String schrift = Integer.toString(index);
textfeld.setFont(new Font(schrift, Font.PLAIN, textfeld.getFont().getSize()));
}
if(action==fontsize)
{
Object schritt1 = this.fontsize.getSelectedItem();
String schritt2 = String.valueOf(schritt1);
int groeße = Integer.parseInt(schritt2);
Object art = this.schrift.getSelectedItem();
String schrift = String.valueOf(art);
textfeld.setFont(new Font(schrift, Font.PLAIN, groeße+5));
}
if(action==zoomIn)
{
int index = schrift.getSelectedIndex();
String schrift = Integer.toString(index);
textfeld.setFont(new Font(schrift, Font.PLAIN, textfeld.getFont().getSize()+5));
}
if(action==zoomOut)
{
int index = schrift.getSelectedIndex();
String schrift = Integer.toString(index);
textfeld.setFont(new Font(schrift, Font.PLAIN, textfeld.getFont().getSize()-5));
}
}
}