[英].drawLine on event (button click) Jpanel/Jbutton/JTabbedPane
我知道如何為按鈕點擊動作/搖擺動作偵聽器,但是我有一個此類的東西,但我希望它具有一個功能/事件,即單擊按鈕時它會運行類似於下面的PaintComponent的方法... (畫一條線)
class CustomPanel extends JPanel {
private int destx = 100;
private int desty = 100;
private int startx = 0;
private int starty = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(startx, starty, destx, desty);
}
}
如何從動作偵聽器中調用此paintcomponent(或類似的畫線的組件)?
這是我的動作偵聽器:(在GUI.java上,而在上面的代碼在CustomPanel.java上)
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == loginButton)
{
//Does other stuff but removed for simplifying
CustomPanel cp = new CustomPanel();
}
}
非常感謝,
您需要將其添加到gui。 像這樣:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parentPanel.add(new CustomPanel());
parentPanel.revalidate();
parentPanel.repaint();
}
});
但是,如果您只想在當前容器上畫一條線,那是另一回事了……
只需將CustomPanel
添加到任何其他JComponent
並更新UI即可解決問題。 Swing為您處理所有繪畫。
這是一個非常有用的揮桿繪畫指南;
http://java.sun.com/products/jfc/tsc/articles/painting/#paint_process
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.