抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Frame

创建第一个Frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

package com.ajream.lesson1;

import java.awt.*;

public class TestFrame {
public static void main(String[] args) {

/*窗口标题*/
Frame frame = new Frame("Title");

frame.setSize(400, 400);
frame.setVisible(true);
frame.setBackground(new Color(114, 72, 173));
frame.setLocation(200,200);

frame.setResizable(false); /*使大小固定*/
}
}


将Frame的创建打包成类使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

package com.ajream.lesson1;

import java.awt.*;

public class TestFrame2 {
public static void main(String[] args) {
MyFrame frame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame frame2 = new MyFrame(300, 100, 200, 200, Color.GREEN);
MyFrame frame3 = new MyFrame(100, 300, 200, 200, Color.YELLOW);
MyFrame frame4 = new MyFrame(300, 300, 200, 200, Color.PINK);
}
}

//封装成类
class MyFrame extends Frame{
static int id = 0;

public MyFrame(int x, int y, int w, int h, Color color){
super("Myframe" + (++id));
setBackground(color);
setSize(w, h);
setLocation(x, y);
setVisible(true);
}
}


image-20210918192418585

面板 panel

panel需要放在Frame上

1
2
3
Frame frame = new Frame();
Panel panel = new Panel();
frame.add(panel);

监听事件

1
2
3
4
5
6
7
8
9

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//super.windowClosing(e);
System.exit(0);
}

});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.ajream.lesson1;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class TestPanel {

public static void main(String[] args) {

Frame frame = new Frame();

// 布局的概念
Panel panel = new Panel();

// 设置布局
frame.setLayout(null);

// frame坐标
frame.setBounds(400, 500, 400, 400);
frame.setBackground(Color.BLUE);

// 设置panel坐标,相对于frame
panel.setBounds(50, 50, 300, 300);
panel.setBackground(Color.GREEN);

frame.add(panel);

frame.setVisible(true);

// 监听事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// super.windowClosing(e);
System.exit(0);
}

});

}



}

image-20210918192224269

布局管理器

流式布局(默认使用)-FlowLayout

使用布局(自动按照规则排列,如果是 FlowLayout.LEFT 则从左向右排)

1
2
//new FlowLayout() 参数有 FlowLayout.LEFT。 FlowLayout.CENTER, RIGHT...
frame.setLayout(new FlowLayout(FlowLayout.LEFT));

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.ajream.lesson1;

import java.awt.*;

public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();

frame.setBounds(400, 500, 500, 500);
frame.setVisible(true);
frame.setBackground(Color.magenta);

// 流式布局
// new FlowLayout() 参数有 FlowLayout.LEFT。 FlowLayout.CENTER, RIGHT...
frame.setLayout(new FlowLayout(FlowLayout.LEFT));

Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");

frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
}
}

image-20210918200305079

东西南北中-BorderLayout

image-20210919162847370

布局使用

1
2
3
4
5
6
7
8
9
10
11
12
13
frame.setLayout(new BorderLayout());

Button btn1 = new Button("东");
Button btn2 = new Button("西");
Button btn3 = new Button("南");
Button btn4 = new Button("北");
Button btn5 = new Button("中");

frame.add(btn1, BorderLayout.EAST);
frame.add(btn2, BorderLayout.WEST);
frame.add(btn3, BorderLayout.SOUTH);
frame.add(btn4, BorderLayout.NORTH);
frame.add(btn5, BorderLayout.CENTER);

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.ajream.lesson1;

import java.awt.*;

public class TestBoderLayout {
public static void main(String[] args) {
Frame frame = new Frame();

frame.setBounds(400, 500, 500, 500);
frame.setVisible(true);
frame.setBackground(Color.magenta);

// 东西南北中布局(贴着边界摆放组件)
frame.setLayout(new BorderLayout());

Button btn1 = new Button("东");
Button btn2 = new Button("西");
Button btn3 = new Button("南");
Button btn4 = new Button("北");
Button btn5 = new Button("中");

frame.add(btn1, BorderLayout.EAST);
frame.add(btn2, BorderLayout.WEST);
frame.add(btn3, BorderLayout.SOUTH);
frame.add(btn4, BorderLayout.NORTH);
frame.add(btn5, BorderLayout.CENTER);
}
}

image-20210918201011515

表格布局-GridLayout

布局使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//表格布局(2行3列)
frame.setLayout(new GridLayout(2,3));

Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");

frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.ajream.lesson1;

import java.awt.*;

public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();

frame.setBounds(400, 500, 500, 500);
frame.setVisible(true);
frame.setBackground(Color.magenta);

// 表格布局(2行3列)
frame.setLayout(new GridLayout(2,3));

Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");

frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
}
}

image-20210918201536196

练习

做一个类似下面的界面

image-20210918195042885

构思:在Frame上添加4个panel

image-20210918202434569

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.ajream.lesson1;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ExerciseLayout {

public static void main(String[] args) {
Frame frame = new Frame();
frame.setBackground(Color.BLUE);
frame.setVisible(true);
frame.setBounds(300, 300, 500, 500);
frame.setLayout(new GridLayout(2,1));

Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();


p3.setLayout(new GridLayout(2,1));
p3.add(new Button("p3_btn1"));
p3.add(new Button("p3_btn2"));
p1.setLayout(new BorderLayout());
p1.add(new Button("p1_btn1"), BorderLayout.WEST);
p1.add(new Button("p1_btn2"), BorderLayout.EAST);
p1.add(p3, BorderLayout.CENTER);

p4.setLayout(new GridLayout(2,2));
for (int i = 0; i < 4; i++) {
p4.add(new Button("p4_btn"+(i+1)));
}
p2.setLayout(new BorderLayout());
p2.add(new Button("p2_btn1"),BorderLayout.WEST);
p2.add(new Button("p2_btn2"),BorderLayout.EAST);
p2.add(p4,BorderLayout.CENTER);

// 把p1, p2添加到frame
frame.add(p1);
frame.add(p2);

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}


}

image-20210918203610240

事件监听

事件:实现了 ActionListener接口的类

ActionListener只有一个方法:void actionPerformed

1
2
3
4
5
6
class MyEvent implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("点击了btn");
}
}

参数e表示某个组件触发的事件

1
2
3
4
5
6
class MyEvent implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("msg => " + e.getActionCommand());
}
}

监听事件

1
button.addActionListener(new MyEvent());

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ajream.lesson2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(200, 300, 300, 300);

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});


Button button1 = new Button("btn1");
Button button2 = new Button("btn2");
button1.setActionCommand("点击了btn1");
button2.setActionCommand("点击了btn2");

MyEvent myEvent = new MyEvent();
button1.addActionListener(myEvent);
button2.addActionListener(myEvent);

frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);

}
}

class MyEvent implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("msg ==> " + e.getActionCommand());
}
}

image-20210918212904619

评论