创建窗体JFrame的一个对象并添加多个按钮
时间: 2024-01-21 16:03:03 浏览: 25
好的,以下是一个创建JFrame对象并添加多个按钮的示例代码:
```java
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyFrame extends JFrame {
public MyFrame() {
setTitle("My JFrame Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
// 创建多个按钮
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
// 将按钮添加到窗体中
add(button1);
add(button2);
add(button3);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
```
上述代码创建了一个名为"My JFrame Example"的JFrame对象,并添加了三个按钮。最终效果如下图所示:
![JFrame with Buttons](https://i.imgur.com/ExlJf2B.png)