Minggu, 02 Juli 2017

CONTOH MEMBUAT NOTEPAD PADA JAVA

Posted by user on July 02, 2017 

Langsung saja ini contoh membuat notepad sederhana pada java :

Library


import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

Script

public class Notepad extends JFrame {

private JTextArea textArea;


public Notepad(){


try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

setSize(600,600);

JPanel panel= (JPanel) this.getContentPane();
GridLayout gridLayout = new GridLayout(1,0);
panel.setLayout(gridLayout);
// JTextField field = new JTextField();// JTextArea textArea; // JTextArea textArea = new JTextArea(); textArea = new JTextArea();
// panel.add(field); panel.add(textArea);
this.setTitle("Notepad");

JMenuBar menuBar = new JMenuBar();

JMenu file = new JMenu("File"),
view = new JMenu("View");
JMenuItem open= new JMenuItem("Open..."),
save= new JMenuItem("Save"),
saveAs= new JMenuItem("SaveAs");
JMenu font = new JMenu("Font");
JMenuItem size14 = new JMenuItem("14");
JMenuItem size16 = new JMenuItem("16");
JMenuItem size18 = new JMenuItem("18");
this.setLocationRelativeTo(null);

size14.setActionCommand("14");

size16.setActionCommand("16");
size18.setActionCommand("18");

//TODO Tambahkan menu baru juga untuk custom size JMenuItem customSize = new JMenuItem("Custom Size....");

customSize.setActionCommand("-");

ActionListener sizeListener = new ActionListener() {

@Override public void actionPerformed(ActionEvent actionEvent){
Integer size;
if(actionEvent.getActionCommand().equals("-")){
size = (Integer) JOptionPane.showInputDialog(Notepad.this, "Ukuran font : ", "Custom Size",
JOptionPane.INFORMATION_MESSAGE, null, new Integer[]{14,16,18,20,24,36,48}, "ukuran");
if(size == null)
return;
}else size = Integer.valueOf(actionEvent.getActionCommand());

Notepad.this.changeTextSize(size);

}
};

size14.addActionListener(sizeListener);

size16.addActionListener(sizeListener);
size18.addActionListener(sizeListener);
customSize.addActionListener(sizeListener);

file.add(open);

file.add(save);
file.add(saveAs);

JMenuItem test = new JMenuItem("Test");

test.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Notepad.this, "cinta", "Hai sayang!", JOptionPane.WARNING_MESSAGE);
String jawaban = JOptionPane.showInputDialog(Notepad.this,"Apa jawaban anda!","Hai Sayang",
JOptionPane.WARNING_MESSAGE);
System.out.println(jawaban);
}
});

view.add(font);

view.add(test);
font.add(size14);
font.add(size16);
font.add(size18);
font.add(customSize);
menuBar.add(file);
menuBar.add(view);
this.setJMenuBar(menuBar);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.pack();

// this.setMinimumSize(this.getPreferredSize());
 this.setVisible(true);
}

private void changeTextSize(Integer size) {

System.out.println(size);
Font originalFont = textArea.getFont();
Font newFont = new Font(originalFont.getFontName(), originalFont.getStyle(),size);

//Set font ke textarea textArea.setFont(newFont);

}


public static void main(String[] args){


Notepad notepad=new Notepad();

}
}


  ingin file lengkap download disini

Tidak ada komentar:

Posting Komentar