jbutton in java
JButton is a Swing component that represents a button on a graphical user interface (GUI) in Java. It is a simple component to add to a JPanel, JFrame, or other Swing container, and can be used to perform actions when clicked by the user.
Here's an example of how to create a JButton in Java:
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
// Create a new JButton object with the label "Click Me"
JButton button = new JButton("Click Me");
// Add an ActionListener to the JButton to perform an action when it is clicked
button.addActionListener(e -> {
System.out.println("Button was clicked");
});
// Add the JButton to the JFrame
frame.add(button);
// Set up the JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
In this example, we create a new JButton object with the label "Click Me". We then add an ActionListener to the JButton to perform an action when it is clicked. Finally, we add the JButton to a JFrame and set up the basic attributes of the JFrame.
When the user clicks the JButton, the ActionListener will be triggered and print a message to the console. You can replace this with any action you like, such as opening a new window or performing a database query.
