📄 mypanel.java
字号:
package lab9Package;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class MyPanel extends JPanel
implements ActionListener{
public JButton blueButton = new JButton("blue");
public JButton redButton = new JButton("red");
public Color color;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g1 = (Graphics2D)g;
//add Button
add(blueButton);
add(redButton);
setLayout(new FlowLayout());
//add ActionListener
blueButton.addActionListener(this);
redButton.addActionListener(this);
//draw a ellipse
Ellipse2D ellipse = new Ellipse2D.Double(100,100,50,50);
g1.setPaint(color);
g1.fill(ellipse);
}
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o == blueButton)
{
color = Color.BLUE;
//updateUI();
repaint();
}
if(o == redButton)
{
color = Color.RED;
//updateUI();
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -