Laman

Sunday, December 30, 2012

MeKuHiSwing.java Merah Kuning Hijau Swing

 import javax.swing.JFrame;  
 import javax.swing.JButton;  
 import java.awt.Dimension;  
 import java.awt.Toolkit;  
 import javax.swing.JPanel;  
 import java.awt.Color;  
 import java.awt.event.ActionListener;  
 import java.awt.event.ActionEvent;  
 public class MeKuHiSwing {  
       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 JFrame f = new JFrame("Merah Kuning Hijau");  
           f.setLayout(null);  
        f.setLocation(awalJendX,awalJendY);  
        f.setSize(lebarJendela,tinggiJendela);  
        f.setVisible(true);  
           f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);  
        final JPanel panelAtas    = new JPanel();  
        panelAtas.setSize(lebarPanel,tinggiPanel);  
        panelAtas.setLocation(0,0);  
        panelAtas.setBackground(Color.red);  
        f.add(panelAtas);  
           final JPanel panelTengah  = new JPanel();  
           panelTengah.setSize(lebarPanel,tinggiPanel);  
           panelTengah.setLocation(0,tinggiJendela/3);  
           panelTengah.setBackground(Color.yellow);  
           f.add(panelTengah);  
           final JPanel panelBawah  = new JPanel();  
           panelBawah.setSize(lebarPanel,tinggiPanel);  
           panelBawah.setLocation(0,2*tinggiJendela/3);  
           panelBawah.setBackground(Color.green);  
           f.add(panelBawah);  
           final JButton tombolAtas = new JButton();  
           tombolAtas.setSize(lebarTombol,tinggiTombol);  
           tombolAtas.setLocation(awalTombX,awalTombY);  
           tombolAtas.setLabel("Ceklek");  
           tombolAtas.setVisible(false);  
           panelAtas.add(tombolAtas);  
           panelAtas.setLayout(null);  
           final JButton tombolTengah = new JButton();  
           tombolTengah.setSize(lebarTombol,tinggiTombol);  
           tombolTengah.setLocation(awalTombX,awalTombY);  
           tombolTengah.setLabel("Ceklek");  
           tombolTengah.setVisible(false);  
           panelTengah.add(tombolTengah);  
           panelTengah.setLayout(null);  
           final JButton tombolBawah = new JButton();  
           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);  
                  }  
                  }  
           );  
      }  
 }  

No comments:

Post a Comment