Saat di lab kami diberi tugas oleh asisten lab membuat notepad dalam bahasa java,dimana seperti notepad asli yang memiliki title bar,menu bar dll..
Dan begini codenya:
package pertemuan4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.SocketTimeoutException;
/**
* Created by Acer on 08/05/2017.
*/
public class Notepad extends JFrame {
private JTextArea textArea;
public Notepad() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) { // menentukan tampilan dari sistem windowsnya
e.printStackTrace();
}
catch(InstantiationException e){
e.printStackTrace();
} catch(IllegalAccessException e){
e.printStackTrace();
} catch(UnsupportedLookAndFeelException e){
e.printStackTrace();
}
JPanel panel = (JPanel) this.getContentPane(); // cara mengambil content Jframe
//GridLayout gridLayout = new GridLayout(1, 0); // layout manager
//panel.setLayout(gridLayout);
JTextField field = new JTextField(); // Membuat text field dan area field=satu baris,area banyak baris
textArea = new JTextArea(); // Layout untuk mengatur sehingga teks bisa ditulis
panel.add(field);
panel.add(textArea);
this.setTitle("Notepad");
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File"),
view = new JMenu("View"),
edit = new JMenu("Edit");
JMenuItem open = new JMenuItem("Open"),
save = new JMenuItem("Save"),
saveAs = new JMenuItem("Save As");
JMenu font = new JMenu("Font");
JMenuItem size14 = new JMenuItem("14");
JMenuItem size16 = new JMenuItem("16");
JMenuItem size18 = new JMenuItem("18");
JMenuItem customSize = new JMenuItem("Custom Size");
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[] {
10,12,14,16,18,20,24,36,48},"Ukuran");
if(size == null)
return;
}else
size = Integer.valueOf(actionEvent.getActionCommand());
Notepad.this.changeTextSize(size);
}
};
size14.setActionCommand("14");
size16.setActionCommand("16");
size18.setActionCommand("18");
customSize.setActionCommand("-");
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");
view.add(test);
view.add(font);
font.add(size14);
font.add(size16);
font.add(size18);
font.add(customSize);
menuBar.add(file);
menuBar.add(view);
menuBar.add(edit);
this.setJMenuBar(menuBar);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//mENGESET uKURAN mINIIMUM JFrame
this.pack(); //komponen nya jadi 1
this.setMinimumSize(this.getPreferredSize()); //mengupdate ukuran yang pas untuk window itu
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();
}
}
Hasil Running nya:
Dan begini codenya:
package pertemuan4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.SocketTimeoutException;
/**
* Created by Acer on 08/05/2017.
*/
public class Notepad extends JFrame {
private JTextArea textArea;
public Notepad() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) { // menentukan tampilan dari sistem windowsnya
e.printStackTrace();
}
catch(InstantiationException e){
e.printStackTrace();
} catch(IllegalAccessException e){
e.printStackTrace();
} catch(UnsupportedLookAndFeelException e){
e.printStackTrace();
}
JPanel panel = (JPanel) this.getContentPane(); // cara mengambil content Jframe
//GridLayout gridLayout = new GridLayout(1, 0); // layout manager
//panel.setLayout(gridLayout);
JTextField field = new JTextField(); // Membuat text field dan area field=satu baris,area banyak baris
textArea = new JTextArea(); // Layout untuk mengatur sehingga teks bisa ditulis
panel.add(field);
panel.add(textArea);
this.setTitle("Notepad");
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File"),
view = new JMenu("View"),
edit = new JMenu("Edit");
JMenuItem open = new JMenuItem("Open"),
save = new JMenuItem("Save"),
saveAs = new JMenuItem("Save As");
JMenu font = new JMenu("Font");
JMenuItem size14 = new JMenuItem("14");
JMenuItem size16 = new JMenuItem("16");
JMenuItem size18 = new JMenuItem("18");
JMenuItem customSize = new JMenuItem("Custom Size");
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[] {
10,12,14,16,18,20,24,36,48},"Ukuran");
if(size == null)
return;
}else
size = Integer.valueOf(actionEvent.getActionCommand());
Notepad.this.changeTextSize(size);
}
};
size14.setActionCommand("14");
size16.setActionCommand("16");
size18.setActionCommand("18");
customSize.setActionCommand("-");
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");
view.add(test);
view.add(font);
font.add(size14);
font.add(size16);
font.add(size18);
font.add(customSize);
menuBar.add(file);
menuBar.add(view);
menuBar.add(edit);
this.setJMenuBar(menuBar);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//mENGESET uKURAN mINIIMUM JFrame
this.pack(); //komponen nya jadi 1
this.setMinimumSize(this.getPreferredSize()); //mengupdate ukuran yang pas untuk window itu
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();
}
}
Hasil Running nya:
Komentar
Posting Komentar