ini ada macem2 coding java keren gan, ada EVENT HANDLING, KEY EVENT & PAINTER...
cekidot...KEY EVENT
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyEventTest extends JFrame implements KeyListener {
private String baris1="", baris2="", baris3="";
private JTextArea textArea;
public KeyEventTest() {
super ("Mencoba Key Event");
textArea = new JTextArea (10,15);
textArea.setText("Tekan sembarang tombol di keyboard...");
textArea.setEnabled(false);
textArea.setDisabledTextColor(Color.BLACK);
getContentPane().add(textArea);
addKeyListener (this);
setSize (300,150);
setLocationRelativeTo(null);
setVisible(true);
}
public void keyPressed (KeyEvent e) {
baris1 = "Tombol yang ditekan : " + e.getKeyText(e.getKeyCode());
setLines2and3 (e);
}
public void keyReleased (KeyEvent e) {
baris1 = "Tombol yang dilepas : " + e.getKeyText(e.getKeyCode());
setLines2and3 (e);
}
public void keyTyped (KeyEvent e) {
baris1 = "Tombol yang ditulis : " + e.getKeyChar();
setLines2and3 (e);
}
private void setLines2and3 (KeyEvent e) {
baris2 = "This key is "+ (e.isActionKey() ? "" : "not ") + "an action key";
String temp = e.getKeyModifiersText(e.getModifiers());
baris3 = "Modifier key pressed : " + (temp.equals("") ? "none" : temp);
textArea.setText(baris1 + "\n" + baris2 + "\n" + baris3 + "\n");
}
public static void main (String args[]) {
KeyEventTest test = new KeyEventTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Painter extends JFrame {
private int pointCount = 0;
private Point points[] = new Point[1000];
public Painter () {
super ("Program menggambar sederhana");
getContentPane().add(new JLabel("Drag mouse to draw"), BorderLayout.SOUTH);
addMouseMotionListener (
new MouseMotionAdapter() {
public void mouseDragged (MouseEvent e) {
if (pointCount < points.length) {
points[pointCount] = e.getPoint();
++pointCount;
repaint();
}
}
}
);
setSize (300,150);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint (Graphics g) {
super.paint(g);
for (int i = 0; i < points.length && points[i] != null; i++) {
g.setColor(Color.red);
g.fillOval (points[i].x, points[i].y, 4,4);
}
}
public static void main (String args[]) {
Painter test = new Painter();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
EVENT HANDLING
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClickMe2 extends JFrame {
private JButton tombol, btnExit;
public ClickMe2() {
super ("Event Handling");
Container container = getContentPane();
container.setLayout(new FlowLayout());
ClickListener cl = new ClickListener ();
tombol = new JButton ("Click Me!");
tombol.addActionListener(cl);
container.add(tombol);
btnExit = new JButton ("Exit");
btnExit.addActionListener(cl);
container.add(btnExit);
setSize (200,100);
setVisible (true);
}
public static void main (String arg[]) {
ClickMe2 test = new ClickMe2();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ClickListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource() == tombol) {
JOptionPane.showMessageDialog(null, "You click me again, guys !!!");
} else if (e.getSource() == btnExit){
JOptionPane.showMessageDialog(null, "See you, guys !");
System.exit(0);
}
}
}
}
Tidak ada komentar:
Posting Komentar