无法在主框架类

时间:2017-11-04 15:58:40

标签: java swing jframe jpanel

TL;博士 我刚刚开始学习java和swing而且我有这个问题在主类里面如果我试图调用我的自定义类中定义的任何自定义方法扩展JPanel,我得到一个错误,上面写着“找不到符号”,就好像那样方法甚至没有声明。 我三倍检查自定义类对象看起来正确启动,并将方法设置为公共。知道为什么会这样吗?

起初我使用的是Intellij Idea,但是gui设计师使用它是如此令人沮丧,我认为这是IDE的错误,但即使在切换到NetBeans并从头开始重写代码后,我也会遇到同样的错误。

我想编写一个名为Drawing的类,它扩展了可以绘制自定义Circuit类对象的JPanel。基本上我想为应用程序做的是从File菜单中选择New将创建并绘制该Circuit对象。为此,我想按下New按钮将调用在Drawing类中定义的setCircuit()方法,该方法初始化电路,然后paintComponent()将绘制电路。

以下代码:

import circuit.*;
import java.awt.Graphics;
public class Drawing extends javax.swing.JPanel {
private Circuit circuit = null;

public Drawing(){

}

public void setCircuit(Circuit circuit) {
    this.circuit = circuit;
}

@Override
public void paintComponent(Graphics graphics){
    super.paintComponent(graphics);
    if(circuit == null){
        graphics.drawString("Create a new circuit.", 10, 20);
    } else{
        // draws the circuit
    }

}

}

import circuit.*;
public class GUI extends javax.swing.JFrame {
private Circuit circuit = null;

public GUI() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    drawing = new Drawing();
    menuBar = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    newMenuItem = new javax.swing.JMenuItem();
    exitMentuItem = new javax.swing.JMenuItem();
    exitMenu = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("My app");
    setName("mainFrame"); // NOI18N

    javax.swing.GroupLayout drawingLayout = new javax.swing.GroupLayout(drawing);
    drawing.setLayout(drawingLayout);
    drawingLayout.setHorizontalGroup(
        drawingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    drawingLayout.setVerticalGroup(
        drawingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );

    fileMenu.setText("File");

    newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
    newMenuItem.setText("New");
    newMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(newMenuItem);

    exitMentuItem.setText("Exit");
    exitMentuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitMentuItemActionPerformed(evt);
        }
    });
    fileMenu.add(exitMentuItem);

    menuBar.add(fileMenu);

    exitMenu.setText("Edit");
    menuBar.add(exitMenu);

    setJMenuBar(menuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(drawing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(drawing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
    circuit = new Circuit();
    drawing.setCircuit(circuit); // this is where I get the error:
/*
cannot find symbol
symbol: setCircuit(Circuit)
location: variable drawing of type JPanel
*/
}                                           

private void exitMentuItemActionPerformed(java.awt.event.ActionEvent evt) {                                              
    System.exit(0);
}                                             

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JPanel drawing;
private javax.swing.JMenuItem exitMentuItem;
private javax.swing.JMenu exitMenu;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem newMenuItem;
// End of variables declaration                   

}

0 个答案:

没有答案