کنترل JButton  جایگزینی برای کنترل Button از مجموعه کنترل های AWT است. JButton در نگارش فعلی از متن ، آیکون ، عکس و تگهای HTML پشتیبانی می کند, همچنین میتوان این کامپوننت را به 5 روش زیر ایجاد می گردد:

  • public  JButton()

JButton button = new JButton();

 

public  JButton(Icon image)

Icon icon = new ImageIcon("Test.gif");

JButton button = new JButton(icon);

 

pubic  JButton (String text)

JButton button = new JButton("TEst Button") ;

 

public  JButton(String text , Icon icon )

Icon icon = new ImageIcon("Text.gif") ;

JButton button = new JButton("Test Button" , icon);

 

public  JButton(Action action)

Action action = ….. ;

JButton button = new JButton(action) ;

تصویر زیر مثالهایی از نحوه استفاده از این کامپوننت را نشان میدهد:

jButton1 = new javax.swing.JButton();

jButton1.setText("Text Only");

jButton1.setMnemonic('T');

 

 jButton2 = new javax.swing.JButton();

jButton2.setIcon(new javax.swing.ImageIcon("Ambulance.gif");

 

jButton3 = new javax.swing.JButton();

jButton3.setIcon(new javax.swing.ImageIcon("Patient Man 1.gif");

jButton3.setText("Text & Image");

 

jButton4 = new javax.swing.JButton();

jButton4.setText("<html><FONT COLOR=RED>HTML</FONT><b><i>JButton</html>");

رویدادهای JButton

یکی از مهمترین رویدادهای و یا Listener های JButton رویداد ActionListener است. این رویداد که در حقیقت همان رویداد OnClick مربوط به Button در سایر زبانهای برنامه نویسی است ، زمانی که JButton کلیک می شود فراخوانی گشته و یک Object از نوع ActionEvent به Listener مربوطه ارسال می گردد.

در جاوا این قابلیت وجود دارد که از یک Listener برای JButton های مختلف استفاده کرد. برای این کار باید از خاصیت actionCommand استفاده کرد. پارامتر ورودی این  خاصیت  یک مقدار رشته ای است که باید با استفاده از متد ()getActionCommand در Listener مورد نظر استفاده شود . برای آشنایی بیشتر با خاصیت actionCommand به مثال زیر توجه کنید.

در این مثال از 4 JButton استفاده شده است . که خاصیت ActionCommand آنها با استفاده از متد ()setActionCommand برابر با شماره JButton تعریف شده است.

در ابتدای این کد ابتدا یک Listener جدید از نوع ActionListener تعریف شده (1),

و سپس متد actionPerformed با پارامتر actiopEvent ایجاد گردید است (2).

در متد ()actionPerformed با استفاده از getActionCommand نام دستوری که در هنگام فشار دادن JButton ارسال می شود، گرفته شده و در یک JLabel نشان داده می شود و مشخص می شود که کدام JButton فشار داده شده است (3).

در این مثال برای تمام JButton ها از یک Listener استفاده شده است . به این نکته توجه داشته باشید که خاصیت ActionCommand بصورت پیش فرض با متن JButton مقدار دهی می شود . کد زیر نحوه انجام این کار را نشان می دهد.

public class MainJFrame extends javax.swing.JFrame {  

    /** Creates new form MainJFrame */

    public MainJFrame() {

        initComponents();

    }

     private void initComponents() {

         ActionListener actionListener = new ActionListener(){  (1)

                 public void actionPerformed(ActionEvent evt){  (2)

                          String command = evt.getActionCommand();  (3)

                          jLabel1.setText("You are selected : " + command);  (3)

                 }

         };

         jButton1 = new javax.swing.JButton();

         jButton1.setText("First");

         jButton1.setActionCommand("First");

         jButton1.addActionListener(actionListener);

 

         jButton2 = new javax.swing.JButton();

         jButton2.setText("Second");

         jButton2.setActionCommand("Second");

         jButton2.addActionListener(actionListener);

 

         jButton3 = new javax.swing.JButton();

         jButton3.setText("Third");

         jButton3.setActionCommand("Third");

         jButton3.addActionListener(actionListener);

         jButton4 = new javax.swing.JButton();

         jButton4.setText("Fourth");

         jButton4.setActionCommand("Fourth");

         jButton4.addActionListener(actionListener);

 

         jLabel1 = new javax.swing.JLabel();

         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));

         jLabel1.setText("You are Selected : ");

 

         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

}

 

در NetBeans IDE قسمت InitComponents به صورت Read-Only است یعنی نمی توان اطلاعات آنرا تغییر داد، برای اضافه کردن کدهای فوق باید بصورت زیر عمل کرد :

1- به علت اینکه متغیر actionListener از نوع کلاس ActionListener بوده و یک متغیر عمومی برای تمام JButton ها است باید کد آن قبل از تمام JButton ها اضافه شود(مراحل 1و 2 و 3 از کد فوق).

برای این کار اولین JButton و یا اولین کنترل را که در این مثال jButton1 است، انتخاب کرده و از پنجره Properties قسمت کد را انتخاب کنید. بر روی گزینه Pre-Creation Code کلیک کرده تا پنجره مربوط به آن اضافه شود، حال مراحل 1 و 2 و 3 از کد فوق را در درون این پنجره بنویسید.

2- حال بر روی گزینه Post-Creation Code  کلیک کرده تا پنجره مربوط به آن باز شود و کدهای زیر را برای JButton1 بنویسید:

jButton1.setActionCommand("First");

jButton1.addActionListener(actionListener);

با این کار در قسمت InitComponents کدهای فوق بعد از  ایجاد JButton1 اضافه می شود.

3- مرحله 2 را برای JButton های دیگر نیز انجام دهید.