sebelum kita masuk ke programnya lebih baik saya sarankan anda harus tau dulu yang dimaksud GUI itu apa?, karena disini saya tidak akan membahasnya. kalau anda sudah membacanya anda bisa kembali Lesson ini.
dimulai dari bentuk sederhana dulu yaitu bentuk Frame.
import javax.swing.JFrame;
public class BentukFrame extends JFrame{
public static void main(String[]args){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setVisible(true);
}
}
ini adalah progam dasar untuk membangun sebuah aplikasi yang udah dibundel dalam packeg swing, jadi sebenarnya code set diatas itu hanya memanggil method-method yang ada dalam kelas JFrame, karena code diatas langsung diturunkan dari kelas JFrame, maka proses pemanggilan methodpun langsung nama methodnya saja, tidak usah dibuatkan object. karena secara otomatis semua isi dari kelas JFrame sudah diturunkan didalam kelas BentukFrame.
Membuat JInternalFrame
import javax.swing.*;
import java.awt.*;
public class AplikasiFrameInternal extends JFrame
{
JDesktopPane desktop=new JDesktopPane();
JInternalFrame iframe=new
JInternalFrame("Internal Frame",true,true,true);
public AplikasiFrameInternal()
{
setTitle("Aplikasi Internal Frame");
setLocation(100,200);
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void komponenVisual()
{
iframe.setLocation(20,20);
iframe.setSize(200,100);
iframe.setVisible(true);
desktop.add(iframe);
setContentPane(desktop);
setVisible(true);
}
public static void main(String[] args)
{
AplikasiFrameInternal if1=new AplikasiFrameInternal();
if1.komponenVisual();
}
}
Membuat JDialog
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BuatDialog extends JDialog implements ActionListener {
public BuatDialog(JFrame parent, String title, String message) {
super(parent, title, true);
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p = parent.getLocation();
setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
}
JPanel messagePane = new JPanel();
messagePane.add(new JLabel(message));
getContentPane().add(messagePane);
JPanel buttonPane = new JPanel();
JButton button = new JButton("OK");
buttonPane.add(button);
button.addActionListener(this);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
public static void main(String[] a) {
BuatDialog dlg = new BuatDialog(new JFrame(), "title", "message");
}
}
Membuat JWindow
import java.awt.*;
import javax.swing.*;
public class Window {
public static void main(String[] args) {
JFrame jframe = new JFrame("Frame Title");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(100, 200);
jframe.setLocation(10, 0);
JWindow jwindow = new JWindow(jframe);
jwindow.setSize(100, 200);
jwindow.setLocation(10, 220);
Container fcp = jframe.getContentPane();
Container wcp = jwindow.getContentPane();
fcp.setBackground(Color.cyan);
wcp.setBackground(Color.magenta);
fcp.add(new JLabel("This is a JFrame",JLabel.CENTER));
wcp.add(new JLabel("This is a Window",JLabel.CENTER));
jframe.setVisible(false);
jwindow.setVisible(true);
}
}
Membuat JFileChooser
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChosserFile extends JFrame implements ActionListener{
JFileChooser fc = new JFileChooser();
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Menu");
JMenuItem opn = new JMenuItem("Open");
JMenuItem sve = new JMenuItem("Save");
JMenuItem ext = new JMenuItem("Exit");
public ChosserFile() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
opn.addActionListener(this);
sve.addActionListener(this);
ext.addActionListener(this);
setJMenuBar(Tampil());
setSize(200,200);
setVisible(true);
}
JMenuBar Tampil(){
bar.add(menu);
menu.add(opn);
menu.add(sve);
menu.add(ext);
return bar;
}
void Open(){
//int = result;
fc.showOpenDialog(this);
//if(result==JFileChooser.APPROVE_OPTION)
}
void Save(){
fc.showSaveDialog(this);
}
public static void main(String [] args){
ChosserFile cf = new ChosserFile();
}
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if(str.equals("Open")){
Open();
}else if(str.equals("Save")){
Save();
}else if (str.equals("Exit")){
System.exit(0);
}
}
}
Membuat JTextField dan JButton
import javax.swing.*;
public class myTextField extends JFrame{
public myTextField(){
JTextField field1 = new JTextField(10);
JTextField field2 = new JTextField(10);
JPanel pane1 = new JPanel();
pane1.add(field1);
pane1.add(field2);
JButton button1 = new JButton("OK");
JButton button2 = new JButton("Cancel");
JPanel pane2 = new JPanel();
pane2.add(button1);
pane2.add(button2);
getContentPane().add(pane1,"North");
getContentPane().add(pane2,"Center");
setSize(200,200);
setVisible(true);
}
public static void main(String[]args){
new myTextField();
}
}
Membuat JList
import java.awt.*;
import javax.swing.*;
public class FileList extends JFrame{
public FileList() {
super("suherman");
Container content = getContentPane();
String[] str={"regular","Bold","Italic","BoldItalic","UnderLine"};
JList lis = new JList(str);
lis.setLayoutOrientation(JList.VERTICAL_WRAP);
lis.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JScrollPane pane = new JScrollPane(lis);
pane.setPreferredSize(new Dimension(400,300));
content.setLayout(new GridLayout());
content.add(pane);
setSize(200,100);
setVisible(true);
}
public static void main(String[]args){
FileList fl = new FileList();
}
}
Membuat JRadioButton
import javax.swing.*;
import java.awt.*;
public class FileRadioButon extends JFrame{
public FileRadioButon() {
super("suherman");
Container content = getContentPane();
JRadioButton rb = new JRadioButton("Nama");
//rb.setMnemonic(KeyEvent.VK_B);
rb.setActionCommand("Nama");
//rb.setSelected(true);
JRadioButton almt = new JRadioButton("Alamat");
//rb.setMnemonic(KeyEvent.VK_B);
rb.setActionCommand("Alamat");
//rb.setSelected(true);
JRadioButton hby = new JRadioButton("Hoby");
//rb.setMnemonic(KeyEvent.VK_B);
rb.setActionCommand("Hoby");
//rb.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(rb);
bg.add(almt);
bg.add(hby);
//content.setLayout(new FlowLayout());
//content.add(rb);
//content.add(almt);
//content.add(hby);
Box bs = new Box(BoxLayout.Y_AXIS);
bs.add(rb);
bs.add(almt);
bs.add(hby);
content.add(bs);
setSize(200,200);
setVisible(true);
}
public static void main(String[]args){
FileRadioButon rd = new FileRadioButon();
}
}
Membuat JComboBox
import javax.swing.*;
import java.awt.*;
public class FilleComboBox extends JFrame{
String[] str={"NIM","Nama","Alamat","No. ID"};
JComboBox cb = new JComboBox(str);
public FilleComboBox() {
super("ProgramMan");
setDefaultCloseOperation(EXIT_ON_CLOSE);
cb.setEditable(true);
Container content = getContentPane();
content.setLayout(new FlowLayout());
content.add(cb);
setSize(200,200);
setVisible(true);
}
public static void main (String[]args){
FilleComboBox fcb = new FilleComboBox();
}
}
Manipulasi Tampilan dengan BorderFactory
import javax.swing.*;
import java.awt.*;
public class BorderFaktori extends JFrame{
public BorderFaktori() {
Tampil();
}
public void Tampil(){
JLabel lb = new JLabel("Nama ");
JLabel lb2 = new JLabel("Password");
JTextField fil = new JTextField(10);
JPasswordField ps = new JPasswordField(10);
JPanel pane = new JPanel();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
pane.add(lb);
pane.add(fil);
pane1.add(lb2);
pane1.add(ps);
pane2.setLayout(new FlowLayout());
pane2.add(pane);
pane2.add(pane1);
getContentPane().add(pane2,"Center");
pane2.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(20,20,10,20),
BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),"Data")));
}
public static void main(String[]args){
BorderFaktori bf = new BorderFaktori();
bf.setSize(300,200);
bf.setVisible(true);
}
}
Menggunakan Box Layout
import javax.swing.*;
import java.awt.*;
public class BikinLayout extends JFrame{
public BikinLayout() {
Box comp = new Box(BoxLayout.Y_AXIS);
JButton bt = new JButton("PS1");
JButton btn = new JButton("PS2");
JButton bt3 = new JButton("PS3");
JButton bt4 = new JButton("PS4");
comp.add(bt);
comp.add(btn);
comp.add(bt3);
comp.add(bt4);
getContentPane().add(comp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[]args){
BikinLayout ly = new BikinLayout();
LayoutFlow fl = new LayoutFlow();
}
}
Menggunakan GridLayout
import javax.swing.*;
import java.awt.*;
public class LayoutGrid extends JFrame{
public LayoutGrid() {
JButton bt = new JButton("SATU");
JButton bt1 = new JButton("DUA");
JTextField f = new JTextField("nama");
JTextField fr = new JTextField("Kelas");
GridLayout gd = new GridLayout(3,2);
gd.setHgap(5);
gd.setVgap(10);
getContentPane().setLayout(gd);
getContentPane().add(bt);
getContentPane().add(bt1);
getContentPane().add(f);
getContentPane().add(fr);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);
}
public static void main(String[]args){
LayoutGrid lg = new LayoutGrid();
}
}
Menggunakan GridBagLayout
import javax.swing.*;
import java.awt.*;
public class LayoutGridBag extends JFrame{
public LayoutGridBag() {
GridBagLayout gbl = new GridBagLayout();
Container pane = getContentPane();
pane.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill=GridBagConstraints.NONE;
JButton bt ;
bt = new JButton("Button1");
gbc.weightx=1;
gbc.gridx=0;
gbc.gridy=0;
gbc.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(bt,gbc);
pane.add(bt);
JButton btn = new JButton("Button2");
gbc.gridx=1;
gbc.gridy=0;
gbc.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(btn,gbc);
pane.add(btn);
bt = new JButton("Button3");
gbc.gridx=2;
gbc.gridy=0;
gbc.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(bt,gbc);
pane.add(bt);
bt = new JButton("Button4");
gbc.ipady=40;
gbc.gridwidth=3;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx=0;
gbc.gridy=1;
gbl.setConstraints(bt,gbc);
pane.add(bt);
bt = new JButton("Button5");
gbc.ipady=0;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets= new Insets(10,0,10,0);
gbc.gridx=1;
gbc.gridwidth=1;
gbc.gridy=2;
gbl.setConstraints(bt, gbc);
pane.add(bt);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[]args){
LayoutGridBag lbg = new LayoutGridBag();
}
}
Menggunakan JTree
import javax.swing.*;
import javax.swing.tree.*;
public class AboutJtree{
public static void main(String[] args) {
JFrame frame = new JFrame("Creating a JTree Component!");
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Color", true);
DefaultMutableTreeNode black = new DefaultMutableTreeNode("Black");
DefaultMutableTreeNode blue = new DefaultMutableTreeNode("Blue");
DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("Navy Blue");
DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("Dark Blue");
DefaultMutableTreeNode green = new DefaultMutableTreeNode("Green");
DefaultMutableTreeNode white = new DefaultMutableTreeNode("White");
parent.add(black);
parent.add(blue);
blue.add(nBlue);
blue.add(dBlue);
parent.add(green );
parent.add(white);
JTree tree = new JTree(parent);
frame.add(tree);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
}
}
wuah.......ngantuk euy..bersambung duyu yah... :-)