Laman

Saturday, December 29, 2012

MeKuHi.java Merah Kuning Hijau AWT

 import java.awt.Frame;  
 import java.awt.Button;  
 import java.awt.Dimension;  
 import java.awt.Toolkit;  
 import java.awt.Panel;  
 import java.awt.Color;  
 import java.awt.event.WindowListener;  
 import java.awt.event.WindowAdapter;  
 import java.awt.event.WindowEvent;  
 import java.awt.event.ActionListener;  
 import java.awt.event.ActionEvent;  
 public class MeKuHi{  
       public static void main(String args[]){  
        int lebarJendela = 250;  
        int tinggiJendela = 600;  
        int lebarPanel  = lebarJendela;  
        int tinggiPanel  = tinggiJendela/3;  
           int lebarTombol = 100;  
           int tinggiTombol = 30;  
           int awalTombX  = (lebarPanel-lebarTombol)/2;  
           int awalTombY  = (tinggiPanel-tinggiTombol)/2;  
        Dimension Layar  = Toolkit.getDefaultToolkit().getScreenSize();  
        int lebarLayar  = Layar.width;  
        int tinggiLayar  = Layar.height;  
        int awalJendX = (lebarLayar-lebarJendela)/2;  
        int awalJendY = (tinggiLayar-tinggiJendela)/2;  
        final Frame f = new Frame("Merah Kuning Hijau");  
           f.setLayout(null);  
        f.setLocation(awalJendX,awalJendY);  
        f.setSize(lebarJendela,tinggiJendela);  
        f.setVisible(true);  
        final Panel panelAtas    = new Panel();  
        panelAtas.setSize(lebarPanel,tinggiPanel);  
        panelAtas.setLocation(0,0);  
        panelAtas.setBackground(Color.red);  
        f.add(panelAtas);  
           final Panel panelTengah  = new Panel();  
           panelTengah.setSize(lebarPanel,tinggiPanel);  
           panelTengah.setLocation(0,tinggiJendela/3);  
           panelTengah.setBackground(Color.yellow);  
           f.add(panelTengah);  
           final Panel panelBawah  = new Panel();  
           panelBawah.setSize(lebarPanel,tinggiPanel);  
           panelBawah.setLocation(0,2*tinggiJendela/3);  
           panelBawah.setBackground(Color.green);  
           f.add(panelBawah);  
           final Button tombolAtas = new Button();  
           tombolAtas.setSize(lebarTombol,tinggiTombol);  
           tombolAtas.setLocation(awalTombX,awalTombY);  
           tombolAtas.setLabel("Ceklek");  
           tombolAtas.setVisible(false);  
           panelAtas.add(tombolAtas);  
           panelAtas.setLayout(null);  
           final Button tombolTengah = new Button();  
           tombolTengah.setSize(lebarTombol,tinggiTombol);  
           tombolTengah.setLocation(awalTombX,awalTombY);  
           tombolTengah.setLabel("Ceklek");  
           tombolTengah.setVisible(false);  
           panelTengah.add(tombolTengah);  
           panelTengah.setLayout(null);  
           final Button tombolBawah = new Button();  
           tombolBawah.setSize(lebarTombol,tinggiTombol);  
           tombolBawah.setLocation(awalTombX,awalTombY);  
           tombolBawah.setLabel("Ceklek");  
           panelBawah.setLayout(null);  
           panelBawah.add(tombolBawah);  
           tombolAtas.addActionListener(  
                 new ActionListener(){  
                   public void actionPerformed(ActionEvent e){  
                         f.setTitle("Kuning Hijau Merah");  
                         panelAtas.setBackground(Color.yellow);  
                         panelTengah.setBackground(Color.green);  
                         panelBawah.setBackground(Color.red);  
                         tombolTengah.setVisible(true);  
                         tombolAtas.setVisible(false);  
                   }  
              }  
            );  
           tombolTengah.addActionListener(  
                new ActionListener(){  
                     public void actionPerformed(ActionEvent e){  
                          f.setTitle("Merah Kuning Hijau");  
                          panelAtas.setBackground(Color.red);  
                          panelTengah.setBackground(Color.yellow);  
                          panelBawah.setBackground(Color.green);  
                       tombolBawah.setVisible(true);  
                         tombolTengah.setVisible(false);  
                    }  
              }  
            );  
           tombolBawah.addActionListener(  
                  new ActionListener(){  
                       public void actionPerformed(ActionEvent e){  
                       f.setTitle("Hijau Merah Kuning");  
                          panelAtas.setBackground(java.awt.Color.green);  
                          panelTengah.setBackground(java.awt.Color.red);  
                          panelBawah.setBackground(java.awt.Color.yellow);  
                          tombolAtas.setVisible(true);  
                          tombolBawah.setVisible(false);  
                  }  
                  }  
           );  
        f.addWindowListener(new WindowAdapter(){  
              public void windowClosing(WindowEvent we){  
               System.exit(0);  
                   }  
             });  
            }  
           public void actionPerformed(ActionEvent e){}  
 }  

No comments:

Post a Comment