我对重绘方法有问题。 如果我只是制作面板并绘制,效果很好。 IMAGE 1
但是,如果我用East面板制作JFrame并向其中添加按钮,并从另一个必须单击和绘制对象的类中制作一个面板,则当我重新痛苦时,它会重新绘制JFrame的按钮,而我只想重新绘制那些我画。
JFrame的代码:
package drawing;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.MatteBorder;
public class DrawingFrm extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DrawingFrm frame = new DrawingFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DrawingFrm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 682, 422);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
PnlDrawing pnldrawing = new PnlDrawing();
pnldrawing.setBorder(null);
contentPane.add(pnldrawing, BorderLayout.CENTER);
GroupLayout gl_pnldrawing = new GroupLayout(pnldrawing);
gl_pnldrawing.setHorizontalGroup(
gl_pnldrawing.createParallelGroup(Alignment.LEADING)
.addGap(0, 577, Short.MAX_VALUE)
);
gl_pnldrawing.setVerticalGroup(
gl_pnldrawing.createParallelGroup(Alignment.LEADING)
.addGap(0, 357, Short.MAX_VALUE)
);
pnldrawing.setLayout(gl_pnldrawing);
JPanel pnlLevo = new JPanel();
pnlLevo.setBorder(null);
contentPane.add(pnlLevo, BorderLayout.WEST);
JButton btnPoint = new JButton("Point");
JButton btnLine = new JButton("Line");
JButton btnRectangle = new JButton("Rectangle");
JButton btnCircle = new JButton("Circle");
JButton btnDonut = new JButton("Donut");
JButton btnModify = new JButton("Modify");
JButton btnDelete = new JButton("Delete");
GroupLayout gl_pnlLevo = new GroupLayout(pnlLevo);
gl_pnlLevo.setHorizontalGroup(
gl_pnlLevo.createParallelGroup(Alignment.LEADING)
.addGroup(gl_pnlLevo.createSequentialGroup()
.addContainerGap()
.addGroup(gl_pnlLevo.createParallelGroup(Alignment.LEADING)
.addComponent(btnRectangle, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnPoint, GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(btnLine, GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(btnCircle, GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(btnDonut, GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(btnModify, GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(btnDelete, GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))
.addContainerGap())
);
gl_pnlLevo.setVerticalGroup(
gl_pnlLevo.createParallelGroup(Alignment.LEADING)
.addGroup(gl_pnlLevo.createSequentialGroup()
.addGap(34)
.addComponent(btnPoint)
.addGap(18)
.addComponent(btnLine)
.addGap(18)
.addComponent(btnRectangle)
.addGap(18)
.addComponent(btnCircle)
.addGap(18)
.addComponent(btnDonut)
.addGap(18)
.addComponent(btnModify)
.addGap(18)
.addComponent(btnDelete)
.addContainerGap(54, Short.MAX_VALUE))
);
pnlLevo.setLayout(gl_pnlLevo);
}
}
JPanel的代码
package drawing;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import oblici.Point;
import oblici.Shapes;
public class PnlDrawing extends JPanel implements MouseListener{
int x,y;
ArrayList <Shapes> shapesarr = new ArrayList <Shapes>();
public PnlDrawing() {
addMouseListener(this);
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
x = e.getX();
y = e.getY();
System.out.println(x + " " + y);
System.out.println(shapesarr);
repaint();
}
public void paint(Graphics g) {
Point p = new Point(x,y);
shapesarr.add(p);
for (Shapes shapes : shapesarr) {
shapes.draw(g);
}
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
}
}