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...

No comments:

Post a Comment