[JavaSwing] JButton

Content
Create simple JButton
Ways to Action
Đặt margin

JButton is an object that allows us to click on will do something.
JButton
Example button “Compute” below, when clicking on it will calculate interest based on the information entered in the JTextField

Example 1: Create a simple JButton

package nguyenvanquan7826.JButton;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class MyJButton extends JFrame {
	private JLabel lb;

	public MyJButton() {
		// create JFrame
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 200);
		setLayout(new GridLayout(2, 1, 5, 5));

		// create JLabel
		lb = new JLabel("My JLabel");
		lb.setHorizontalAlignment(JLabel.CENTER);
		add(lb);

		// create JButton with text "Click Button"
		JButton btn = new JButton("Click Button");
		// add action
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				changeTextJLabel();

			}
		});
		add(btn);

		// display JFrame
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// change text of lb
	private void changeTextJLabel() {
		lb.setText("You are clicked JButton");
	}

	public static void main(String[] args) {
		new MyJButton();
	}
}

JButton
Some of the note
* Initialize JButton: we have some initialization method JButton
– JButton() : Create a button or icon without text
– JButton(Action a) : Create a Button with properties taken from an Action
– JButton(Icon icon) : Create a Button specify an icon
– JButton(String text) : Create a Button specified text
– JButton(String text, Icon icon) : Create a Button specified text and icon
* Create action for JButton I use the method of JButton addActionListener to do this. Then you can create a new 1 ActionListener within which the example above. You just need to write new Action then press Ctrl + Space (from) NetBean or Eclipse will show you a list of methods and interfaces, you choose ActionListener() it will automatically create methods ActionPerformed() for you. Then we call the method changeTextJLabel() to change the text of JLabel when I click on the button. Note that because we create new ones right in addActionListener ActionListener should not be called directly by JLabel setText. If you want to call to call us directly through the class name: MyJButton.lb.setText(), however little we do it this way.

Example 2: The way to create and capture events JButton

package nguyenvanquan7826.JButton;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class JButtonAction extends JFrame implements ActionListener {
	private JButton btnGreen;
	private JLabel lb;

	public JButtonAction() {
		// create JFrame
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(200, 300);
		setLayout(new GridLayout(3, 1, 5, 5));

		// add JFrame content
		lb = new JLabel("Background default");
		lb.setOpaque(true);
		add(lb);

		btnGreen = createJButton("Green");
		add(btnGreen);
		add(createJButton("Blue"));

		// display JFrame
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create JButton with text is title
	private JButton createJButton(String title) {
		JButton btn = new JButton(title);
		// add action for JButton
		btn.addActionListener(this);
		return btn;
	}

	// change text and background of JLabel when click button
	private void changeBackgroundJLabel(Color bgcolor, String nameBgcolor) {
		lb.setBackground(bgcolor);
		lb.setText("Background is " + nameBgcolor);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// click button green
		if (e.getSource() == btnGreen) {
			changeBackgroundJLabel(Color.green, "Green");
		}
		// click button blue
		if (e.getActionCommand() == "Blue") {
			changeBackgroundJLabel(Color.blue, "Blue");
		}
	}

	public static void main(String[] args) {
		new JButtonAction();
	}
}

action button

In the example 2 we create a JLabel and 2 JButton is placed in a GriLayour with 3 goods and 1 post. When you click on the button “Green” or “Blue” the text and background of the JLabel be changed.

2 how to create and capture events in this example is completely different than the previous example. Here we implements ActionListener Interface to serve the listening event. When implements this interface, we need to Override methods actionPerfomed() of it to make the event listener. For this method is 1 ActionEvent and, this is the event that it is listening.
To know exactly what button is pressed, then we need to compare it with the button. For btnGreen, it is 1 variable “Global” of class so we can compare by using e.getSource() == BtnGreen to check, however the Blue button, it's not that it is a method JButton created in createJButton() should want to test it to compare with its text label with the command e.getActionCommand() == “Blue”.

You pay attention to the methods createJButton() it returns 1 JButton. The button is created by this method should be addActionListener(this). Because we have so implements ActionListener Interface methods have to be this ie the impact on the ActionListener that button so that the event can be heard.

Put Magin for JButton:

If the text of JButton slightly too long compared to the size of it, you can set the distance between the edge of the button to the text using the method:

setMargin(new Insets(int bottom, int left, int right, int top));

CEO: btn.setMargin(new Insets(0, 0, 0, 0)); text will be from the edge 0 (marginal).

Refer to: Class JButton, Use Button