java jbutton
时间: 2023-10-14 19:28:57 浏览: 41
A JButton in Java is a graphical user interface component that represents a clickable button on a Java Swing application. It can perform an action when clicked by the user. JButton can display text, images, or both.
Here's an example code to create a JButton:
```
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyButton {
public static void main(String[] args) {
JFrame frame = new JFrame("JButton Example");
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Click me!");
panel.add(button);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
```
In this code, we create a JFrame and a JPanel to hold our JButton. We then create a JButton and add it to the JPanel. Finally, we set the size of the JFrame and make it visible. When the user clicks the button, nothing happens yet. You can add an ActionListener to the button to perform an action when it's clicked.