JavaGUI编程

虽说java在做pc应用并不是很有市场,我那时学的也是VS下用WPF做的,有了WPF那些基础,之后的Android也更容易入手,还是有些帮助的。

GUI编程

组件和容器

Frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 =new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame2 =new MyFrame(200,100,200,200,Color.yellow);
MyFrame myFrame3 =new MyFrame(300,100,200,200,Color.red);
MyFrame myFrame4 =new MyFrame(400,100,200,200,Color.gray);
}
}
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);
setBounds(x,y,w,h);
setVisible(true);
}
}

Panel

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
public class TestPanel {
public static void main(String[] args) {
Frame frame=new Frame();
Panel panel=new Panel();

frame.setLayout(null);

frame.setBounds(300,300,500,500);
frame.setBackground(new Color(40,160,50));

panel.setBounds(50,50,400,400);
panel.setBackground(new Color(90,20,150));

frame.add(panel);

frame.setVisible(true);

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

布局管理器

流式布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();

Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");

frame.setLayout(new FlowLayout(FlowLayout.LEFT));

frame.setSize(200,200);
frame.add(button1);java
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}

边界布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();

Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");

frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);

frame.setSize(500,500);

frame.setVisible(true);
}
}

网格布局

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
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("GridLayout");

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.setLayout(new GridLayout(3,2));

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

frame.pack();

frame.setVisible(true);
}
}

事件监听

单个按钮

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
public class TestActionEvent {
public static void main(String[] args) {
Frame frame=new Frame();
Button button=new Button();
MyActionListener myActionListener=new MyActionListener();
button.addActionListener(myActionListener);

frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

windowClosing(frame);

}
private static void windowClosing(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("as");
}
}

多个按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class TestAction2 {
public static void main(String[] args) {
Frame frame=new Frame("开始-停止");
Button start=new Button("start");
Button stop=new Button("stop");
start.setActionCommand("start");
stop.setActionCommand("stop");
MyMonitor myMonitor=new MyMonitor();
start.addActionListener(myMonitor);
stop.addActionListener(myMonitor);


frame.add(start,BorderLayout.NORTH);
frame.add(stop,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("点击,msg=>"+e.getActionCommand());
}
}

文本框

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
public class TestText0 {
public static void main(String[] args) {
new MyFrame2();
}
}

class MyFrame2 extends Frame {
public MyFrame2() {
TextField textField = new TextField();
add(textField);

MyActionListener2 myActionListener = new MyActionListener2();
textField.addActionListener(myActionListener);

//设置替换编码
textField.setEchoChar('*');

pack();
setVisible(true);
}
}

class MyActionListener2 implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();
System.out.println(textField.getText());
textField.setText("");
}
}

简易计算器

组合

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
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}

class Calculator extends Frame {
TextField num1,num2,num3;

public void loadFrame(){
num1 = new TextField("10");
num2 = new TextField("10");
num3 = new TextField("20");

Button btn = new Button("=");
Label label = new Label("+");

btn.addActionListener(new MyCalcActionListener(this));

setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
pack();
setVisible(true);
}
}

class MyCalcActionListener implements ActionListener {
Calculator calculator=null;

public MyCalcActionListener(Calculator calculator) {
this.calculator=calculator;
}

@Override
public void actionPerformed(ActionEvent e) {
int n1= Integer.parseInt(calculator.num1.getText());
int n2= Integer.parseInt(calculator.num2.getText());

calculator.num3.setText(""+(n1+n2));

calculator.num1.setText("");
calculator.num2.setText("");
}
}

内部类

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
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}

class Calculator extends Frame {
TextField num1,num2,num3;

public void loadFrame(){
num1 = new TextField("10");
num2 = new TextField("10");
num3 = new TextField("20");

Button btn = new Button("=");
Label label = new Label("+");

btn.addActionListener(new MyCalcActionListener());

setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
pack();
setVisible(true);
}
public class MyCalcActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int n1= Integer.parseInt(num1.getText());
int n2= Integer.parseInt(num2.getText());

num3.setText(""+(n1+n2));

num1.setText("");
num2.setText("");
}
}
}

画笔Paint

鼠标监听

窗口监听

键盘监听

Swing

参考

https://www.bilibili.com/video/BV1DJ411B75F

总结

前面很多都没去总结,我是觉得这方面了解就好,不想深入钻研。