Tugas 10 PBO C Text Editor
Nama : Dewangga Dharmawan
NRP : 05111940000029
Kelas : PBO C
Diagram Class
Source code (Classnya hanya 1)
/**
* Class Text Editor (semacam Notepad
*
* @author (Dewangga Dharmawan)
* @version (14/1/2021)
*/
//Library
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
public class TextEditor extends JFrame implements ActionListener
{
//Bentuk Window
JTextArea t;
JFrame f;
//Methodenya
public TextEditor()
{
//Title
f = new JFrame("TextEditor");
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
}
catch(Exception e)
{
}
//Ruang tulis
t = new JTextArea();
//Bar-bar yang diperlukan
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("File");
JMenuItem mi1 = new JMenuItem("File Baru"),
mi2 = new JMenuItem("Buka File"),
mi3 = new JMenuItem("Save File"),
mi9 = new JMenuItem("Print File");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi9.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi9);
JMenu m2 = new JMenu("Edit File");
JMenuItem mi4 = new JMenuItem("Ctrl + X"),
mi5 = new JMenuItem("Ctrl + C"),
mi6 = new JMenuItem("Ctrl + V");
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
JMenuItem mc = new JMenuItem("Tutup File");
mc.addActionListener(this);
mb.add(m1);
mb.add(m2);
mb.add(mc);
f.setJMenuBar(mb);
f.add(t);
f.setSize(500,500);
f.show();
}
//Input Mouse pada masing-masing bar
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Ctrl + X"))
t.cut();
else if(s.equals("Ctrl + C"))
t.copy();
else if(s.equals("Ctrl + V"))
t.paste();
else if(s.equals("Save File"))
{
JFileChooser j = new JFileChooser("f:");
int r = j.showSaveDialog(null);
if(r == JFileChooser.APPROVE_OPTION)
{
File fi = new File(j.getSelectedFile().getAbsolutePath());
try
{
FileWriter wr = new FileWriter(fi, false);
BufferedWriter w = new BufferedWriter(wr);
w.write(t.getText());
w.flush();
w.close();
}
catch(Exception evt)
{
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
else
JOptionPane.showMessageDialog(f, "Programnya dihentikan");
}
else if(s.equals("Print File"))
{
try
{
t.print();
}
catch(Exception evt)
{
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
else if(s.equals("Buka File"))
{
JFileChooser j = new JFileChooser("f:");
int r = j.showOpenDialog(null);
if(r == JFileChooser.APPROVE_OPTION)
{
File fi = new File(j.getSelectedFile().getAbsolutePath());
try
{
String s1 = "", sl = "";
FileReader fr = new FileReader(fi);
BufferedReader br = new BufferedReader(fr);
sl = br.readLine();
while((s1 = br.readLine()) != null)
sl = sl + "\n" + s1;
t.setText(sl);
}
catch(Exception evt)
{
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
else
JOptionPane.showMessageDialog(f, "Programnya telah dihentikan");
}
else if(s.equals("File Baru"))
t.setText("");
else if(s.equals("Tutup File"))
f.setVisible(false);
}
}
Implementasi
Cut, Copy, Paste tidak bisa di SS
Komentar
Posting Komentar