java学习10.17
今天继续Java图形化页面的学习
窗口的分别显示
import java.awt.;
import java.awt.event.;
public class _1016 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(500, 500, 300, 300);
frame.setAlwaysOnTop(true);
// 设置 GridLayout
GridLayout gridLayout = new GridLayout(2, 3); // 2行3列
frame.setLayout(gridLayout);
Panel topPanel = new Panel();
topPanel.setBackground(Color.ORANGE);
topPanel.setLayout(new GridLayout(2, 3)); // 2行3列
for (int i = 0; i < 6; i++) {
topPanel.add(new Button(i + ". button"));
}
frame.add(topPanel);
Panel bottomPanel = new Panel();
bottomPanel.setBackground(Color.PINK);
bottomPanel.setLayout(new FlowLayout());
for (int i = 0; i < 5; i++) {
bottomPanel.add(new Button(i + ". button"));
}
frame.add(bottomPanel);
frame.setVisible(true);
// 添加窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose(); // 关闭主窗口
openSecondWindow(); // 打开第二个窗口
}
});
}
// 打开第二个窗口的函数
private static void openSecondWindow() {
Frame frame2 = new Frame();
frame2.setBounds(600, 600, 300, 300); // 修改位置以避免重叠
frame2.setAlwaysOnTop(true);
frame2.setVisible(true);
// 添加关闭事件
frame2.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame2.dispose(); // 关闭第二个窗口
}
});
}
}