Final Project Membuat Aplikasi Notepad
Membuat Aplikasi Notepad
Pada Final Project ini, saya membuat sebuah program sejenis notepad.
Berikut sourcecode program:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import java.io.*; | |
public class notipedi extends JFrame { | |
// Komponen | |
private JTextArea _editArea; | |
private JFileChooser _fileChooser = new JFileChooser(); | |
// Membuat action pada menu | |
private Action _openAction = new OpenAction(); | |
private Action _saveAction = new SaveAction(); | |
private Action _exitAction = new ExitAction(); | |
public static void main(String[] args) { | |
new notipedi(); | |
} | |
public notipedi() { | |
// Membuat area text yang dapat discroll | |
_editArea = new JTextArea(15, 80); | |
_editArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2)); | |
_editArea.setFont(new Font("monospaced", Font.PLAIN, 14)); | |
JScrollPane scrollingText = new JScrollPane(_editArea); | |
// Membuat panel | |
JPanel content = new JPanel(); | |
content.setLayout(new BorderLayout()); | |
content.add(scrollingText, BorderLayout.CENTER); | |
// Membuat menu | |
JMenuBar menuBar = new JMenuBar(); | |
JMenu fileMenu = menuBar.add(new JMenu("File")); | |
fileMenu.setMnemonic('F'); | |
fileMenu.add(_openAction); | |
fileMenu.add(_saveAction); | |
fileMenu.addSeparator(); | |
fileMenu.add(_exitAction); | |
// Set menu content dan menu. | |
setContentPane(content); | |
setJMenuBar(menuBar); | |
// Karakeristik window pada umumnya | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setTitle("NotiPedi"); | |
pack(); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} | |
class OpenAction extends AbstractAction { | |
public OpenAction() { | |
super("Open..."); | |
putValue(MNEMONIC_KEY, new Integer('O')); | |
} | |
public void actionPerformed(ActionEvent e) { | |
int retval = _fileChooser.showOpenDialog(notipedi.this); | |
if (retval == JFileChooser.APPROVE_OPTION) { | |
File f = _fileChooser.getSelectedFile(); | |
try { | |
FileReader reader = new FileReader(f); | |
_editArea.read(reader, ""); | |
} catch (IOException ioex) { | |
System.out.println(e); | |
System.exit(1); | |
} | |
} | |
} | |
} | |
class SaveAction extends AbstractAction { | |
SaveAction() { | |
super("Save..."); | |
putValue(MNEMONIC_KEY, new Integer('S')); | |
} | |
public void actionPerformed(ActionEvent e) { | |
int retval = _fileChooser.showSaveDialog(notipedi.this); | |
if (retval == JFileChooser.APPROVE_OPTION) { | |
File f = _fileChooser.getSelectedFile(); | |
try { | |
FileWriter writer = new FileWriter(f); | |
_editArea.write(writer); | |
} catch (IOException ioex) { | |
JOptionPane.showMessageDialog(notipedi.this, ioex); | |
System.exit(1); | |
} | |
} | |
} | |
} | |
class ExitAction extends AbstractAction { | |
public ExitAction() { | |
super("Exit"); | |
putValue(MNEMONIC_KEY, new Integer('X')); | |
} | |
public void actionPerformed(ActionEvent e) { | |
System.exit(0); | |
} | |
} | |
} |
Demo:
Link download jar:
Komentar
Posting Komentar