Java
1.11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.ArrayList;
public class hellp {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> numbers = new ArrayList<>(10);
// 添加元素
list.add("a");
System.out.println("第一个元素: " + list.get(0)); // 修正索引为 0
// 添加一个元素到索引 1,然后再修改它
list.add("b");
list.set(1, "D");
System.out.println("第二个元素: " + list.get(1)); // 修正操作顺序
// 删除索引为 1 的元素
list.remove(1);
// 遍历列表
for (String item : list) {
System.out.println("剩余元素: " + item);
}
}
}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
29import javax.swing.*; // 确保导入 Swing 的所有组件
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class hellp {
public static void main(String[] args) {
// 创建窗口
JFrame frame = new JFrame("我的桌面窗口");
frame.setSize(400, 300); // 设置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击关闭按钮退出程序
// 创建按钮
JButton button = new JButton("点我");
// 添加按钮的点击事件
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Hello, World!");
}
});
// 将按钮添加到窗口
frame.getContentPane().add(button);
// 显示窗口
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49import javax.swing.*;
import java.awt.*;
public class hellp {
public static void main(String[] args) {
// 创建主窗口
JFrame frame = new JFrame("自定义背景示例");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建自定义面板(带背景)
CustomPanel panel = new CustomPanel();
frame.setContentPane(panel); // 将自定义面板设置为内容面板
// 添加按钮(或其他组件)
JButton button = new JButton("点我");
button.setBounds(200, 300, 100, 40); // 设置按钮的位置和大小
panel.setLayout(null); // 使用绝对布局以便自定义组件位置
panel.add(button);
// 显示窗口
frame.setVisible(true);
}
}
// 自定义面板类,支持绘制背景
class CustomPanel extends JPanel {
private Image backgroundImage;
public CustomPanel() {
// 加载背景图片
backgroundImage = new ImageIcon("src/1.png").getImage(); // 替换为你的图片路径
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制背景图片
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
} else {
// 如果没有图片,则绘制纯色背景
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}