2012-10-26 82 views
1

我有一個MyFrame類延伸JFrame。我使用NET BEANS中的設計選項將組件(控件即按鈕)添加到此框架。我有一個MyCanvas類,它使用覆蓋的paintComponent()方法擴展了JPanel。我正在嘗試將此組件添加到MyFrame類。但是,當我添加它並使其可見時,畫布(JPanel)不會顯示在JFrame上。 (首先,我試圖添加Mycanvas類延長Canvas。但是,然後我在這裏讀一個線程,嘗試將其更改爲JPanel。我也沒有工作。對於畫布我顯然使用油漆不是paintcomponent()) 我的代碼是這裏下面Canvas/JPanel不顯示在JFrame上?

MyCanvas類

public class MyCanvas extends javax.swing.JPanel{ 

MyCanvas() 
{ 
     super(); 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    Graphics2D graphics2D=(Graphics2D)g; 

    graphics2D.drawRect(10, 10, 10, 10); 

    graphics2D.drawOval(0, 0, 100, 100); 
} 

} 

MyFrame

public class Myframe extends javax.swing.JFrame { 

public Myframe() { 

    initComponents(); 
    @SuppressWarnings("unchecked") 
    +generatedcode by the designer 

private void RectangleActionPerformed(java.awt.event.ActionEvent evt) { 
} 

private void SquareActionPerformed(java.awt.event.ActionEvent evt) { 
} 

private void CircleActionPerformed(java.awt.event.ActionEvent evt) { 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new Myframe().setVisible(true); 
     } 
    }); 
} 
public void run() { 
     new Myframe().setVisible(true); 
    } 
// Variables declaration - do not modify 
private javax.swing.JButton Circle; 
private javax.swing.JButton Rectangle; 
private javax.swing.JButton Square; 
// End of variables declaration 
} 

我的主要計劃

public static void main(String[] args) { 

    MyCanvas c = new MyCanvas(); 
    Myframe f= new Myframe();//Works if used JFrame instead of MyFrame 

    f.add(c); 
    f.setVisible(true); 
} 

基本上我想創造,我想這可以觸發事件和改變是什麼畫在畫布上的按鈕的GUI。我用空的jframe試了一下。將畫布/面板添加到JFrame。有效。此外,我改變了我的面板/帆布,並重新框架。這也很好。但我無法這樣做。

回答

2

的是,你是混合創建JFrame與IDE和創建JPanel自己,記住IDE會將所有的組件在initComponents()JFrame其理想,你會希望有你的加入Canvas

可以創建自己的JFrameJPanel(不使用NetBeans GUI Builder中的)

你可以使用NetBeans的面板管理器添加您Component的調色板,那麼你就可以使用GUI Builder中,就像任何其他類:

  • ​​

(只需從項目樹上拖動Canvas類的JFrame在GUI設計師)

要確保您的自定義Canvas功能:

  • 覆蓋getPrefferedSize(..)並返回其尺寸配合

參考:

+0

仍然不工作 –

+0

@MuhammadTaha看到更新請 –

+0

好吧,現在我已經根據你的指示編輯,我的畫布顯示自己。但是,由於initcomponents方法已被刪除,它不顯示我添加的按鈕。我想按鈕和畫布應該顯示自己。這是預期的結果。任何方式根據我的要求做到這一點? –

0

嘗試將其更改爲:

public static void main(String[] args) { 

    MyCanvas c = new MyCanvas(); 
    Myframe f= new Myframe();//Works if used JFrame instead of MyFrame 

    f.add(c); 
    c.setVisible(true); 
    f.setVisible(true); 
} 

而且,在你MyFrame類代碼:

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new Myframe().setVisible(true); 
     } 
    }); 
} 

將不會被執行,除非你正在運行MyFrame,而不是你的程序主要的。

同時檢查您的MyFrame構造函數中是否有super()調用。

+0

沒有doesn.t工作。我也試過很多時間 'f.invalidate();'' f.validate();'' f.repaint() ;' –

+0

我如何運行MyFrame的主代碼(正如你所說的)? 在MyFrame中添加了super(),但它仍然不起作用。 –

0
@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    g.setColor(Color.BLACK); 

    Graphics2D graphics2D=(Graphics2D)g; 

    graphics2D.drawRect(10, 10, 10, 10); 

    graphics2D.drawOval(0, 0, 100, 100); 
}