我有两个JPanel
s。第一个包含s ,第二个简单地我们可以使用MouseJButton
绘制。问题是当我单击并开始在. 请给我一些方向,关于我不看的地方?JButton
JButton
JPanel
主班
public class LabelDemo extends JFrame {
JPanel p1 = new JPanel();
painter p2 = new painter();
JButton red = new JButton("red");
JButton blue = new JButton(" blue ");
JLabel lbl = new JLabel("Label");
ImageIcon icon = new ImageIcon("image/YouTube.png");
public LabelDemo() {
setLayout(new BorderLayout());
p1.setBorder(new LineBorder(Color.gray));
//jbt1.setIcon(icon);
p1.add(red);
p1.add(blue);
lbl.setOpaque(true);
lbl.setBackground(Color.yellow);
p1.add(lbl);
p1.setBounds(20, 30, 40, 78);
add(p1,BorderLayout.EAST);
add(p2,BorderLayout.CENTER);
}
public static void main(String[] args){
LabelDemo frame = new LabelDemo();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 400);
frame.setLocationRelativeTo(null);
}
}
客栈类
class painter extends JPanel {
int x , y;
boolean isPresed = false;
public void setPainter(int x , int y) {
this.x = x;
this.y = y;
}
public painter() {
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
isPresed = true;
setPainter(e.getX(),e.getY());
repaint();
}
});
}
protected void paintComponent(Graphics g){
Color randomColor = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F );
if(isPresed){
g.setColor(randomColor);
g.fillOval(x-5, y-5, 10, 10);
}
}
}//end of painter