How to create Java JButton swing component

How to create Java JButton Swing Component

Its pretty easy to create Java JButton swing component.All you need to do is first to import it “import javax.swing.JButton” then go ahead using available JButton constructors to create the class.

Java JButton  Constructors

Here are some of the commonly used JButton constructors.

  • JButton(String s)-creates a JButton with a text.
  • JButton()-creates empty JButton.
  • JButton(Icon icon)-creates a JButton with Icon.
  • JButton(String s,Icon icon)-creates a JButton with both icon and Strings.

Java JButton Methods

  • void setText(String s)-sets JButton text.
  • String getText()-returns JButton text.
  • void setEnabled(boolean )-enables of disables the JButton.
  • void addActionListener()-adds actionlistener to the button.

Creating  Java JButton Example

 

 Output

How to create Java JButton swing component

Adding  ActionListener to Java JButton

 

 Output:Adding  ActionListener to Java JButton

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com