我有两个JPanel
个。第一个包含JButton
s,在第二个中我们可以使用鼠标进行绘制。问题是,当我点击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);
}
}
inr class
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
答案 0 :(得分:0)
好的,正如我所知,你想在点击红色或蓝色按钮时摆脱东面板(p1面板):
package stack;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
import javax.swing.border.LineBorder;
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);
p2.setPreferredSize(new Dimension(600,400));
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);
red.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove(p1);
repaint();
revalidate();
}
});
blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove(p1);
repaint();
revalidate();
}
});
}
public static void main(String[] args){
LabelDemo frame = new LabelDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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
另外不要调用setSize()方法调用pack()和setPreferredSize(),因为这个原因,我被批评了几次。只是一个注释。
答案 1 :(得分:0)
在painter
窗格的paintComponent
方法中,您真的应该先致电super.paintComponent
。
Swing重用Graphics
,因此可以将旧内容保留在其缓冲区中。如果您致电super.paintComponent
,则会将其清理为
protected void paintComponent(Graphics g){
// Must call super.paintComponent() so the Graphics is updated correctly...
super.paintComponent();
Color randomColor = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F );
if(isPresed){
g.setColor(randomColor);
g.fillOval(x-5, y-5, 10, 10);
}
}