如何修复 Intelij“无法解析符号”

问题描述 投票:0回答:1

我一直在尝试用 javafx 制作一个计算器,但我不断收到错误,这并没有让我运行该程序:(

代码:

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

public class Main implements ActionListener {

    JFrame frame;
    JTextField textField;
    JButton[] NumberButtons = new JButton[10];
    JButton[] FunctionButtons = new JButton[8];
    JButton addButton , subButton , mulButton , divButton;
    JButton decButton , eqlButton , clrButton , delButton;
    JPanel panel;
    
    Font myfont = new Font("8514oem", Font.BOLD,30);
    double num1 = 0 , num2 = 0 , result= 0;
    char operator;
    
    void Calculator(){    
        frame = new JFrame("Calculator");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420,550);
        frame.setLayout(null);
    }
    
    public static void main(String[]args){
        Calculator calc = new Calculator(); /* this is the part where it is showing the error "cannot resolve symbol "Calculator" */
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
    }
}

我试图运行这个程序来看看我的 Jframe 是否工作。但由于这个错误我无法...

java ide
1个回答
0
投票

错误“无法解析符号Calculator”是因为类名是“Main”,而不是“Calculator”。您应该将行 Calculator calc = new Calculator(); 更改为 Main calc = new Main();

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

public class Main implements ActionListener {

    JFrame frame;
    JTextField textField;
    JButton[] NumberButtons = new JButton[10];
    JButton[] FunctionButtons = new JButton[8];
    JButton addButton, subButton, mulButton, divButton;
    JButton decButton, eqlButton, clrButton, delButton;
    JPanel panel;

    Font myfont = new Font("8514oem", Font.BOLD, 30);
    double num1 = 0, num2 = 0, result = 0;
    char operator;

    public Main() {
        frame = new JFrame("Calculator");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 550);
        frame.setLayout(null);

        textField = new JTextField();
        textField.setBounds(50, 25, 300, 50);
        textField.setFont(myfont);
        frame.add(textField);

        // Add buttons, panels, and set their properties here

    }

    public static void main(String[] args) {
        Main calc = new Main();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // Handle button clicks here
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.