Wednesday, April 28, 2010

week 11

today's material :
- event listener
listener is needed to make a button do something when clicked
using method addActionListener

here's the code...
learn from this and the java api...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class EventListener extends JFrame implements ActionListener{

JButton ok;
JButton cancel;
JButton add;
JTextField text;
JTextArea area;
String content;

public EventListener(){
setSize(640, 480);
setTitle("Simple Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

ok = new JButton("OK");
ok.addActionListener(this);

cancel = new JButton("Cancel");
cancel.addActionListener(this);

add = new JButton("Add to List");
add.addActionListener(this);

text = new JTextField(15);
area = new JTextArea();

JPanel pnlSouth = new JPanel(new GridLayout(2, 1));
JPanel pnlNorth = new JPanel(new FlowLayout());

JPanel pnl1 = new JPanel(new FlowLayout());
JPanel pnl2 = new JPanel(new FlowLayout());

pnlNorth.add(new Label("Customer List"));
pnlNorth.setBackground(Color.LIGHT_GRAY);

pnlSouth.add(pnl1);
pnlSouth.add(pnl2);
pnlSouth.setBackground(Color.LIGHT_GRAY);

pnl1.add(text);
pnl1.add(add);

pnl2.add(ok);
pnl2.add(cancel);

getContentPane().add(pnlNorth , BorderLayout.NORTH);
getContentPane().add(pnlSouth , BorderLayout.SOUTH);
getContentPane().add(area);
}


public static void main(String[] args) {
EventListener frame = new EventListener();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == ok){
JOptionPane.showMessageDialog(this, "Button OK Clicked");
}
if(e.getSource() == cancel){
JOptionPane.showMessageDialog(this, "Button Cancel Clicked");
}
if(e.getSource() == add){
content = content + "\n" + text.getText();
area.setText(content);
}
}
}


thats all..............

Wednesday, April 21, 2010

week11 - starting GUI

its week 11 already..
one step further, Graphical User Interface we were taught...
here, this is what i got..

Graphical User Interface
- in java, GUI used by including classes from javax.swing and java.awt packages
- java.awt is the oldest type of GUI
- different from VB and Delphi, java's GUI can be used in wide selection of OS

Subclassing JFrame
- first step using GUI : making frame
- frame consist of : title, size[in pixel]

Trying out
-
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import javax.swing.*;
public class MySimpleFrame extends JFrame{

public MySimpleFrame() throws HeadlessException{
// setting the size of the frame
setSize(300, 200);

// setting the title of the frame
setTitle("My Simple Frame");

// setting the location of the frame
setLocation(100, 200);
// or
setLocationRelativeTo(null);

// setting the background color
getContentPane().setBackground(Color.blue);

// sets to terminate the program when the frame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// initializing button 'ok'
JButton ok = new JButton();
ok.setSize(getMinimumSize());
ok.setText("OK");
ok.setBackground(Color.red);

// initializing button 'cancel'
JButton cancel = new JButton();
cancel.setSize(getMinimumSize());
cancel.setText("Cancel");
cancel.setBackground(Color.red);

// setting panel consist of button 'ok' and 'cancel'
JPanel pnlSouth = new JPanel(new FlowLayout());
add(pnlSouth, BorderLayout.SOUTH);
pnlSouth.setBackground(Color.BLACK);
pnlSouth.add(ok);
pnlSouth.add(cancel);

// makes the frame visible
setVisible(true);
}

public static void main(String[] args) {
MySimpleFrame msf = new MySimpleFrame();
}
}

i've put everything new with comment above it...
so it would be easy to learn from the code only...

that's all....

ciao...

Thursday, April 15, 2010

week 10

what i learn today :

array, which includes :
- array initialization
- array usage
- array in both 1 dimension and 2 dimensions
- array of objects

collections, which includes :
- arraylist
- hashset
- iteration

basic input output :
- how to write / save a file
- how to read from an exisiting file

thats all...

Wednesday, April 7, 2010

week 9

back programming now...
its been 2 weeks...

here's what i got today..

characters and string

try this code
import javax.swing.*;
public class W09Chars {
public static void main(String[] args) {
char ch = 'Z';
byte b = 65;

JOptionPane.showMessageDialog(null, "ASCII code of character " + ch +
" is " + (int)ch);
JOptionPane.showMessageDialog(null, "Character with ASCII code " + b +
" is " + (char)b);
JOptionPane.showMessageDialog(null, ch + " is "
+ ((ch > b) ? "bigger" : "smaller")
+ " than " + (char)b);
}
}

highlight
((ch > b) ? "bigger" : "smaller")
same as
if(ch > b)
"bigger";
else
"smaller"

each character has a code in hexadecimal, which will be shown if the code is ran

back to topic,
character is a single digit while string is array consists of characters

comparing strings
case a : reffering to the same object
String word1 = "Surabaya"
String word2 = word1
both variable reffers to the same object, so
word1 == word2 is true
word1.equals(word2) is true
case b : reffering to different but identical object
String word1 = "Surabaya"
String word2 = new String("Surabaya")
which make
word1 == word2 is false, bacause the address is not the same
word1.equals(word2) is true, because the value is identical
case c : diferent value, different object

pattern matching
sometimes its needed to use a pattern
example 17-08-1945
the regular expression is:
[0-3][0-9]-[0-1][0-9]-[0-9]{4}

string buffer
needed because string is immutable or unchangable
any modification will produce new string
which will use more and more memory


thats all..