swing程序 要求出现东南西北四个按钮
时间: 2024-03-17 13:47:49 浏览: 15
好的,我可以为您提供一个简单的Swing程序,其中包含东南西北四个按钮。以下是示例代码:
```java
import javax.swing.*;
import java.awt.*;
public class DirectionButtons extends JFrame {
public DirectionButtons() {
// 设置窗口标题
setTitle("Direction Buttons");
// 创建东南西北四个按钮
JButton northButton = new JButton("North");
JButton southButton = new JButton("South");
JButton eastButton = new JButton("East");
JButton westButton = new JButton("West");
// 将按钮添加到内容面板上
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(northButton, BorderLayout.NORTH);
contentPane.add(southButton, BorderLayout.SOUTH);
contentPane.add(eastButton, BorderLayout.EAST);
contentPane.add(westButton, BorderLayout.WEST);
// 设置窗口大小并显示
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new DirectionButtons();
}
}
```
运行程序后,您将看到一个包含东南西北四个按钮的窗口。希望这可以回答您的问题。