我有以下代码来显示和绘制JPanel
缓冲图像。
我将其大小设置为缓冲图像的大小,但实际上并没有设置。
当我调整大小时它会显示滚动条,但是当我调整大小JFrame
大于面板大小BufferedImage
时,当我点击面板大小之外时,我仍然会收到鼠标事件。
我省略了额外的代码。
public class PaintFrame extends JFrame{
private JScrollPane paintScrollPane;
private Painter painter;
private JPanel paintPanel;
private BufferedImage paintImage;
private Color forgroundColor;
public PaintFrame(){
super("Paint");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
setLocationByPlatform(true);
getContentPane().setBackground(Color.black);
paintImage = new BufferedImage(700, 500, BufferedImage.TYPE_3BYTE_BGR);
paintPanel = new JPanel(){
@Override
public void paint(Graphics g){
if(paintImage != null){
g.drawImage(paintImage, 0, 0, paintImage.getWidth(), paintImage.getHeight(), null);
}
}
};
paintPanel.setBackground(Color.white);
paintPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me){
mouseClickedOnPaint(me);
}
});
paintPanel.setPreferredSize(new Dimension(paintImage.getWidth(), paintImage.getHeight()));
paintScrollPane = new JScrollPane(paintPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(paintScrollPane);
pack();
}
}