JButton code example

Example 1: how to create a button in java

JButton button = new JButton();

Example 2: jbutton

btn.setFocusable(false);
btn.setBorderPainted(false);

Example 3: JButton

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Jbutton {

	public static void main(String[] args) {

		JFrame frame = new JFrame("GRProgram");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    int width = 400;										// Window Value Width 
	    int height = 300;										// Window Value Height								
	    frame.setSize(width,height);							// Window Dimension with Variables 
	    frame.setLayout(null);									//better Layout
	   	    
	    JButton button = new JButton();
	    String bText = "My Button in a long version";			// String value for Button
	    button.setText(bText);									// set String for Button Text
	    String tText = "Tool Tip text";							// String value for ToolTip on Button
	    button.setToolTipText(tText);							// set String for Button ToolTipText
	    Font schriftart=new Font("Arial",Font.BOLD,18);			// Define a Font, mayby Bold and size
	    button.setFont(schriftart);								// Set Font on Botton
	    Dimension size = button.getPreferredSize();				// Get prefered Size for Button on Text Size 
	    button.setBounds(width/5, height/5, size.width, size.height);	// Set Button Start Point (Left upper Corner) and Size on text Size
	    
	    frame.add(button);
	    frame.setVisible(true);
		
	}

}

Tags:

Java Example