布局练习:
一.
public class PracticePanelDemo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(500,500,400,300);
frame.setVisible(true);
}
}
二.首先分上下布局
package lesson01;
import java.awt.*;
public class PracticePanelDemo {
public static void main(String[] args) {
//总的Frame
Frame frame = new Frame();
frame.setBounds(500,500,400,300);
frame.setVisible(true);
//分为上下结构
frame.setLayout(new GridLayout(2,1));
Panel panel01 = new Panel();
}
}
三.先搞定上部分
panel01:
panel02:
import java.awt.*;
public class PracticePanelDemo {
public static void main(String[] args) {
//总的Frame
Frame frame = new Frame();
frame.setBounds(500,500,400,300);
frame.setVisible(true);
//分为上下结构
frame.setLayout(new GridLayout(2,1));
//四个面板 先设计面板 再将面板加入窗口中
//东西南北中布局
Panel panel01 = new Panel(new BorderLayout());
//表格布局
Panel panel02 = new Panel(new GridLayout(2,1));
Panel panel03 = new Panel(new BorderLayout());
Panel panel04 = new Panel(new GridLayout(2,2));
//给面板添加组件、
panel01.add(new Button("east"),BorderLayout.EAST);
panel01.add(new Button("west"),BorderLayout.WEST);
panel02.add(new Button("p2-1"));
panel02.add(new Button("p2-2"));
//上面ok
panel01.add(panel02,BorderLayout.CENTER);
frame.add(panel01);
}
}
四.接着搞定下面部分
package lesson01;
import java.awt.*;
public class PracticePanelDemo {
public static void main(String[] args) {
//总的Frame
Frame frame = new Frame();
frame.setBounds(500,500,400,300);
frame.setVisible(true);
//分为上下结构
frame.setLayout(new GridLayout(2,1));
//四个面板 先设计面板 再将面板加入窗口中
//东西南北中布局
Panel panel01 = new Panel(new BorderLayout());
//表格布局
Panel panel02 = new Panel(new GridLayout(2,1));
Panel panel03 = new Panel(new BorderLayout());
Panel panel04 = new Panel(new GridLayout(2,2));
//给面板添加组件、
panel01.add(new Button("east-1"),BorderLayout.EAST);
panel01.add(new Button("west-1"),BorderLayout.WEST);
panel02.add(new Button("p2-1"));
panel02.add(new Button("p2-2"));
//上面ok
panel01.add(panel02,BorderLayout.CENTER);
frame.add(panel01);
//同理搞定下面
panel03.add(new Button("east-2"),BorderLayout.EAST);
panel03.add(new Button("west-2"),BorderLayout.WEST);
//利用for循环解决下面四个组件
for (int i = 1; i <= 4; i++) {
panel04.add(new Button("p4-"+i));
}
panel03.add(panel04,BorderLayout.CENTER);
//下面ok
frame.add(panel03);
}
}
五.再加个鼠标监听事件,布局搞定
package lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class PracticePanelDemo {
public static void main(String[] args) {
//总的Frame
Frame frame = new Frame();
frame.setBounds(500,500,400,300);
frame.setVisible(true);
//分为上下结构
frame.setLayout(new GridLayout(2,1));
//四个面板 先设计面板 再将面板加入窗口中
//东西南北中布局
Panel panel01 = new Panel(new BorderLayout());
//表格布局
Panel panel02 = new Panel(new GridLayout(2,1));
Panel panel03 = new Panel(new BorderLayout());
Panel panel04 = new Panel(new GridLayout(2,2));
//给面板添加组件、
panel01.add(new Button("east-1"),BorderLayout.EAST);
panel01.add(new Button("west-1"),BorderLayout.WEST);
panel02.add(new Button("p2-1"));
panel02.add(new Button("p2-2"));
//上面ok
panel01.add(panel02,BorderLayout.CENTER);
frame.add(panel01);
//同理搞定下面
panel03.add(new Button("east-2"),BorderLayout.EAST);
panel03.add(new Button("west-2"),BorderLayout.WEST);
//利用for循环解决下面四个组件
for (int i = 1; i <= 4; i++) {
panel04.add(new Button("p4-"+i));
}
panel03.add(panel04,BorderLayout.CENTER);
//下面ok
frame.add(panel03);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}