Java swing JButton is an object that allows us to click on and doing something.
- JButton (): Create a button without text or icon
- JButton (Action a): Creates a Button whose properties are derived from an Action
- JButton (Icon icon): Creates a Button that specifies an icon
- JButton (String text): Create a Button that specifies text
- JButton (String text, Icon icon): Create a Button that specifies text and icons
* Create a Jbutton action using the JButton addActionListener method to do this. You can then create an ActionListener in the same way as the example above. Just write a new action and press Ctrl + Space. NetBean or Eclipse will show you the list of methods and interfaces. Select ActionListener (). It will automatically create an ActionPerformed () method for you. Then we call the changeTextJLabel () method to change the JLabel text when we click on the button. Note that since we created the ActionListener in addActionListener, we can not call JLabel's setText directly. If we want to call directly we need to call through the class name: MyJButton.lb.setText (), but we do not do it this way.
File name: JButtonAction.java continue
The output:
In this second example, we create a JLabel and 2 JButton placed in a GridLayour with 3 rows and 1 column. When you click on the "Green" or "Blue" button, the text and background of JLabel are changed.
Two ways to create and capture events in this example are completely different from the previous example. Here we implements the ActionListener interface for event listeners. When implements this interface, we need to override its actionPerfomed () method to perform event listeners. For this method is 1 ActionEvent e, this is the event that it heard.
To know exactly which button is pressed, we need to compare it with buttons. For btnGreen, it is a global variable of the class, so we can compare it using e.getSource () == btnGreen for testing, but with the Blue button it's not that, it's a JButton. Created in the createJButton () method, we want to test it against our text label with the e.getActionCommand () == "Blue" command.
Again, notice that the createJButton () method returns 1 JButton. The buttons created by this method should be addActionListener (this).
Eg: tbn.setMargin (new Insets (0, 0, 0)); The distance between the text and boder will be 0 (borderline).
More about how to use super keyword in java with detail example:
Example 1: Create a simple java swing JButton
Some points of note about java swing jbutton
* Initialize JButton: We have some JButton initialization methods- JButton (): Create a button without text or icon
- JButton (Action a): Creates a Button whose properties are derived from an Action
- JButton (Icon icon): Creates a Button that specifies an icon
- JButton (String text): Create a Button that specifies text
- JButton (String text, Icon icon): Create a Button that specifies text and icons
* Create a Jbutton action using the JButton addActionListener method to do this. You can then create an ActionListener in the same way as the example above. Just write a new action and press Ctrl + Space. NetBean or Eclipse will show you the list of methods and interfaces. Select ActionListener (). It will automatically create an ActionPerformed () method for you. Then we call the changeTextJLabel () method to change the JLabel text when we click on the button. Note that since we created the ActionListener in addActionListener, we can not call JLabel's setText directly. If we want to call directly we need to call through the class name: MyJButton.lb.setText (), but we do not do it this way.
Example 2: Methods for creating and capturing java swing JButton events
File name: JButtonAction.javaThe output:
In this second example, we create a JLabel and 2 JButton placed in a GridLayour with 3 rows and 1 column. When you click on the "Green" or "Blue" button, the text and background of JLabel are changed.
Two ways to create and capture events in this example are completely different from the previous example. Here we implements the ActionListener interface for event listeners. When implements this interface, we need to override its actionPerfomed () method to perform event listeners. For this method is 1 ActionEvent e, this is the event that it heard.
To know exactly which button is pressed, we need to compare it with buttons. For btnGreen, it is a global variable of the class, so we can compare it using e.getSource () == btnGreen for testing, but with the Blue button it's not that, it's a JButton. Created in the createJButton () method, we want to test it against our text label with the e.getActionCommand () == "Blue" command.
Again, notice that the createJButton () method returns 1 JButton. The buttons created by this method should be addActionListener (this).
Set Margin for java swing JButton:
If Jbutton's text is too long for its size, you can set the distance between the border of the button and the text using the following method:Eg: tbn.setMargin (new Insets (0, 0, 0)); The distance between the text and boder will be 0 (borderline).
More about how to use super keyword in java with detail example:
Comments
Post a Comment