我一直在試圖在最後兩天在我的JPanel
上繪製對象。當我試圖在JPanel
上繪製對象時,代碼正在運行,當它未放置在JTabbedPane
中時。但現在對我來說太忙了。請幫助我。在JPanel上繪製對象放置在JTabbedPane
與MouseMotionListener
panel[i].addMouseMotionListener(this);
加入面板註冊JPanel
到JTabbedPane
tp.addTab("Tab1",panel[i]);
mouseDragged
事件具有以下代碼
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
事件的mouseReleased
public void mouseReleased(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
repaint();
}
塗料功能(即必須被自動調用)具有以下線
public void paint(Graphics g)
{
super.paint(g);
// I even tried to add the following line but it didn't work too
/// g=panel[tp.getSelectedIndex()].getGraphics();
Graphics2D gtd=(Graphics2D) g;
gtd.fillOval(x, y, x1-x, y1-y); //x1-x: For width of Oval; y1-y: for height
}
發佈[MCVE(鏈接)](http://stackoverflow.com/help/mcve)。 – user1803551
不要重寫'paint',否則重寫'paintComponent'。 – user1803551
請參見[相關](http://stackoverflow.com/a/23183787/2587435) –