Menampilkan CheckBox di Java

6 Mei 2011

Contoh program java berikut ini mendemokan bagaimana penanganan checkbox di java. Bukan hanya bagaimana membuat object checkbox dengan class JCheckBox, namun juga contoh penanganan event dengan ItemListener. Berikut tampilan output yang dihasilkan :


Berikut ini program lengkapnya :


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CheckBoxTest extends JFrame {

   private JTextField text;

   private JCheckBox bold, italic, merah;

   public CheckBoxTest() {

       super ("Mencoba CheckBox");

       Container container = getContentPane ();

       container.setLayout(new FlowLayout());

       text = new JTextField ("Hello Java !", 20);

       text.setFont(new Font ("Arial", Font.PLAIN, 14));

       text.setForeground(Color.BLACK);

       text.setBackground(Color.WHITE);

       text.setEditable(false);

       container.add(text);

       bold = new JCheckBox ("Bold");

       container.add(bold);

       italic = new JCheckBox ("Italic");

       container.add(italic);

       merah = new JCheckBox ("Merah");

       container.add(merah);

       CheckBoxHandler atur = new CheckBoxHandler();

       bold.addItemListener(atur);

       italic.addItemListener(atur);

       merah.addItemListener(atur);

       setSize (275,100);

       setLocationRelativeTo(null);

       setVisible(true);

   }

   public static void main (String args[]) {

       CheckBoxTest test = new CheckBoxTest();

       test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }

   //inner class

   private class CheckBoxHandler implements ItemListener {

       private int valBold = Font.PLAIN;

       private int valItalic = Font.PLAIN;

       private Color valMerah = Color.BLACK;

       //method untuk menangani checkbox event

       public void itemStateChanged(ItemEvent e) {

           if (e.getSource() == bold) {

               valBold = bold.isSelected() ? Font.BOLD : Font.PLAIN;

           }

           if (e.getSource() == italic) {

               valItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN;

           }

           text.setFont(new Font ("Arial",valBold + valItalic, 14) );

           if (e.getSource() == merah) {

               valMerah = merah.isSelected() ? Color.RED : Color.BLACK;

           }

           text.setForeground(valMerah);

       }

   } //end of inner class

} //end of class CheckBoxTest





Semoga bermanfaat buat sobat sekalian,.


Sumber : http://contohprogram.info/
READ MORE - Menampilkan CheckBox di Java

Cara Membuat Form Login Sederhana Menggunakan Java

Contoh program sederhana yang menampilkan form login di dalam JFrame. Inputan username berupa JTextField dan inputan password berupa JPasswordField serta tombol dibuat dengan JButton. Form sederhana ini juga mendemonstrasikan adanya Listener berupa ActionListener. Berikut tampilan output yang dihasilkan :


Berikut ini program lengkapnya :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleLogin extends JFrame implements ActionListener {
    private JLabel label1, label2;
    private JTextField txtUser;
    private JPasswordField pwdPass;
    private JButton btnLogin, btnExit;

    public SimpleLogin() {
        super ("Login here...");

        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        label1 = new JLabel ("Username : ");
        label2 = new JLabel ("Password : ");

        txtUser = new JTextField (20);
        txtUser.setToolTipText("Input Username");
        pwdPass = new JPasswordField(20);

        btnLogin = new JButton ("Login");
        btnLogin.addActionListener(this);
        btnExit = new JButton ("Exit");
        btnExit.addActionListener(this);

        container.add(label1);
        container.add(txtUser);
        container.add(label2);
        container.add(pwdPass);
        container.add(btnLogin);
        container.add(btnExit);

        setSize (300,200);
        setVisible (true);
    }

    public static void main (String args[]) {
        SimpleLogin test = new SimpleLogin();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private String user = "", pass = "";
    public void actionPerformed (ActionEvent e) {
        if (e.getSource() == btnLogin) {
            user = txtUser.getText();
            pass = pwdPass.getText();
          if (user.equals("raditz") && pass.equals("aalizzwell")) {
          JOptionPane.showMessageDialog(null, "Login successfull");
            } else {
          JOptionPane.showMessageDialog(null, "Username and password dosn't match!");
                txtUser.setText("");
                pwdPass.setText("");
                txtUser.requestFocus(true);
            }
        } else if (e.getSource() == btnExit){
            JOptionPane.showMessageDialog(null,"Thanks to try my program. See you..");
            System.exit(0);
        }
    }
}


Semoga bermanfaat buat sobat sekalian,.


Sumber : http://contohprogram.info/
READ MORE - Cara Membuat Form Login Sederhana Menggunakan Java

Class Painter : Menggambar dengan Mouse di Java

Class Painter dapat digunakan untuk membuat program kecil yang membentuk garis dengan mouse (saat mouse di-drag). Berikut tampilan output yang dihasilkan :


Berikut ini program lengkapnya :
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter extends JFrame {
    private int pointCount = 0;
    private Point points[] = new Point[1000];

    public Painter () {
        super ("Program menggambar sederhana");

        getContentPane().add(new JLabel("Drag mouse to draw"), BorderLayout.SOUTH);

        addMouseMotionListener (
            new MouseMotionAdapter() {
                public void mouseDragged (MouseEvent e) {
                    if (pointCount < points.length) {
                        points[pointCount] = e.getPoint();
                        ++pointCount;
                        repaint();
                    }
                }
            } //end of anonymous class
        ); //end method addMotionListener

        setSize (300,150);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void paint (Graphics g) {
        super.paint(g);
        for (int i = 0; i < points.length && points[i] != null; i++) {
            g.setColor(Color.red);
            g.fillOval (points[i].x, points[i].y, 4,4);
        }
    }

    public static void main (String args[]) {
     Painter test = new Painter();
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
 
 
Semoga bermanfaat buat sobat sekalian,.


Sumber : http://contohprogram.info/
READ MORE - Class Painter : Menggambar dengan Mouse di Java

Exam DO’s and DON’Ts

5 Mei 2011

Exam time is coming again! Many of us are facing final exams, national exams, university entrance exams, and even course exams, and for most, it is a stressful time. Here are great preparation tips for before, during, and after the exams to make your exam experience a bearable one.

DO’s

  • Before : Prepare items you need for the exam, including writing utensils, calculator, identification card, etc. It’s calming for the nerves if you arrive on time, so you have time to find your seat and settle down without feeling rushed. Praying is also a great way to ease the nerves before jumpstarting a great thinking session.
  • During : Read the instructions for your exams. Sometimes a certain exam calls for long essay answers, or it could be a multiple choice type of exam. Follow the instructions, so that you don’t get penalized for giving the wrong answer.
  • After : Double checks your answers and identification. Be sure that your name and your identity number are written correctly to avoid any mishaps that could affect the outcome.

DON’Ts

  • Before : Don’t panic when you see that others look better prepared than you are. You must be confident with what you’ve learned and be ready to shine!
  • During : Don’t cheat. Not only is it a dishonest thing to do, but you also risk getting caught and punished for your action. Believe in your self, and you’ll feel good afterwards, because you know your great exam score is a proof of your hard work.
  • After : Don’t compulsively check your text book or notes to see whether you got it right or not. At this point, all that matters is that you did your best. Knowing that you gave the wrong answer to a problem might bring you down, when in fact, you probably did a great job!
READ MORE - Exam DO’s and DON’Ts

Cara Membuat Artikel Terkait dengan Disertai Thumbnails

3 Mei 2011

Dalam dunia per-blog-an ini, "related post" atau biasa kita sebut "artikel terkait" juga turut andil dalam perkembangan statistik blog kita. Dengan Artikel Terkait yang disertai gambar (thumbnails) tentu saja akan menarik pembaca untuk meng-klik dan melihat isi dari tulisan kita yang lain yang masih ada hubungannya dengan artikel yang mereka baca sebelumnya. Dan hasil akhir yang bisa kita dapatkan adalah page view blog kita jadi meningkat, orang akan betah untuk berlama-lama di blog kita. :-D


Cara Membuat Artikel Terkait dengan Disertai Thumbnails
1. Login ke Blogger.
2. Di halaman Dasbor, kita pilih Tata Letak.
3. Kemudian pilih Edit HTML
4. Beri tanda centang pada Expand Template Widget
5. Cari kode </head>
6. Taruh (copy paste) kode berikut ini di atasnya


<!--Related Posts with thumbnails Scripts and Styles Start-->
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<style type="text/css">
#related-posts {
float:center;
text-transform:none;
height:100%;
min-height:100%;
padding-top:5px;
padding-left:5px;
}

#related-posts h2{
font-size: 1.6em;
font-weight: bold;
color: black;
font-family: Georgia, &#8220;Times New Roman&#8221;, Times, serif;
margin-bottom: 0.75em;
margin-top: 0em;
padding-top: 0em;
}
#related-posts a{
color:black;
}
#related-posts a:hover{
color:black;
}

#related-posts  a:hover {
background-color:#d4eaf2;
}
</style>
<script src='http://blogergadgets.googlecode.com/files/relatedthumbs21.js' type='text/javascript'/>
</b:if>
<!--Related Posts with thumbnails Scripts and Styles End-->


7. Cari kode di bawah ini

<div class='post-footer-line post-footer-line-1'>

Atau jika tidak menemukannya cari kode berikut ini

<p class='post-footer-line post-footer-line-1'>

8. Jika sudah ketemu, taruh (copy paste) kode berikut ini di bawah salah satu kode di atas (no.7)

<!-- Related Posts with Thumbnails Code Start-->
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<div id='related-posts'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.isLast != &quot;true&quot;'>
</b:if>
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=related_results_labels_thumbs&amp;max-results=6&quot;' type='text/javascript'/></b:if></b:loop><a href='http://www.bloggerplugins.org/2009/08/related-posts-thumbnails-blogger-widget.html' style='display:none;'>Related Posts with thumbnails for blogger</a><a href='http://www.bloggerplugins.org/' style='display:none;'>blogger tutorials</a>
<script type='text/javascript'>
var currentposturl=&quot;<data:post.url/>&quot;;
var maxresults=5;
var relatedpoststitle="Related Posts";
removeRelatedDuplicates_thumbs();
printRelatedLabels_thumbs();
</script>
</div><div style='clear:both'/>
</b:if>
<!-- Related Posts with Thumbnails Code End-->


9. Kita juga dapat menganti beberapa kode css, seperti berikut ini
var maxresults=5;
Berarti artikel terkait yang akan kita tampilkan adalah 5
var relatedpoststitle="Related Posts";
Judulnya adalah Related Posts

10. Simpan Template jika sudah selesai.
11. Untuk preview hasil script di atas, bisa sobat lihat dibawah postingan ini.

Sumber : http://kurniasepta.blogspot.com/
READ MORE - Cara Membuat Artikel Terkait dengan Disertai Thumbnails

Best Friend or Foe?

So your best friend and you are inseparable, huh? You two always stick together like glue. Whatever your pal thinks and does, you’re sure to agree and tag along, and vice versa. After all, that’s what friends are for, riht? NO! Somewhere along the friendship road, if things look like they’re going the wrong way, you must be brave enough to draw the line and ask your self : Is this the kind of friendship that I want? Is it a healthy one? Here a some bits and bobs of what best friend should and shouldn’t do to each other.

Best friend respect and listen. 
A true best friend knows how to listen earnestly without changing the subject to focus on him/herself. He/she respects you and lets you express your thoughts without interrupting. A chatterbox, someone who talks a lot and often dominates the conversation, tends to give his/her best friend a hard time.

Friendship should be positive. 
Friendship makes you feel good, right? Ideally, it should also inspire you to do positive things. So next time you hear your best friend ask you to learn something new, say, how to make some handicrafts or join an extracurricular activity, be grateful to know that you have chosen the right best friend. Positive encouragement or praise from a best friend, even for the smallest achievement, will also strengthen the beautiful bond.


Friendship is all about trust. 
Spending a lot of times together and sharing so many things needs trust. There’s no way you can find the true meaning of friendship with someone who talks behind your back. What you need is someone who can make you feel safe, enjoy, and comfortable to be with. It means that whatever you say won’t be repeated to others without your consent.


Friendship shouldn’t rob you of your space and freedom. 
When your best friend keeps reminding you that both of you should always stick together and gives you a sour look when you do things on your own, beware! You have possesive pal. A healthy relationship should give you enough room to be on your own, doing the stuff that you like to do in private.
What to do : Tell your best friend that there are things you do best, and make you feel best, if you do it by yourself. Also say that the same thing probably applies to him/her too.


Friendship shouldn’t turn you into a pushover. 
“Let’s hang out at that mall again this afternoon. The other day I saw this cute cellphone case I really, really want”. You fully realize your sad financial. You know that if you say yes to your best friend’s invitation, you’ll spend your money, at least, on snacks and drink while wandering around at the mall. Yet, you just CAN’T say no. Even worse, you’ll end up buying a case as well just because your best friend keep nagging you to do the same!
What to do : Enough being this submissive best friend. Take stock of yourself and be brave to say no.


Friendship shouldn’t turn you into an indifferent person. 
You and your gang are motorbike enthusiasts. It feels awesome when all of you blast off on the road. You feel that way because you and your best friend have taken the silencers off your bikes or changed them with the racing type. Thundering like missile being shot into the sky, the bikes surely attract other people and provoke anger! There are things in friendship that can destroy empathy and sensitivity towards others. In the name of gang solidarity, many of us are willing to take part in selfish behavior like littering, smoking, and drawing graffiti in public facilities.
What to do : Listen to your heart and think twice before you and your gang offend or cause harm to others.
READ MORE - Best Friend or Foe?

The Gifts of Friendship

2 Mei 2011

Friendship is a gift in itself. Isn’t it nice to have someone who will there to share your good and bad times? It would also be nice to show wih a gift, once in a while, how much you appreciate your buddy. And what better time to give him/her a present than on his/her birthday? The real question is : what to give your pal? The following article might give you some ideas.

“It’s the thought that counts,“ goes a saying. Who says you have to give your mate an expensive gift? You do, however, have to put some thought into choosing the right thing. Pick something that your best friend can use instead of something that’s only pretty to look at.

Hand-make a gift.
Giving something that you make with your own hands is a good way of showing how much you appreciate your pal. It takes a little more and effort than just buying the present, but it can be cheaper and a lot more memorable. If you’re a handy-crafty person, you can scrape up a scrapbook (or a scrap-video) that contains your memorable scenes together and funny comments. String some beads and present a friendship bracelet to your bud. For those technologically inclined, desingning a web page for the friend is also an option.

Give your bud a treat.
Usually, the person who’s celebratin birthday pays for the celebration. Why not do it the other way around? With the others in your posse, chip in for a day out together. Eat out or rent a mini theater and watch your favorite movie together. Or how about this: spa day for the whole gang! It surely beats beauty care day all by yourself.

Give a tree/an animal to adopt.
Is your pal an enviromentally aware person? Maybe he/she would like to participate in minimizing global warming. Well then, you can give your buddy a tree to adopt! These days, a lot of organizations provide a program called Adopt-a-Tree. All you have to do is donate a reasonable amount of money to one of the organizations, and you’ll receive information about the tree’s growth over a period. For example, Green Radio 89,2 FM. And GEDEPAHALA Consortium have a just launched a program that allows everyone to adopt a tree at Mount Gede-Pangrango area for only Rp 3.000 a month over a period of three years.
If your buddy loves anmals, you can present him/her with a pet-a hamster, a rabbit, a fish, a puppy, or a kitten. 

Buy him/her something you like.
These common rule is to pick something your friend likes. How about choosing something you like? Go for something that you’d want for yourself-a book, a CD, a pair of shoes, or even a piece of batik material. It’s not being self-centered. Best friends usually have something in common, share the same interests, so chances are your buddy will like what you like.

Buy your chum what he/she needs.
Take a closer look at your best friend. Is your best friend’s wallet old and battered? Or maybe his T-Shirt is so faded that you feel uncomfortable to be seen hanging out with him? Maybe she needs a new flash disk (the old one having been wiped off by viruses).
There is always something your buddy needs which is within your budget. The trick is to find something unique, say a wallet that looks like a pair of jeans, or a T-Shirt that says “Hottie available”. You can also have an item customized, like having a cap embroidered, “The world’s best chum”, or a mug printed with your friend’s picture.
READ MORE - The Gifts of Friendship

IP

Terimakasih Sudah Berkunjung..
©
 
 
 

About Me

Foto Saya
Raditz Rastogi
Malang, Jawa Timur, Indonesia
Thanks for visiting my blog,.
Lihat profil lengkapku