我有以下代碼來顯示和繪製JPanel
與緩衝的圖像。JPanel大小問題,當我調整
我將其大小設置爲緩衝圖像的大小,但實際上並未設置。
它顯示滾動條時我調整,但是當我調整比面板或BufferedImage
的大小JFrame
更大,我仍然得到鼠標事件,當我點擊面板的尺寸之外。
我省略了額外的代碼。
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();
}
}
增加進口和註釋掉'Painter'後,我仍然得到一個'mouseClickedOnPaint(me);'錯誤。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-21 05:36:32
@AndrewThompson你有沒有在粘貼? :P – Sinkingpoint 2013-04-21 05:37:55
@Quirliom它的最後部分?當然。什麼,你覺得我很厚? – 2013-04-21 05:48:38