12月24日Java作业

Shen Ying Lv6

作业要求

如图:

截屏2024-12-24 18.55.47

第一题

截屏2024-12-24 18.58.05

代码

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
package homeWork;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

public class Test1 extends JFrame {
JTextField t1 = new JTextField("3", 5);
JTextField t2 = new JTextField("9", 5);
JTextField t3 = new JTextField("27", 5);
JButton cal = new JButton("计算");
Test1() {
super("作业1");
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(t1);
con.add(cal);
cal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Integer n1 = Integer.parseInt(t1.getText());
Integer n2 = Integer.parseInt(t2.getText());
Integer mul = n1 * n2;
t3.setText(mul.toString());
}
});
con.add(t2);
con.add(t3);

// 窗口设置
setVisible(true);
setSize(400, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test1 f = new Test1();
}
}

第二题

代码

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
package homeWork;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test2 extends JFrame {
JTextField t1 = new JTextField("3", 5);
JLabel l = new JLabel("xx欢迎您");
JButton cal = new JButton("确定");
Test2() {
super("作业2");
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(t1);
con.add(cal);
con.add(l);
cal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
l.setText(t1.getText() + "欢迎您");
}
});

// 窗口设置
setVisible(true);
setSize(300, 80);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test2 f = new Test2();
}
}

第三题

截屏2024-12-24 18.58.56

代码

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package homeWork;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Test3 extends JFrame {
// 注册组件
JPanel j1 = new JPanel();
JLabel name = new JLabel("请输入姓名:");
JTextField t1 = new JTextField("小雪", 10);

JPanel j2 = new JPanel();
JLabel sex = new JLabel("性别:");
JRadioButton man = new JRadioButton("男");
JRadioButton woman = new JRadioButton("女");
ButtonGroup g = new ButtonGroup();

JPanel j3 = new JPanel();
JLabel hobby = new JLabel("爱好:");
JCheckBox book = new JCheckBox("读书");
JCheckBox pe = new JCheckBox("体育");
JCheckBox music = new JCheckBox("音乐");

JPanel j4 = new JPanel();
JLabel info1 = new JLabel("");
JLabel info2 = new JLabel("");
JLabel info3 = new JLabel("");
String st1 = "";
String st2 = "";
String st3 = "";
Test3() {
super("作业3");
Container con = getContentPane();
con.setLayout(new GridLayout(4, 1));
// 添加panel盒子
con.add(j1);
con.add(j2);
con.add(j3);
con.add(j4);

// 单选框添加进按钮组
g.add(man);
g.add(woman);

// 添加组件
j1.add(name);
j1.add(t1);

j2.add(sex);
j2.add(man);
j2.add(woman);

j3.add(hobby);
j3.add(book);
j3.add(pe);
j3.add(music);

j4.add(info1);
j4.add(info2);
j4.add(info3);

// 添加事件
t1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
info3.setText("姓名: " + t1.getText());
}
});

// 多选框事件
ItemListener item_listener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
JCheckBox source = (JCheckBox)e.getSource();
if (source == book) {
if (source.isSelected()) st1 = "书籍,";
else st1 = "";
} else if (source == pe) {
if (source.isSelected()) st2 = "体育,";
else st2 = "";
} else if (source == music) {
if (source.isSelected()) st3 = "音乐";
else st3 = "";
}
info2.setText("爱好:"+st1+st2+st3);
}
};
book.addItemListener(item_listener);
pe.addItemListener(item_listener);
music.addItemListener(item_listener);
// 性别事件
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JRadioButton source = (JRadioButton)e.getSource();
if (source == man) {
info1.setText("性别:男");
} else if (source == woman) {
info1.setText("性别:女");
}
}
};
man.addActionListener(listener);
woman.addActionListener(listener);
// 窗口设置
setVisible(true);
setSize(340, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test3 f = new Test3();
}
}

第四题

截屏2024-12-24 18.59.14

代码

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
package homeWork;

import javax.swing.*;
import java.awt.*;

public class Test4 extends JFrame {
Test4() {
super("作业四");
Container con = getContentPane();
con.setLayout(new GridLayout(3, 1));

JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
con.add(p1);
con.add(p2);
con.add(p3);

p1.add(new JLabel("请选择一个容器类:"));
p2.add(new JRadioButton("JFrame"));
p2.add(new JRadioButton("JDialog"));
p2.add(new JRadioButton("JApplet"));

p3.add(new JButton("OK"));
p3.add(new JButton("Cancel"));
// 设置
setVisible(true);
setSize(300, 120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test4 f = new Test4();
}
}

第五题

截屏2024-12-24 18.59.26

代码

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
package homeWork;

import javax.swing.*;
import java.awt.*;

public class Test5 extends JFrame {
Test5() {
super("作业5");
Container con = getContentPane();
con.setLayout(new GridLayout(5, 1));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
p1.setLayout(new CardLayout());
p2.setLayout(new CardLayout());
p3.setLayout(new CardLayout());
p4.setLayout(new CardLayout());

con.add(p1);
con.add(p2);
con.add(p3);
con.add(p4);
con.add(p5);


p1.add(new JLabel("请输入书籍名称:"));
p2.add(new JTextField(10));
p3.add(new JLabel("请输入书籍的详细描述:"));
p4.add(new JTextArea(10, 5));
p5.add(new JButton("OK"));
p5.add(new JButton("Cancel"));
// 设置
setVisible(true);
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test5 f = new Test5();
}
}
  • 标题: 12月24日Java作业
  • 作者: Shen Ying
  • 创建于 : 2024-12-24 18:52:14
  • 更新于 : 2024-12-27 10:54:20
  • 链接: https://shenying.online/2024/12/24/12月24日Java作业/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论