Layout Pada Java


Kali ini kita akan belajar membuat layout pada java. langsung saja ketikkan script dibawah ini :

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Tugassaya{
public static void addComponentsToPane(Container pane){
JLabel label;
JLabel labelB;
JLabel labelC;
pane.setLayout(null);
label = new JLabel(“FlowLayout”);
pane.add(label);
JButton a = new JButton(“0″);
JButton b = new JButton(“1″);
JButton c = new JButton(“2″);
JButton d = new JButton(“3″);
JButton e = new JButton(“4″);
JButton f = new JButton(“5″);
JButton g = new JButton(“6″);
JButton h = new JButton(“7″);
JButton i = new JButton(“8″);
JButton j = new JButton(“9″);
JButton k = new JButton(“10″);
JButton l = new JButton(“11″);
pane.add(a);
pane.add(b);
pane.add(c);
pane.add(d);
pane.add(e);
pane.add(f);
pane.add(g);
pane.add(h);
pane.add(i);
pane.add(j);
pane.add(k);
pane.add(l);
i.setBounds(500,50,55,20);
j.setBounds(500,50,55,20);
k.setBounds(500,50,55,20);
l.setBounds(500,50,55,20);
labelB = new JLabel(“None Layout”);
pane.add(labelB);
labelC = new JLabel(“Grid Layout”);
pane.add(labelC);
Insets insets = pane.getInsets();
Dimension size = a.getPreferredSize();
a.setBounds(10+insets.left,50+insets.top,size.width,size.height);
size=b.getPreferredSize();
b.setBounds(60+insets.left,50+insets.top,size.width,size.height);
size=c.getPreferredSize();
c.setBounds(110+insets.left,50+insets.top,size.width,size.height);
size=d.getPreferredSize();
d.setBounds(160+insets.left,50+insets.top,size.width,size.height);
size=e.getPreferredSize();
e.setBounds(230+insets.left,50+insets.top,size.width,size.height);
size=f.getPreferredSize();
f.setBounds(230+insets.left,77+insets.top,size.width,size.height);
size=g.getPreferredSize();
g.setBounds(230+insets.left,104+insets.top,size.width,size.height);
size=h.getPreferredSize();
h.setBounds(230+insets.left,131+insets.top,size.width,size.height);
size=i.getPreferredSize();
i.setBounds(380+insets.left,50+insets.top,size.width,size.height);
size=j.getPreferredSize();
j.setBounds(430+insets.left,50+insets.top,size.width,size.height);
size=k.getPreferredSize();
k.setBounds(380+insets.left,77+insets.top,size.width,size.height);
size=l.getPreferredSize();
l.setBounds(430+insets.left,77+insets.top,size.width,size.height);
size=label.getPreferredSize();
label.setBounds(10+insets.left,15+insets.top,size.width,size.height);
size=labelB.getPreferredSize();
labelB.setBounds(230+insets.left,15+insets.top,size.width,size.height);
size=labelC.getPreferredSize();
labelC.setBounds(385+insets.left,15+insets.top,size.width,size.height);
}
public static void main(String [] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(“Multi Layout”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());

Insets insets = frame.getInsets();
frame.setSize(450+insets.left+insets.right,250+insets.top+insets.bottom);
frame.setVisible(true);
}
}
Output:

0 Response to "Layout Pada Java"

Post a Comment