TUGAS
PENGANTAR TEKNIK INFORMASI
Dosen Pembimbing Lia Ulfa,
S.ST
Contoh Program yang
Dijalankan di Under Windows
Disusun Oleh
Muhammad Budi Rahman
TI.13105
Jurusan Teknik Informatika
AKADEMI KOMUNITAS NEGERI
KABUPATEN SUMBAWA BARAT
Program
Yang dijalankan Melalaui UNDER WINDOWS
Contoh
program berikut ini memberikan contoh bagaimana penanganan event di dalam
window. Event akan aktif saat window diubah ukurannnya, diclose, aktif, dan
sebagainya. Listener yang digunakan dalam contoh program ini adalah
WindowListener, WindowFocusListener dan WindowStateListener.
1. /*
2. * WindowEventDemo.java is a 1.4 example that
requires
3. * no other files.
4. */
5.
6. import
javax.swing.*;
7. import
java.awt.*;
8. import
java.awt.event.*;
9.
10. public class
WindowEventDemo extends JPanel
11. implements
WindowListener,
12. WindowFocusListener,
13. WindowStateListener
{
14. final static String newline =
"\n";
15. final static String space = " ";
16. static JFrame frame;
17. JTextArea display;
18.
19. public WindowEventDemo() {
20. super(new BorderLayout());
21. display = new JTextArea();
22. display.setEditable(false);
23. JScrollPane scrollPane = new
JScrollPane(display);
24. scrollPane.setPreferredSize(new
Dimension(500, 450));
25. add(scrollPane, BorderLayout.CENTER);
26.
27. frame.addWindowListener(this);
28. frame.addWindowFocusListener(this);
29. frame.addWindowStateListener(this);
30.
31. checkWM();
32. }
33.
34. //Some window managers don't support all
window states.
35. //For example, dtwm doesn't support true
maximization,
36. //but mimics it by resizing the window to
be the size
37. //of the screen. In this case the window does not fire
38. //the MAXIMIZED_ constants on the window's
state listener.
39. //Microsoft Windows supports
MAXIMIZED_BOTH, but not
40. //MAXIMIZED_VERT or MAXIMIZED_HORIZ.
41. public void checkWM() {
42. Toolkit tk = frame.getToolkit();
43. if
(!(tk.isFrameStateSupported(Frame.ICONIFIED))) {
44. displayMessage(
45. "Your window manager
doesn't support ICONIFIED.");
46. }
47. if
(!(tk.isFrameStateSupported(Frame.MAXIMIZED_VERT))) {
48. displayMessage(
49. "Your window manager
doesn't support MAXIMIZED_VERT.");
50. }
51. if
(!(tk.isFrameStateSupported(Frame.MAXIMIZED_HORIZ))) {
52. displayMessage(
53. "Your window manager
doesn't support MAXIMIZED_HORIZ.");
54. }
55. if
(!(tk.isFrameStateSupported(Frame.MAXIMIZED_BOTH))) {
56. displayMessage(
57. "Your window manager
doesn't support MAXIMIZED_BOTH.");
58. } else {
59. displayMessage(
60. "Your window manager
supports MAXIMIZED_BOTH.");
61. }
62. }
63.
64. public void windowClosing(WindowEvent e) {
65. displayMessage("WindowListener
method called: windowClosing.");
66.
67. //A pause so user can see the message
before
68. //the window actually closes.
69. ActionListener task = new
ActionListener() {
70. boolean alreadyDisposed = false;
71. public void actionPerformed(ActionEvent
e) {
72. if (!alreadyDisposed) {
73. alreadyDisposed = true;
74. frame.dispose();
75. } else { //make sure the
program exits
76. System.exit(0);
77. }
78. }
79. };
80. Timer timer = new Timer(500, task);
//fire every half second
81. timer.setInitialDelay(2000); //first delay 2 seconds
82. timer.start();
83. }
84.
85. public void windowClosed(WindowEvent e) {
86. //This will only be seen on standard
output.
87. displayMessage("WindowListener
method called: windowClosed.");
88. }
89.
90. public void windowOpened(WindowEvent e) {
91. displayMessage("WindowListener
method called: windowOpened.");
92. }
93.
94. public void windowIconified(WindowEvent e)
{
95. displayMessage("WindowListener
method called: windowIconified.");
96. }
97.
98. public void windowDeiconified(WindowEvent
e) {
99. displayMessage("WindowListener
method called: windowDeiconified.");
100.
}
101.
102.
public void windowActivated(WindowEvent e)
{
103.
displayMessage("WindowListener
method called: windowActivated.");
104.
}
105.
106.
public void windowDeactivated(WindowEvent
e) {
107.
displayMessage("WindowListener
method called: windowDeactivated.");
108.
}
109.
110.
public void windowGainedFocus(WindowEvent
e) {
111.
displayMessage("WindowFocusListener
method called: windowGainedFocus.");
112.
}
113.
114.
public void windowLostFocus(WindowEvent e)
{
115.
displayMessage("WindowFocusListener
method called: windowLostFocus.");
116.
}
117.
118.
public void windowStateChanged(WindowEvent
e) {
119.
displayStateMessage(
120.
"WindowStateListener method
called: windowStateChanged.", e);
121.
}
122.
123.
void displayMessage(String msg) {
124.
display.append(msg + newline);
125.
System.out.println(msg);
126.
}
127.
128.
void displayStateMessage(String prefix,
WindowEvent e) {
129.
int state = e.getNewState();
130.
int oldState = e.getOldState();
131.
String msg = prefix
132.
+ newline + space
133.
+ "New state: "
134.
+
convertStateToString(state)
135.
+ newline + space
136.
+ "Old state: "
137.
+
convertStateToString(oldState);
138.
display.append(msg + newline);
139.
System.out.println(msg);
140.
}
141.
142.
String convertStateToString(int state) {
143.
if (state == Frame.NORMAL) {
144.
return "NORMAL";
145.
}
146.
if ((state & Frame.ICONIFIED) != 0)
{
147.
return "ICONIFIED";
148.
}
149.
//MAXIMIZED_BOTH is a concatenation of
two bits, so
150.
//we need to test for an exact match.
151.
if
((state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) {
152.
return "MAXIMIZED_BOTH";
153.
}
154.
if ((state & Frame.MAXIMIZED_VERT)
!= 0) {
155.
return "MAXIMIZED_VERT";
156.
}
157.
if ((state & Frame.MAXIMIZED_HORIZ)
!= 0) {
158.
return "MAXIMIZED_HORIZ";
159.
}
160.
return "UNKNOWN";
161.
}
162.
163.
/**
164.
* Create the GUI and show it. For thread safety,
165.
* this method should be invoked from the
166.
* event-dispatching thread.
167.
*/
168.
private static void createAndShowGUI() {
169.
//Make sure we have nice window
decorations.
170.
JFrame.setDefaultLookAndFeelDecorated(true);
171.
172.
//Create and set up the window.
173.
frame = new
JFrame("WindowEventDemo");
174.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
175.
176.
//Create and set up the content pane.
177.
JComponent newContentPane = new
WindowEventDemo();
178.
newContentPane.setOpaque(true);
//content panes must be opaque
179.
frame.setContentPane(newContentPane);
180.
181.
//Display the window.
182.
frame.pack();
183.
frame.setVisible(true);
184.
}
185.
186.
public static void main(String[] args) {
187.
//Schedule a job for the
event-dispatching thread:
188.
//creating and showing this
application's GUI.
189.
javax.swing.SwingUtilities.invokeLater(new
Runnable() {
190.
public void run() {
191.
createAndShowGUI();
192.
}
193.
});
194.
}
195.
}
Program Yang dijalankan Melalaui UNDER
DOS
Berikut Progrmnya;
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
main()
{
int i,jd,uk,jmlah,total,harga;
char mc,lagi,enter;
atas:
clrscr();
cout<<”\t\tBOYCREATIONS”<<endl;
cout<<”\tPusat Penjualan Cat Tembok”<<endl;
cout<<”———————————”<<endl;
cout<<”\A. ICI Dulux”<<endl;
cout<< “1. Ukuran Kecil –> Rp. 15000\n”;
cout<< “2. Ukuran Sedang –> Rp. 20000\n”;
cout<< “3. Ukuran Besar –> Rp. 25000\n”;
cout<<”\nB. Movilex VIP”<<endl;
cout<< “1. Ukuran Kecil –> Rp. 13500\n”;
cout<< “2. Ukuran Sedang –> Rp. 17500\n”;
cout<< “3. Ukuran Besar –> Rp. 20000\n”;
cout<<”\nC. Jotun Strax”<<endl;
cout<< “1. Ukuran Kecil –> Rp. 15000\n”;
cout<< “2. Ukuran Sedang –> Rp. 18500\n”;
cout<< “3. Ukuran Besar –> Rp. 22000\n”;
cout<<”———————————”<<endl;
cout<<”Input Jumlah Data : “;cin>>jd;
for (i=1;i<=jd;i++)
{
cout<<”———————————”<<endl;
cout<<”Pilih Merek Cat [A/B/C] : “;cin>>mc;
cout<<”Pilih Ukuran Kaleng [1/2/3] : “;cin>>uk;
if (mc==’A'||mc==’a')
switch(uk)
{
case 1:harga=15000;break;
case 2:harga=20000;break;
case 3:harga=25000;break;
default:harga=0;break;
}
else if (mc==’B'||mc==’b')
switch(uk)
{
case 1:harga=13500;break;
case 2:harga=17500;break;
case 3:harga=20000;break;
default:harga=0;break;
}
else if(mc==’C'||mc==’c')
switch(uk)
{
case 1:harga=15000;break;
case 2:harga=18500;break;
case 3:harga=22000;break;
default:harga=0;break;
}
else
{cout<<”\nAnda salah memasukan kode cat (GUNAKAN HURUF KAPITAL)”;
cout<<”\nTEKAN (B) LALU ENTER UNTUK KEMBALI MENGHITUNG : “;cin>>enter;
if(enter==’B'||enter==’b')
goto atas;
else
cout<<”\tDATA EROR”;
goto bawah;}
cout<<”Harga Satuan Barang : Rp.”<<harga<<endl;
cout<<”Jumlah Yang dibeli : “;cin>>jmlah;
total=(harga*jmlah);
cout<<”Harga yang harus dibayar : Rp.”<<total<<endl;
cout<<”———————————”<<endl;
}
cout<<”Ingin Hitung Lagi [Y/N] : “;cin>>lagi;
if (lagi==’Y'||lagi==’y')
goto atas;
else
cout<<”\n TERIMA KASIH BANYAK “<<endl;
bawah:
getch();
}
#include <stdio.h>
#include <iostream.h>
main()
{
int i,jd,uk,jmlah,total,harga;
char mc,lagi,enter;
atas:
clrscr();
cout<<”\t\tBOYCREATIONS”<<endl;
cout<<”\tPusat Penjualan Cat Tembok”<<endl;
cout<<”———————————”<<endl;
cout<<”\A. ICI Dulux”<<endl;
cout<< “1. Ukuran Kecil –> Rp. 15000\n”;
cout<< “2. Ukuran Sedang –> Rp. 20000\n”;
cout<< “3. Ukuran Besar –> Rp. 25000\n”;
cout<<”\nB. Movilex VIP”<<endl;
cout<< “1. Ukuran Kecil –> Rp. 13500\n”;
cout<< “2. Ukuran Sedang –> Rp. 17500\n”;
cout<< “3. Ukuran Besar –> Rp. 20000\n”;
cout<<”\nC. Jotun Strax”<<endl;
cout<< “1. Ukuran Kecil –> Rp. 15000\n”;
cout<< “2. Ukuran Sedang –> Rp. 18500\n”;
cout<< “3. Ukuran Besar –> Rp. 22000\n”;
cout<<”———————————”<<endl;
cout<<”Input Jumlah Data : “;cin>>jd;
for (i=1;i<=jd;i++)
{
cout<<”———————————”<<endl;
cout<<”Pilih Merek Cat [A/B/C] : “;cin>>mc;
cout<<”Pilih Ukuran Kaleng [1/2/3] : “;cin>>uk;
if (mc==’A'||mc==’a')
switch(uk)
{
case 1:harga=15000;break;
case 2:harga=20000;break;
case 3:harga=25000;break;
default:harga=0;break;
}
else if (mc==’B'||mc==’b')
switch(uk)
{
case 1:harga=13500;break;
case 2:harga=17500;break;
case 3:harga=20000;break;
default:harga=0;break;
}
else if(mc==’C'||mc==’c')
switch(uk)
{
case 1:harga=15000;break;
case 2:harga=18500;break;
case 3:harga=22000;break;
default:harga=0;break;
}
else
{cout<<”\nAnda salah memasukan kode cat (GUNAKAN HURUF KAPITAL)”;
cout<<”\nTEKAN (B) LALU ENTER UNTUK KEMBALI MENGHITUNG : “;cin>>enter;
if(enter==’B'||enter==’b')
goto atas;
else
cout<<”\tDATA EROR”;
goto bawah;}
cout<<”Harga Satuan Barang : Rp.”<<harga<<endl;
cout<<”Jumlah Yang dibeli : “;cin>>jmlah;
total=(harga*jmlah);
cout<<”Harga yang harus dibayar : Rp.”<<total<<endl;
cout<<”———————————”<<endl;
}
cout<<”Ingin Hitung Lagi [Y/N] : “;cin>>lagi;
if (lagi==’Y'||lagi==’y')
goto atas;
else
cout<<”\n TERIMA KASIH BANYAK “<<endl;
bawah:
getch();
}
Tidak ada komentar:
Posting Komentar