【问题标题】:Not able to compile the java program in netbeans [duplicate]无法在netbeans中编译java程序[重复]
【发布时间】:2017-10-26 18:25:25
【问题描述】:

我创建了一个 java 程序,但我无法在 netbeans 中运行它。当我尝试从另一个文件导入名为 invice 的类时。它抛出错误说

error: cannot find symbol
invice invc=new invice();

程序如下。我在一个名为 Invicedemo 的类中编写了 main 函数

package invicedemo;
    public class Invicedemo {
        public static void main(String[] args) {
                    invice invc=new invice();
            invc.setSize(300,250);
            invc.setTitle("party memo");
            invc.setVisible(true);
        }

    }

我要导入的类与文件名 invice.java 位于同一个包中

import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class invice extends JFrame implements ActionListener, KeyListener, FocusListener {

    JTextField tf, tf1, tf2, tf3;
    JButton bt, bt1;
    JLabel lb, lb1, lb2, lb3;
    Container cn;

    public invice() {
        cn = getContentPane();
        cn.setLayout(null);

        lb = new JLabel("Bill No.:");
        lb.setBounds(10, 10, 70, 25);
        cn.add(lb);
        tf = new JTextField();
        tf.setBounds(100, 10, 150, 25);
        cn.add(tf);

        lb1 = new JLabel("Party Name:");
        lb1.setBounds(10, 40, 70, 25);
        cn.add(lb1);
        tf1 = new JTextField();
        tf1.setBounds(100, 40, 150, 25);
        cn.add(tf1);

        lb2 = new JLabel("Date:");
        lb2.setBounds(10, 70, 70, 25);
        cn.add(lb2);
        tf2 = new JTextField();
        tf2.setBounds(100, 70, 150, 25);
        cn.add(tf2);

        lb3 = new JLabel("Gr No.:");
        lb3.setBounds(10, 100, 70, 25);
        cn.add(lb3);
        tf3 = new JTextField();
        tf3.setBounds(100, 100, 150, 25);
        cn.add(tf3);

        bt = new JButton("Store:");
        bt.setBounds(30, 160, 70, 25);
        cn.add(bt);

        bt1 = new JButton("Cancel:");
        bt1.setBounds(150, 160, 80, 25);
        cn.add(bt1);

        tf.addKeyListener(this);
        tf1.addKeyListener(this);
        tf2.addKeyListener(this);
        tf.addFocusListener(this);
        tf1.addFocusListener(this);
        tf2.addFocusListener(this);

        bt.addKeyListener(this);
        bt.addFocusListener(this);

        tf3.addKeyListener(this);
        tf3.addFocusListener(this);

        bt1.addKeyListener(this);
        bt1.addFocusListener(this);
        bt.addActionListener(this);
        bt1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == bt) {

            String party_name = tf.getText();
            String date = tf1.getText();
            String gr_no = tf2.getText();
            String Sno = tf3.getText();

            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "asiya");
                Statement st = con.createStatement();
                st.executeUpdate("Insert into invis values('" + Sno + "','" + party_name + "', '" + date + "', '" + gr_no + "')");

                JOptionPane.showMessageDialog(cn, "your data is successfully store", "storage", JOptionPane.INFORMATION_MESSAGE);

            } catch (Exception e) {
                JOptionPane.showMessageDialog(cn, e, "storage", JOptionPane.INFORMATION_MESSAGE);
            }

        }
        if (ae.getSource() == bt1) {
            tf.setText("");
            tf1.setText("");
            tf2.setText("");
            tf3.setText("");
        }
    }

    public void keyPressed(KeyEvent ke) {
        if (ke.getSource() == tf && ke.getKeyCode() == KeyEvent.VK_ENTER) {
            tf1.requestFocus();
        }

        if (ke.getSource() == tf1 && ke.getKeyCode() == KeyEvent.VK_ENTER) {
            tf2.requestFocus();
        }

        if (ke.getSource() == tf2 && ke.getKeyCode() == KeyEvent.VK_ENTER) {
            tf3.requestFocus();
        }
        if (ke.getSource() == tf3 && ke.getKeyCode() == KeyEvent.VK_ENTER) {
            bt.requestFocus();
        }

        if (ke.getSource() == bt && ke.getKeyCode() == KeyEvent.VK_ENTER) {
            bt1.requestFocus();

            tf.setText("");
            tf1.setText("");
            tf2.setText("");
        }

    }

    public void keyTyped(KeyEvent ke) {
    }

    public void keyReleased(KeyEvent ke) {
    }

    public void focusGained(FocusEvent fe) {
    }

    public void focusLost(FocusEvent fe) {
    }
}

谁能告诉我我做错了什么。

【问题讨论】:

  • 因为两个类不在同一个包中。你必须import你的班级invice
  • 您的班级中没有package 声明invice?这可能是问题所在。顺便说一句:坚持 Java 代码约定并编写类名CamelCase
  • @JigarJoshi 他们在同一个包下。
  • @Seelenvirtuose 这解决了我的问题,你能不能把它作为答案,这样我才能接受。是的,我会坚持你提到的约定。谢谢:)

标签: java netbeans symbols


【解决方案1】:

您只是忘记了第二堂课中的package 语句:

package invicedemo;
class invice ... { ... }

【讨论】:

  • 为什么这被否决了?
  • @JigarJoshi 好问题。这些天来,人们投票的速度有点快……我不在乎。
  • 投票应该是绝对的而不是相对的。如果您认为 answer 被高度评价并且您认为它不值得 upvote 。没有投票比投反对票更好。
  • @JigarJoshi 我知道有时很难正确地投票。但毕竟,如果一个答案没有提供答案或者本身是错误的或具有误导性,那么它应该被否决。没那么难,嗯?
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
相关资源
最近更新 更多