📄 taijitu.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.util.*;
public class paopao extends JFrame {
int width = 400;
int height = 300;
public taijitu() {
setSize(width, height);
setTitle("改变泡泡大小");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
setLocation(x, y);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
taijitu frame = new taijitu();
DrawPanel draw = new DrawPanel();
Container c = frame.getContentPane();
JPanel button = new JPanel();
JButton jb1 = new JButton("变大泡泡");
JButton jb2 = new JButton("减小泡泡");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.print("nihap");
DrawPanel.x += 5;
//定义为静态数据 用类名来直接访问
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.print("nihap");
DrawPanel.x -= 5;
//定义为静态数据 用类名来直接访问
}
});
button.add(jb1);
button.add(jb2);
button.setBackground(Color.BLUE);
c.add(button, BorderLayout.SOUTH);
c.add(draw, BorderLayout.CENTER);
frame.setVisible(true);
}
}
class DrawPanel extends JPanel {
public DrawPanel() {
setBackground(Color.cyan);
}
public void paintComponent(Graphics g) {
repaint();
super.paintComponent(g);
g.drawString("第一个泡泡的当前大小是:" + x, 20, 20);
g.setColor(Color.BLACK);
g.fillArc(m, n, x, x, 0, 180);
g.setColor(Color.WHITE);
g.fillArc(m, n, x, x, 180, 180);
g.setColor(Color.BLACK);
g.fillArc(m + x / 2, n + x / 4, x / 2, x / 2, 180, 180);
g.setColor(Color.WHITE);
g.fillArc(m, n + 3 + x / 4, x / 2, x / 2, 0, 180);
g.fillOval(m + x * 3 / 4 - 5, n + x / 2 - 5, 10, 10);
g.setColor(Color.BLACK);
g.fillOval(m + x / 4 - 5, n + x / 2 - 5, 10, 10);
g.drawString("第二个泡泡的当前大小是:" + x, 30, 40);
g.setColor(Color.BLACK);
g.fillArc(p, n, x, x, 0, 180);
g.setColor(Color.WHITE);
g.fillArc(p, n, x, x, 180, 180);
g.setColor(Color.BLACK);
g.fillArc(p + x / 2, n + x / 4, x / 2, x / 2, 180, 180);
g.setColor(Color.WHITE);
g.fillArc(p, n + 3 + x / 4, x / 2, x / 2, 0, 180);
g.fillOval(p + x * 3 / 4 - 5, n + x / 2 - 5, 10, 10);
g.setColor(Color.BLACK);
g.fillOval(p + x / 4 - 5, n + x / 2 - 5, 10, 10);
}
Random r = new Random();
int i = r.nextInt(50) + 1;
int m = 20, n = 60,p=200;
static int x = 100;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -