23 Apr 2011

PBO (Program Belah Ketupat dengan JAVA)

Hasil yang akan keluar adalah sebagai berikut:



Listing Program


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Ketupat extends JFrame implements ActionListener{
                private JTextArea output;
                private JButton btnProses, btnTutup, btnHapus, btnNama;
                private JTextField size;
    public Ketupat(){
                                JPanel panel_1=new JPanel();
                                panel_1.setLayout(new FlowLayout());
                                panel_1.add(btnTutup=new JButton("Exit"));
                                panel_1.add(btnHapus=new JButton("Delete"));
                                panel_1.add(btnNama=new JButton("About Me"));
                                btnTutup.setForeground(Color.blue);
                                btnHapus.setForeground(Color.red);
                                btnNama.setForeground(Color.green);
                                JScrollPane scroll = new JScrollPane(output=new JTextArea());
                output.setFont(new Font("Tahoma",0,14));
                                output.setLineWrap(true);
                                output.setWrapStyleWord(true);
                                scroll.setPreferredSize(new Dimension(500, 500));
                                JPanel panel_2=new JPanel();
                                panel_2.setLayout(new BorderLayout());
                                panel_2.add(scroll);
                output.setEditable(false);
                                JPanel panel_3=new JPanel();
                                panel_3.setLayout(new FlowLayout());
                                panel_3.add(new JLabel("Enter number 0 - 100"));
                                panel_3.add(size=new JTextField(3));
                                panel_3.add(btnProses=new JButton("Print"));
                                setLayout(new BorderLayout());
                                add(panel_2,"Center");
                                add(panel_1,"South");
                                add(panel_3,"North");
                                btnProses.addActionListener(this);
                btnHapus.addActionListener(this);
                                btnTutup.addActionListener(this);
                                btnNama.addActionListener(this);  }
                public void actionPerformed(ActionEvent e){
         String str="";
                   if(e.getSource()==btnProses){                  
                                                  int i,j,k;
                                   try{
                                                   int ukuran=Integer.parseInt(size.getText().trim());
               if((ukuran >= 0)&&(ukuran < 101)){
                  for(i=1;i<=ukuran;i++){
                                                                                  for(j=ukuran;j>=i;j--)
                                                                                                  str+=" ";
                                                                                  for(k=1;k<=i;k++)
                                                                                                  str+="*";
                                                                                                 str+="\n";}
                                                                  for(i=ukuran;i>=1;i--){
                                                                                  for(j=i;j<=ukuran;j++)
                                                                                                  str+=" ";
                      for(k=i;k>=1;k--)
                                                                                                  str+="*";
                                                                                                  str+="\n";           }
                                                       output.setText(str);
                                                }else{
                                  JOptionPane.showMessageDialog(null,"Anda salah input!!!","Warning",
                                  JOptionPane.WARNING_MESSAGE);   }
                                   }catch(Exception ex){
                                                   JOptionPane.showMessageDialog(null,"Error--"+ex.toString(),"Error",
                                                   JOptionPane.ERROR_MESSAGE);}}
                if(e.getSource()==btnTutup){
JOptionPane.showMessageDialog(null,"Bye Bye :D", "Exit", JOptionPane.INFORMATION_MESSAGE);
                                                System.exit(0);}
                if(e.getSource()==btnNama){
                                   JOptionPane.showMessageDialog(null,"nothing spesial", "About Me",              JOptionPane.INFORMATION_MESSAGE);}
                   if(e.getSource()==btnHapus){
                       output.setText(null);
                                   size.setText(null);
                                   str="";}}
    public static void main(String[] args){       
        Ketupat frame = new Ketupat();
                                frame.pack();
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.setTitle("Program Belah Ketupat");
                                frame.setVisible(true);}}

0 komentar:

Posting Komentar