首页 文章

Canvas / JPanel没有显示在JFrame上?

提问于
浏览
1

我有一个 MyFrame 类扩展 JFrame . 我使用NET BEANS中的设计选项向此框架添加了组件(控件,即按钮) . 我有一个 MyCanvas 类,它使用重写的 paintComponent() 方法扩展 JPanel . 我正在尝试将此组件添加到 MyFrame 类 . 但是当我添加它并使其可见时,画布( JPanel )不会在 JFrame 上显示 . (首先我试图添加由 Canvas 扩展的 Mycanvas 类 . 但是后来我在这里读了一个线程尝试将其更改为 JPanel . 我也没有工作 . 而对于画布我显然使用绘画而不是 paintcomponent() )我的代码在这里

MyCanvas Class

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
}

My Main Program

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 . 有效 . 我还改变了我的Panel / Canvas并重新刷新了框架 . 这也很好 . 但我不能这样做 .

3 回答

  • 2

    这是你正在混合创建 JFrame 与IDE并自己创建 JPanel ,请记住IDE将所有组件添加到 JFrame initComponents() 中,理想情况下,您希望添加 Canvas .

    自己创建 JFrameJPanel (不使用Netbeans GUI Builder)

    要么

    您可以使用Netbeans的Palette Manager将 Component 添加到调色板,然后您可以像在任何其他类中一样在GUI构建器中使用它:

    (只需将 Canvas 类从项目树拖到GUI设计器中的 JFrame

    要确保您的自定义 Canvas 功能:

    • 覆盖 getPrefferedSize(..) 并返回适合的大小

    Reference:

  • 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 而不是程序main,否则不会执行 .

    还要检查 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);
    }
    

相关问题